Get the combobox text in C# - c#

I filled up a combobox with the values from an Enum.
Now a combobox is text right? So I'm using a getter and a setter. I'm having problems reading the text.
Here's the code:
public BookType type
{
get
{
return (BookType)Enum.Parse(typeof(BookType), this.typeComboBox.Text);
}
set
{
this.typeComboBox.Text = value.ToString();
}
}
For some reason, this.typeComboBox.Text always returns an empty string when I select an item on the combobox.
Does someone see what I'm doing wrong?
EDIT: I have come to the conclusion that the problem lies in timing.
The point in time at which I summon the text is indeed after I changed the combobox, but still before that value is parsed as a value.
Problem fixed in a different way now, thanks for all the ideas.

string selectedText = this.ComboBox.GetItemText(this.ComboBox.SelectedItem);
The GetItemText method analyzes the item and returns the text of the bound to that item.

Set the DropDownStyle of the ComboBox to DropDownList. This will ensure that only the elements already in the list can be selected (no need to check that the text actually is a valid value).
Then if you use Enum.GetValues(typeof(BookType)) to fill the combobox then typeComboBox.SelectedItem property will be a value of BookType. So you can use this in the property getter and setter.
So to summarize. You don't have to bind the combobox to a list of text values as long as you use the DropDownList style. Use the SelectedItem property to get an item of the wanted type instead of checking the Text property.
Edit: You may have to check the SelectedItem property for null

The combobox starts at index -1, which has no text, thus an empty string: ""
I then change the index to a BookType that I need and then I get the wrong output...

Have you tried using this.typeComboBox.SelectedText instead of typeComboBox.Text ?

this.typeComboBox.SelectedItem.ToString()

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

Related

Set a default value in a combobox from enum

I have a combobox that I have some values in from an enum.
I want a default value to show on the combobox when the program start.. but I cant get it to work..
This is what I have tried so far in my MainForm:
cmbPrio.Items.AddRange(Enum.GetNames(typeof(PriorityType.Prioritytypes)));
cmbPrio.SelectedValue = PriorityType.Prioritytypes.Normal;
this is the class with the enum:
class PriorityType
{
public enum Prioritytypes
{
Very_Important,
Important,
Normal,
Less_Importan
}
}
You are populating the names into the combo, which is just a collection of string, which means that values you use to set must also be strings.
cmbPrio.SelectedValue = Enum.GetName(typeof(PriorityType.Prioritytypes), PriorityType.Prioritytypes.Normal);
Why don't you simply set the Index? :
cmbPrio.SelectedIndex = 0;
Another alternative would be to set the ComboBox items to:
cmbPrio.Items.AddRange(Enum.GetValues(typeof(PriorityType.PriorityTypes))
.Cast<PriorityType.PriorityTypes>());
cmbPrio.SelectedValue = PriorityType.PriorityTypes.Normal;
This allows you to treat the selected values as actual PriorityType.PriorityTypes as opposed to strings.

Binding dictionary to combobox in C#

