如果选择了组合框,则显示

时间:2014-08-12 12:26:01

标签: c# combobox selectedindexchanged

我必须加载表单,并且在此表单中我想隐藏某些标签和文本框。另外,我想显示符合条件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();
    }
}

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

7 个答案:

答案 0 :(得分:2)

尝试

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

尝试以下代码

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

答案 3 :(得分:0)

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

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

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

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

来源msdn
进一步阅读dotnetperls

构建我为学习如何实现它而制作的演示程序。

ComboBox demo


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

答案 4 :(得分:0)

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。

答案 5 :(得分:0)

using (var con = new SqlConnection(ConStr))
{
    string query = @"UPDATE tblCompanySetup SET companyName = @companyName";

    using (var cmd = new SqlCommand(query, con))
    {
        con.Open();
        cmd.Parameters.AddWithValue("@companyName", SqlDbType.NVarChar).Value = tbCompanyName.Text;
        cmd.ExecuteNonQuery();
    }
}

答案 6 :(得分:0)

试试这个:

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