C# 组合框。可见属性不工作
晚上,我有以下代码,在一个静态类中,它帮助我的userInterface部分类。每当我到达检查combobox/textbox是否可见的部分时:C# 组合框。可见属性不工作,c#,winforms,c#-4.0,combobox,textbox,C#,Winforms,C# 4.0,Combobox,Textbox,晚上,我有以下代码,在一个静态类中,它帮助我的userInterface部分类。每当我到达检查combobox/textbox是否可见的部分时: if (cb.Visible == true) & 即使控件在窗体上可见,它也始终为false 有什么想法吗 谢谢 public static bool VerifyTableLayoutControlsContainData(TableLayoutPanel tlp) { foreach (Control input in
if (cb.Visible == true)
&
即使控件在窗体上可见,它也始终为false
有什么想法吗
谢谢
public static bool VerifyTableLayoutControlsContainData(TableLayoutPanel tlp)
{
foreach (Control input in tlp.Controls)
{
ComboBox cb = input as ComboBox;
if(cb != null)
{
if (cb.Visible == true)
{
if (string.IsNullOrEmpty(cb.SelectedItem.ToString()))
{
return false;
}
}
}
TextBox tb = input as TextBox;
if (tb != null)
{
if (tb.Visible == true)
{
if (string.IsNullOrEmpty(tb.Text))
{
return false;
}
}
}
}
return true;
}
Edit1:
用户接口代码
private void uiBtnDataVerification_Click(object sender, EventArgs e)
{
if (VerifyingData != true)
{
AllInputsContainDataCsvFileVerificationStage = true;
//Check all inputs have something in them
InputsContainDataCsvFileVerificationStage();
if (AllInputsContainDataCsvFileVerificationStage == false)
{
UiHelper.UpdateLog("One or more inputs has not been specified.", uiTxtOperationLog);
return;
}
...
}
else
{
//Cancel permanent cancellation token.
}
}
public void InputsContainDataCsvFileVerificationStage()
{
....
if (UiHelper.VerifyTableLayoutControlsContainData(uiTableLayoutPanelColumnDataTypes)) { }
else
{
AllInputsContainDataCsvFileVerificationStage = false;
}
....
}
原始代码位于UiHelper类中
Edit2:根据汤姆的建议,我做了以下更改
public userInterface()
{
InitializeComponent();
uiTableLayoutPanelColumnDataTypes.VisibleChanged += new EventHandler(notifyMe);
uiCBColumn1DataType.VisibleChanged += new EventHandler(notifyMe1);
SortedColumnNames = new List<TextBox>();
SortedDataTypes = new List<ComboBox>();
AllInputsContainDataCsvFileVerificationStage = true;
}
public void notifyMe1(object sender, EventArgs e)
{
bool temp = uiCBColumn1DataType.Visible;
MessageBox.Show("its been changed");
}
public void notifyMe(object sender, EventArgs e)
{
bool temp = uiTableLayoutPanelColumnDataTypes.Visible;
MessageBox.Show("its been changed");
}
公共用户界面()
{
初始化组件();
uiTableLayoutPanelColumnDataTypes.VisibleChanged+=新事件处理程序(notifyMe);
uiCBColumn1DataType.VisibleChanged+=新事件处理程序(notifyMe1);
SortedColumnNames=新列表();
SortedDataTypes=新列表();
AllInputsContainDataCsvFileVerificationStage=true;
}
public void notifyMe1(对象发送方,事件参数e)
{
bool temp=uiCBColumn1DataType.Visible;
MessageBox.Show(“它已被更改”);
}
public void notifyMe(对象发送方,事件参数)
{
bool temp=uiTableLayoutPanelColumnDataTypes.Visible;
MessageBox.Show(“它已被更改”);
}
在单击按钮之前,我检查了cb1可见性属性设置为什么,它是真的。当我尝试通过原始方法进行检查时,仍然会出现错误
我被难住了
Edit3:
似乎当我单击第二个选项卡时,组合框的visible属性=false
有人知道为什么会这样吗?从我的评论中:
当运行验证代码时,TableLayoutPanel控件必须在屏幕上可见。如果它位于非活动选项卡页后面,则它不在屏幕上,控件将报告Visible属性为false,而不管该属性在设计器中是否设置为true
表单中是否有combobox的子类?代码在combobox上运行时出现异常,我必须为
SelectedIndex>-1
检查添加一个复选框。你什么时候调用这个例程?我单击一个按钮来执行文件验证。此代码是在我创建任务之前运行的。是否可以尝试捕获事件[visibleChanged]:在运行验证代码时,TableLayoutPanel控件必须在屏幕上可见。如果它位于非活动选项卡页后面,则它不在屏幕上。
public userInterface()
{
InitializeComponent();
uiTableLayoutPanelColumnDataTypes.VisibleChanged += new EventHandler(notifyMe);
uiCBColumn1DataType.VisibleChanged += new EventHandler(notifyMe1);
SortedColumnNames = new List<TextBox>();
SortedDataTypes = new List<ComboBox>();
AllInputsContainDataCsvFileVerificationStage = true;
}
public void notifyMe1(object sender, EventArgs e)
{
bool temp = uiCBColumn1DataType.Visible;
MessageBox.Show("its been changed");
}
public void notifyMe(object sender, EventArgs e)
{
bool temp = uiTableLayoutPanelColumnDataTypes.Visible;
MessageBox.Show("its been changed");
}