I have this in Load of my form which contains combobox (cmbTip)
EventTypeRepository tip = new EventTypeRepository();
cmbTip.DataSource = new BindingSource(tip.FindAll(), null);
cmbTip.DisplayMember = "Value";
cmbTip.ValueMember = "Key";
(FindAll() is a method in EventTypeRepository which returns Dictionary(string, EventType>))
For some reason this displays MyProject.Model.EventType as all combobox items. I even added:
public string toString()
{
return _name + "(" + _id + ")";
}
in my EventType class, but it stills displays names as MyProject.Model.EventType (there are as many items as there are event types, so I think it works fine expect for displaying names). I have no idea how to fix this...
You should override ToString method (keep in mind C# is case-sensitive language):
public override string ToString()
{
return String.Format("{0}({1})", _name, _id);
}
Also it's better to set DisplayMember and ValueMember before you set DataSource.
If you ever find your self in a situation where you can't override ToString(), another option is to use the Format event built in to the combo box. First you must set FormattingEnabled to true on the combo box, then subscribe to the Format event and use code similar to the following.
private void cmbTip_Format(Object sender, ListControlConvertEventArgs e)
{
var item = (EventType) e.ListItem;
e.Value = String.Format("{0}({1})", Name, Id);
}
This assumes that _name and _id have corresponding public properties Name and Id.

combobox item selected To be fixed

I had a databound combobox in my windows form I populate it by a function deptload() IN FORM LOAD
public void DeptcomboLoad()
{
DataTable dt = depttrans.getDeptName();
Cmb_Department.DataSource = dt;
Cmb_Department.DisplayMember = "DepartmentName"; //CHAR
Cmb_Department.ValueMember = "DepartmentPK"; //INT
}
Now when an employee of a department (say accounts DepartmentName="Accounts " , DepartmentPK=23 ) login I want the ComboBox text to be selected as "acounts "
and when I go to get the selected value of the ComboBox I should get 23
I tried
Cmb_Department.selectedtext="Accounts"
Cmb_Department.Text="Accounts"
but its not giving the selected value
Can anyone give a suggestion
Instead of trying to put a value INTO the combobox, try to GET the SelectedItem like this:
string txt= Cmb_Department.SelectedItem.Text
or just:
string txt= Cmb_Department.SelectedText
To change selected value of the combobox you can use
SelectedItem property or SelectedIndex.
Index must be exact number in your data sourse, and Item must be exact object from datasource
You can get it to select the right item by issuing something like this:
Cmb_Department.SelectedValue = 23;
Where 23 comes from some other variable, maybe on another object, maybe from a local variable, whatever works in your case.
Now, to get the selected value you can use this statement:
var val = Cmb_Department.SelectedValue;
To get the selected text (which would be the text associated with the value):
var text = ((DataRow)Cmb_Department.SelectedItem)["DepartmentName"];
The reason I'm prescribing the aforementioned is because the SelectedText property is volatile, and the Text property doesn't always work based on how the DropDownStyle is set.
However, some would probably argue to get the same as the aforementioned you could issue this statement:
var text = Cmb_Department.Text;

How to store the actual underlying DB value in a combobox while displaying a more user-friendly value?

I need to display values like "Surreptitiously" and "Discreetly" in a comboBox but thereafter be able to set the comboboxes' SelectedItem based on the underlying DB values for those words (e.g., "S" and "D").
I reckon I can use the comboBoxes' DisplayMember and ValueMember properties for this somehow, but can I subsequently do something analagous to the following with the actual (valuemember) values:
comboBoxAdverbs.SelectedIndex = comboBoxAdverbs.Items.IndexOf(weirdAdverbs[CURRENT_ADVERB]);
As "weirdAdverbs[CURRENT_ADVERB]" contains the values like "S" and "D" it, of course, doesn't find and set the SelectedIndex when the comboBox contains the values "Surreptitiously" and "Discreetly"
If I set the combobox Item Tag value to "S" and "D" (assuming that's possible), I can loop through those values, but I'm hoping there's a one-line way of doing it similar to the "IndexOf()" above.
I use a template class for this and it comes in pretty darn handy. The combo box will show whatever text you want and you can store a value with it.
public class cboItem<T>
{
public cboItem(string name, T value)
{
this.Name = name;
this.Value = value;
}
public string Name { get; set; }
public T Value { get; set; }
public override string ToString()
{
return Name == null ? "" : Name;
}
}
Combo box items can be anything, including classes/structs. By default it will use the ToString() implementation to display items, but if you populate a set of objects you can use DisplayMember and ValueMember to great effect.
As a simple example to give you some ideas we'll bind the combo box to a set of KeyValuePair instances for your weird verb codes and their descriptive names. Alternatively you can use linq to compose anonymous types, or create your own suitable classes/structs.
private void populateCombo()
{
comboBoxAdverbs.Items.Clear();
comboBoxAdverbs.Items.Add( new Tuple<string, string>( "S", "Surreptitiously" ) );
comboBoxAdverbs.Items.Add( new Tuple<string, string>( "D", "Discreetly" ) );
comboBoxAdverbs.DisplayMember = "Item2";
}
Then in your code where you want to select an item matching a provided code: (I.e. "D")
var item = comboBoxAdverbs.Items
.OfType<Tuple<string,string>>()
.FirstOrDefault(i => string.Compare(i.Item1, textBox1.Text, true) == 0);
if (item != null)
comboBoxAdverbs.SelectedItem = item;
This attempts to find the matching item by comparing the key against whatever input (in this case a textbox value) and if it finds a match, sets the SelectedItem to tell the combo box to select it.
** Edit: Whups, had originally use KeyValuePair which I didn't realize was a struct so no Null check-ability. Changed to Tuple (Essentially Pair)
What I found that works for me is to store the selectedindex value, after it's set, into the combobox's Tag property. Then, if the user attempts to change the selectedIndex when it is supposed to be in a "readonly" state, simply change it back to the selectedIndex value stored in the Tag property:
comboBoxPlatypusId.SelectedIndex = comboBoxPlatypusId.Items.IndexOf(DuckbillVals[Duckbill_PlatypusID]);
comboBoxPlatypusId.Tag = comboBoxPlatypusId.SelectedIndex;
...
private void comboBoxFunnyMammals_SelectedValueChanged(object sender, EventArgs e) {
var cb = sender as ComboBox;
if (cb != null)
{
int validSelection = Convert.ToInt32(cb.Tag);
if (cb.SelectedIndex != validSelection) {
cb.SelectedIndex = validSelection;
}
}
}

How can I add an item to a ListBox in C# and WinForms?

I'm having trouble figuring out how to add items to a ListBox in WinForms.
I have tried:
list.DisplayMember = "clan";
list.ValueMember = sifOsoba;
How can I add ValueMember to the list with an int value and some text for the DisplayMember?
list.Items.add(?)
Btw. I can't use ListBoxItem for any reasons.
ListBoxItem is a WPF class, NOT a WinForms class.
For WPF, use ListBoxItem.
For WinForms, the item is a Object type, so use one of these:
1. Provide your own ToString() method for the Object type.
2. Use databinding with DisplayMemeber and ValueMember (see Kelsey's answer)
list.Items.add(new ListBoxItem("name", "value"));
The internal (default) data structure of the ListBox is the ListBoxItem.
In WinForms, ValueMember and DisplayMember are used when data-binding the list. If you're not data-binding, then you can add any arbitrary object as a ListItem.
The catch to that is that, in order to display the item, ToString() will be called on it. Thus, it is highly recommended that you only add objects to the ListBox where calling ToString() will result in meaningful output.
You might want to checkout this SO question:
C# - WinForms - What is the proper way to load up a ListBox?
DisplayMember and ValueMember are mostly only useful if you're databinding to objects that have those properties defined. You would then need to add an instance of that object.
e.g.:
public class MyObject
{
public string clan { get; set; }
public int sifOsoba { get; set; }
public MyObject(string aClan, int aSif0soba)
{
this.clan = aClan;
this.sif0soba = aSif0soba;
}
public override string ToString() { return this.clan; }
}
....
list.Items.Add(new MyObject("hello", 5));
If you're binding it manually then you can use the example provided by goggles
The way I do this - using the format Event
MyClass c = new MyClass();
listBox1.Items.Add(c);
private void listBox1_Format(object sender, ListControlConvertEventArgs e)
{
if(e.ListItem is MyClass)
{
e.Value = ((MyClass)e.ListItem).ToString();
}
else
{
e.Value = "Unknown item added";
}
}
e.Value being the Display Text
Then you can attempt to cast the SelectedItem to MyClass to get access to anything you had in there.
Also note, you can use anything (that inherits from object anyway(which is pretty much everything)) in the Items Collection.
If you just want to add a string to it, the simple answer is:
ListBox.Items.Add("some text");
You have to create an item of type ListBoxItem and add that to the Items collection:
list.Items.add( new ListBoxItem("clan", "sifOsoba"));
If you are adding integers, as you say in your question, this will add 50 (from 1 to 50):
for (int x = 1; x <= 50; x++)
{
list.Items.Add(x);
}
You do not need to set DisplayMember and ValueMember unless you are adding objects that have specific properties that you want to display to the user. In your example:
listbox1.Items.Add(new { clan = "Foo", sifOsoba = 1234 });

Categories

Resources