根据不同的选项卡页面选择隐藏或显示文本框
本文关键字:隐藏 选择 显示 文本 选项 | 更新日期: 2023-09-27 18:28:54
首先,我确实发现了这一点,它很有用,但并不完全是我所需要的Windows窗体事件";在选择选项卡上"?
这只涵盖整个选项卡控件的选择更改事件,而不是每个单独的选项卡页。
我需要做的是根据我选择的选项卡页面隐藏或显示某些文本框。除了点击事件外,下面的示例不起作用。ExpenseReport和ExpenseReview是我的两个选项卡。任何帮助都将不胜感激!
private void tpExpenseReport_Click(object sender, EventArgs e)
{
txtTripNo.Visible = true;
txtTripNo2.Visible = false;
}
private void tpExpenseReview_Click(object sender, EventArgs e)
{
txtTripNo.Visible = false;
txtTripNo2.Visible = true;
}
使用TabControl的SelectedIndexChanged事件。
假设只有两个选项卡。(如果有更多,我会使用switch语句,而不是If.else)
tabControl1.SelectedIndex_Changed(object sender, EventArgs e)
{
if(tabControl1.SelectedIndex == 0)
{
txtTripNo.Visible = true;
txtTripNo2.Visible = false;
}
else
{
txtTripNo.Visible = false;
txtTripNo2.Visible = true;
}
}
你可能会这样做:
private void tabControl_SelectedIndexChanged(...)
{
int index = tabControl.SelectedIndex;
if (index == 0) HideTextBoxes(false);
else HideTextBoxes(true);
}
private void HideTextBoxes(bool someSelector)
{
txtTripNo.Visible = someSelector;
txtTripNo2.Visible = !someSelector;
}
最好使用Selected
事件。它通过TabControlEventArgs
参数中选择的实际选项卡:
private void tabControl1_Selected(object sender, TabControlEventArgs e) {
if (e.TabPage == tpExpenseReport) {
txtTripNo.Visible = true;
txtTripNo2.Visible = false;
} else if (e.TabPage == tpExpenseReview) {
txtTripNo.Visible = false;
txtTripNo2.Visible = true;
}
}