How to Get the Selected Item of a Combo Box to a String Variable in C#

How to get the selected item of a combo box to a string variable in c#

Try this:

string selected = this.ComboBox.GetItemText(this.ComboBox.SelectedItem);
MessageBox.Show(selected);

C# Can't get combobox Selected Item string

this.name = comboBoxAlbums.GetItemText(comboBoxAlbums.SelectedItem);

Get the combobox text in C#

I just created a simple windows form, and everything worked okay for me. Here is the code.

public enum Test
{
One, Two, Three
}

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

this.comboBox1.DataSource = Enum.GetNames(typeof(Test));
}

public Test Test
{
get
{
return (Test)Enum.Parse(typeof(Test), this.comboBox1.Text);
}
set
{
this.comboBox1.Text = value.ToString();
}
}

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(this.Test.ToString());

this.Test = Test.Two;

MessageBox.Show(this.Test.ToString());
}
}

How to set the selected value of a combobox to a string value

Get rid of SelectedIndex="0" in your combo definition and try this:

 <ComboBox x:Name="cmbMyCmb" SelectedValuePath="Content" SelectionChanged="cmbMyCmb_SelectionChanged" Margin="99,104,117.4,157.8">
<ComboBoxItem Content="Please Select"/>
<ComboBoxItem Content="Item 1"/>
<ComboBoxItem Content="Item 2"/>
<ComboBoxItem Content="Item 3"/>
</ComboBox>

Code behind:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
//get item from database here
string itemFromDatabase = "Item 2";

cmbMyCmb.SelectedValue = itemFromDatabase;
}

Sample Image

Note I'm doing it in the Loaded event as a sample on how to change. If you do it in SelectionChanged event, the result is that whatever the user selects, your code behind is going to put it back to whatever value you want, which doesn't make sense.

Retrieving value from combo box selected item

comboBox was local variable in Form1 .ctor. You can't access it from another method

options:

1) access to the control by name

private void button2_Click(object sender, EventArgs e)
{
var comboBox = this.Controls["comboBox"] as ComboBox;
...
}

2) make it a private member of the form as controls normally are, if they are created in designer

ComboBox comboBox;
public Form1()
{
InitializeComponent();
// Initialize combo box
comboBox = new ComboBox() {...};
...
}

Getting the selected value from a combobox populated from a data base, for comparison, through data binding WPF/ MVVM

You can use List which contains model and bind to combobox itemsource and you can bind DisplayMemberPath to say "Name" which is the property of model and bind SelectedItem to this model .

Please see the code . I have used dummy string values. In your case this should be populated from DB as you mentioned . Note that, in the setter of SelectedEmployee i am assigning value to NumText as per your requirement . As soon as selected item changes, this will be assigned and shown in XAML textbox

In your case, you have wrongly assigned SelectedItem to list . it should be related to itemsource which you have bound . You created separate lists one for itemsource and one for selected item. And also i cannot see any changes in property setters related to INotifyPropertyChanged in your code .

XAML :

<Window x:Class="WpfApplication13.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication13"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50*"/>
<RowDefinition Height="50*"/>
</Grid.RowDefinitions>
<TextBox x:Name="text_nume" HorizontalAlignment="Center" Height="23" TextWrapping="Wrap" Text="{Binding NumeText}" VerticalAlignment="Center" Width="89" Grid.Row="0" />
<ComboBox x:Name="comboPersonal" Grid.Row="1" Height="30" Width="100" HorizontalAlignment="Center" DisplayMemberPath="Name" VerticalAlignment="Center" Margin="13,60,-62,0" ItemsSource="{Binding Ang1}" SelectedItem="{Binding SelectedEmployee}" />
</Grid>
</Window>

ViewModel:

 public class ViewModel : INotifyPropertyChanged
{

#region Constants and Enums

#endregion

#region Private and Protected Member Variables
private IList<EmployeeModel> ang1;
EmployeeModel _selectedEmployee;
private string numeText;
#endregion

#region Private and Protected Methods
private void OnPropertyChanged(string propName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
#endregion

#region Constructors
public ViewModel()
{
Ang1 = new List<EmployeeModel>();
Ang1.Add(new EmployeeModel() { Name="1"});
Ang1.Add(new EmployeeModel() { Name = "2" });
}
#endregion

#region Public Properties
public IList<EmployeeModel> Ang1
{
get
{
return ang1;
}
set
{
ang1 = value;
OnPropertyChanged(nameof(Ang1));
}
}

public EmployeeModel SelectedEmployee
{
get
{
return _selectedEmployee;
}
set
{
_selectedEmployee = value;
NumeText = value.Name;
OnPropertyChanged(nameof(SelectedEmployee));
}
}

public string NumeText
{
get
{
return this.numeText;
}
set
{

this.numeText = value;
OnPropertyChanged(nameof(NumeText));
}
}
#endregion

#region Public Methods
public event PropertyChangedEventHandler PropertyChanged;

#endregion

}

Model:

 public class EmployeeModel
{
public string Name { get; set; }
}

I used INotifyPropertyChanged for binding notification . Let me know if this helps



Related Topics



Leave a reply



Submit