根据不同的标签页选择隐藏或显示文本框

时间:2012-03-23 21:36:31

标签: c# winforms c#-4.0

首先,我确实找到了这个有用但不完全符合我需要的东西 Windows Forms event "On Select Tab"?

仅涵盖整个选项卡控件的选择更改事件,而不是每个单独的选项卡页面。

我需要做的是根据我选择的标签页隐藏或显示某些文本框。除了click事件之外,下面的示例不起作用。 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;
}

3 个答案:

答案 0 :(得分:5)

使用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;
   }
}

答案 1 :(得分:1)

你可能会这样:

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;
}

答案 2 :(得分:0)

使用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;
  }
}