【问题标题】:If combobox selected then show如果选择了组合框,则显示
【发布时间】:2014-08-12 12:26:00
【问题描述】:

我必须加载一个表单,在这个表单中我想隐藏某些标签和文本框。另外我想显示符合条件if combo-box selected =="Something"的标签和文本框@

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox1.Text == "Something")
    {
        label1.Show();
        label2.Show();
        textBox1.Show();
        textBox2.Show();
    }
}

我如何在选择组合框后显示这些标签和文本框

【问题讨论】:

  • 我假设您使用的是 ASP.NET 而不是 WinForms...确保您使用的是类似于以下内容的
  • 这是 ASP.NET、WinForms 还是 WPF?

标签: c# combobox selectedindexchanged


【解决方案1】:

试试

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox1.SelectedValue == "Something")
    {
        label1.Visible = true;
        label2.Visible = true;
        textBox1.Visible = true;
        textBox2.Visible = true;
    }
}

【讨论】:

  • @Weblover 试试 if (!string.IsNullOrEmpty(comboBox1.SelectedValue))
【解决方案2】:

我会这样尝试,不需要 if:

label1.Visible = label2.Visible = textBox1.Visible = textBox2.Visible = 
           comboBox1.SelectedValue.toString() == "Something";

【讨论】:

    【解决方案3】:

    试试下面的代码

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        var item = (ComboBoxItem)ComboBox.SelectedItem;
    
        if (item == null) 
            return;
    
        var content = (string) item.Content;
        if(content == "Something")
        {
            label1.Visible = true;
            label2.Visible = true;
            textBox1.Visible = true;
            textBox2.Visible = true;
        }
    }
    

    【讨论】:

      【解决方案4】:

      仔细查看 ComboBox 及其相关属性的功能可能会有所帮助。您遇到的问题可能与“.Text”字段未反映当前选定的项目有关。

      SelectedItem:获取或设置 ComboBox 中当前选定的项目。
      基于 ComboBox.SelectionChangeCommitted

      文本: 获取或设置与此控件关联的文本。 (覆盖 Control.Text。)
      设置文本值将更改组合框的当前值

      SelectedValue: 获取或设置由 ValueMember 属性指定的成员属性的值。 (继承自 ListControl。)
      基于 ListControl.SelectedValueChanged

      来源msdn
      进一步阅读dotnetperls

      构建我制作的这个演示程序,以便学习查看它的实际效果。


      XAML

      <Window x:Class="WpfApplication1.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow" Height="220" Width="711" Background="#FF6937D4">
      <Grid>
          <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="10,38,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="333"/>
          <ComboBox x:Name="comboBox1" SelectedValuePath="Content" HorizontalAlignment="Left" Margin="10,7,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="comboSelectChanged">
              <ComboBoxItem Content="Zero" Tag="Tag_Zero"/>
              <ComboBoxItem Content="One" Tag="Tag_One"/>
              <ComboBoxItem Content="Two" Tag="Tag_Two"/>
              <ComboBoxItem Content="Three" Tag="Tag_Three"/>
          </ComboBox>
          <TextBox x:Name="textBox2" HorizontalAlignment="Left" Height="23" Margin="10,66,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="333"/>
          <TextBox x:Name="textBox3" HorizontalAlignment="Left" Height="23" Margin="10,94,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="333"/>
          <TextBox x:Name="textBox4" HorizontalAlignment="Left" Height="23" Margin="10,122,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="333"/>
          <Label Content="comboBox1.SelectedItem.ToString()" HorizontalAlignment="Left" Margin="348,38,0,0" VerticalAlignment="Top" Foreground="White" Height="23" Width="215" Padding="7,3,0,0" ScrollViewer.CanContentScroll="True" UseLayoutRounding="True"/>
          <Label Content="comboBox1.Text" HorizontalAlignment="Left" Margin="348,66,0,0" VerticalAlignment="Top" Foreground="White" Height="23" Width="215" Padding="7,3,0,0" ScrollViewer.CanContentScroll="True" UseLayoutRounding="True"/>
          <Label Content="comboBox1.SelectedIndex.ToString()" HorizontalAlignment="Left" Margin="348,94,0,0" VerticalAlignment="Top" Foreground="White" Height="23" Width="215" Padding="7,3,0,0" ScrollViewer.CanContentScroll="True" UseLayoutRounding="True"/>
          <Label Content="comboBox1.SelectedValue.ToString()" HorizontalAlignment="Left" Margin="348,122,0,0" VerticalAlignment="Top" Foreground="White" Height="23" Width="215" Padding="7,3,0,0" ScrollViewer.CanContentScroll="True" UseLayoutRounding="True"/>
          <TextBox x:Name="textBox5" HorizontalAlignment="Left" Height="23" Margin="10,150,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="333"/>
          <Label Content=" (comboBox1.SelectedItem as ComboBoxItem).Content as string" HorizontalAlignment="Left" Margin="348,150,-119,0" VerticalAlignment="Top" Foreground="White" Height="23" Width="360" Padding="7,3,0,0" ScrollViewer.CanContentScroll="True" UseLayoutRounding="True"/>
          <Button x:Name="btnSelect" Content="Select based on value" HorizontalAlignment="Left" Margin="175,7,0,0" VerticalAlignment="Top" Width="168" Click="btnSelect_Click"/>
      </Grid>
      

      XAML.CS

      using System;
      using System.Windows;
      using System.Windows.Controls;
      
      namespace WpfApplication1
      {
          public partial class MainWindow : Window
          {
              public MainWindow() {
                  InitializeComponent();
              }
      
              private void comboSelectChanged(object sender, SelectionChangedEventArgs e) {
                  textBox1.Text = comboBox1.SelectedItem.ToString();
                  textBox2.Text = comboBox1.Text;
                  textBox3.Text = comboBox1.SelectedIndex.ToString();
                  textBox4.Text = comboBox1.SelectedValue.ToString();
                  textBox5.Text = (comboBox1.SelectedItem as ComboBoxItem).Content as string;  
               }
      
              private void btnSelect_Click(object sender, RoutedEventArgs e) {
                  // Winform working code:  comboBox1.SelectedIndex = comboBox1.FindString("string");
                  // WPF - This REQUIRES "SelectedValuePath="Content"" in XAML combobox def.
                  comboBox1.SelectedValue = "Three";
              }
          }
      }
      

      【讨论】:

        【解决方案5】:
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
        if (comboBox1.SelectedValue == "Something")
        {
            label1.Visible = true;
            label2.Visible = true;
            textBox1.Visible = true;
            textBox2.Visible = true;
        }
        }
        

        为下拉菜单设置 autoPostback true。

        【讨论】:

          【解决方案6】:
          private void cmbPosition_SelectedIndexChanged(object sender, EventArgs e)
          {
              if(cmbPosition.Text =="Staff")
              {
                  label11.Visible = true;
                  txtRole.Visible = true;
              }
          }
          

          【讨论】:

          • 请改进这篇文章。现在是代码转储。
          【解决方案7】:

          试试这个:

          private void datagrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
              {
                  if (datagrid.SelectedItem == null || datagrid.SelectedItem.ToString() == "{NewItemPlaceholder}")
                  {
                      btnRemove.Visibility = System.Windows.Visibility.Hidden;
                  }
                  else
                  {
                      btnRemove.Visibility = System.Windows.Visibility.Visible;
                  }
              }
          

          【讨论】:

            猜你喜欢
            • 2013-02-02
            • 1970-01-01
            • 2015-10-08
            • 2014-09-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多