Right way to set selected text of combobox in winforms? - c#

I have a form that takes in an object in it's constructor and populates controls on the form from properties in that object. I am having an issue where I can't set a ComboBox's SelectedText property, or at least it isn't working how I expect it to.
public Form(ValueHoldingObject obj)
{
// yeah I know this is not a very clean way to populate the combobox, the issue
// isn't limited to the combobox so I don't think this is relevant
List<int> items = Repo.GetAllItems().Reverse();
foreach (int id in checkInPrizeIds.Take(100))
// Insert at beginning to put more recently used items at the top
combobox.Items.Insert(0, id);
combobox.DropDownHeight = 200;
combobox.SelectedText = obj.StringProperty;
}
When I am testing this form the text of the combobox isn't being populated. If I add a breakpoint on the line where I assign the text it DOES get assigned, so some event is firing (multiple focus change events probably) and making it work the way I want. Obviously I can't use a breakpoint as a fix in production code. Am I assigning this value incorrectly? Should I be using a different method to populate the values?
Further testing has reviled that it isn't just the combobox, all of my controls are only being populated correctly if I have the breakpoint.

In the constructor, you need to set the selected item, for example:
foreach ( var item in combobox.Items )
if ( (string)item == obj.StringProperty )
combobox.SelectedItem = item;
Or:
foreach ( var item in combobox.Items )
if ( (int)item == Convert.ToInt32(obj.StringProperty) )
combobox.SelectedItem = item;
It's confusing but despite its name, the property SelectedText is not really the selected item... because combo box items are objects and not strings: texts shown are a representation of the item objects using ToString().
Therefore setting the selected text will not guarantee to select an item and we can prefer setting the SelectedItem.
In addition to these considerations, you set the selected text property in the constructor after populating the combo box and that can cause problems because it is before the form and the control are drawn or something like that... that is to say perhaps before the ToString() methods are called on items to prepare the visual cache, so setting the selected text can't get a match with the list.
Setting the selected text selects an existing item if done in the form load or shown events.
private void Form_Load(object sender, EventArgs e)
{
combobox.SelectedText = obj.StringProperty;
}
ComboBox.SelectedText doesn't give me the SelectedText
ComboBox.SelectedText Property

Related

WPF drop-down list for auto generated column in datagrid

How to make a drop-down list when you click on an element of a specific table column in which you can select an element for this cell? Column is auto generated.
A Combobox in xaml/wpf is used like this:
<ComboBox x:Name="some Name" SelectionChanged="comboboxChanged">
<ComboBoxItem>The Content of your Combobox</ComboBoxItem>
</Combobox>
ComboBoxItems are essentially the dropdown part. You can add as many as you want.
In your back end (c#) you can get the selected Value as soon as the "SelectionChanged"-event is triggered. The code for getting the selected Value can be done multiple ways. Example:
private void comboboxChanged(object sender, SelectionChangedEventArgs e){
string comboboxvalue = comboboxname.Text;
//Then set associated textblock or label
labelname.Content = comboboxvalue;
}
The code above would be static though. Dynamically generating these elements could look like this for instance.
When auto-generating, using an inline function for the event is easy.
for (int i = 0; i < 10; i++){
ComboBox comboboxname = new ComboBox();
comboboxname.SelectionChanged += (ss,ee) { string comboBoxValue = comboboxname.Text; labelname.Content = comboBoxValue;}
}
Labelname being the name of the Label you want to set. In that loop you will need to implement a way of giving each box a unique name and getting the name of the associated label in there as well. That you will have to figure out on your own as i do not know how and what exactly is generated and what is static.
You will also need to add your dynamically created combobox to your listpanel or grid or whatever you are using. This works like this:
listpanelname.Children.Add(comboboxname);
Just add that to the "for" loop.

How to reset the item select on ListView winform

I have a small project where I get some values from a txt and put inside of a ListView. I need to get the selected value when I click in some item, the first item that I ever select, works fine, but if I try to select again, I get an exception.
This is what I did..
Json = new StreamReader(openDialog.FileName).ReadToEnd();
var ParsedValue = JsonValue.Parse(Json);
Parsed = JsonConvert.DeserializeObject<List<Model>>(ParsedValue.ToString());
foreach (var item in Parsed)
{
var rows = new string[] { item.car, Convert.ToString(item.age )};
var items = new ListViewItem(rows)
{
Tag = item
};
ListViewCars.Items.Add(items);
}
The List view is Filled.
And to get the Item selected from the list :
private void cartsList_SelectedIndexChanged(object sender, EventArgs e)
{
ItemSelected = (Model)ListViewCars.SelectedItems[0].Tag;
}
I can only get the value that I select first when the program runs.
The exception :
System.ArgumentOutOfRangeException: 'InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index'
Check out the documentation on ListView.SelectedIndexChanged. Specifically, look at the Remarks section, which reads:
The SelectedIndices collection changes whenever the Selected property of a ListViewItem changes. The property change can occur programmatically or when the user selects an item or clears the selection of an item. When the user selects an item without pressing CTRL to perform a multiple selection, the control first clears the previous selection. In this case, this event occurs one time for each item that was previously selected and one time for the newly selected item.
I added the emphasis. This means that when you select the second item, the currently selected item is unselected and SelectedIndexChanged is triggered before the new item is selected. So when you try to get the first selected item with ListViewCars.SelectedItems[0].Tag you get that ArgumentOutOfRangeException because there are no selected items.
You need to add a check to the top of your event handler to make sure that there is at least one selected item before accessing SelectedItems[0].

Binding TextBox Text value to ComboBox.SelectedItem, and list from selected item of a ComboBox to another ComboBox

So I have a class "Worker" and a BindingList which is bound to a ComboBox.
workers = new BindingList<Worker>();
//// .. An "Add" button is somewhere there to add "Worker" objects to the list
//ComboBox Workers List
cbWorkersList.DataSource = workers;
cbWorkersList.DisplayMember = "Name";
I want to bind the "Text" field in a TextBox to the "Name" property of the selected "Worker" object, in the ComboBox.
Now I've done it by using the "cbWorkersList_SelectedIndexChanged" event handler, manually modifying txtWorkerName.Text property:
private void cbWorkersList_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbWorkersList.SelectedItem != null)
{
txtWorkerName.Text = workers[workers.IndexOf((Worker)cbWorkersList.SelectedItem)].Name;
}
}
I tried to bound the TextBox in this way:
InitializeComponent();
txtWorkerName.DataBindings.Add("Text", cbWorkersList, "Text");
But it is only updating the TextBox if I click on other item on the list, if I use arrow keys to change the selection, the TextBox text field is not changing.
Is there a way to truly bind them so that if a ComboBox item is selected, the text field will change accordingly?
While on this topic, I also have other 2 ComboBoxes, and I would like to bind 1 to a list which is inside the others' ComboBox object.
What I want is a solution with the following logic:
cbPositions.Items = ((<Shift>)cbShifts.SelcetedItem).PositionsList
when
cbPositions is a ComboBox which should be populated with "Position" objects.
And cbShifts is a ComboBox which is populated with "Shift" objects, where each "Shift" contains a list of Positions.
How can I bind them so every time I choose an other Shift from the list, the Positions ComboBox will be populated with the Position objects from the Shift.PositionList?
And also, I would like to know if it's possible to bind the "Items" of a ComboBox to a List (my workers list in this example), not with code, but in the designer(Application Settings or DataBindings)?

problem with drop down list

I have a drop down list control populated with items and some code to take the currently selected item value. The problem is I only get the value of the first item in the list regardless of what item is actually selected.
Here is my code to populate the drop down:
protected void displayCreateCategories()
{
StoreDataContext db = new StoreDataContext();
var a = from c in db.Categories
orderby c.Name
select new{catName= c.Name,
catId=c.CategoryID};
ddlCategory.DataSource = a;
ddlCategory.DataTextField = "catName";
ddlCategory.DataValueField = "catId";
ddlCategory.DataBind();
}
To get the value of the currently selected item which in my case is always of type integer I do label1.text=Convert.toInt32(ddlCategory.SelectedValue);
I get the selected value, but it is always for the 1st item in the list. I'm pulling my hair out over this. :(
I suspect you're running the list loading code every time the page loads, which is destroying the list, repopulating the list, and auto-selecting the first item before your selection retrieval code gets run.
Use this construction in Page_Load:
if (!IsPostBack)
{
// Initial control population goes here
}
Data binding will reset the control's selected value so make sure you retrieve the selected value before data binding on postback.

Working with listbox in winforms (c#)

How can i add 10 items to listbox dynamically to a listbox and after that I want to show the selected item value in click event of list box.
I tried like this
for(int i=1;i<10 ;i++)
{
mylistbox.Items.Add(i.ToString());
}
in click event handler
MessageBox.Show(mylistbox.SelectedValue.ToString());
it is showing error.
Whats the wrong with this?
Try using the SelectedItem property instead.
SelectedValue only works when you fill the ListBox with objects and have a ValueMember assigned.
Here is a minimal example:
var mylistbox = new ListBox {Dock = DockStyle.Fill};
mylistbox.Click += (sender, e) =>
MessageBox.Show(mylistbox.SelectedItem.ToString());
for (int i = 1; i < 10; i++)
{
mylistbox.Items.Add(i.ToString());
}
new Form {Controls = {mylistbox}}.ShowDialog();
Use the following code on the click handler
MessageBox.Show(mylistbox.Text.ToString()); //This will show the selected item as your requirement.
replace the .SelectedValue with .Text
Dmitriy has it exactly.
A good way to check what is happening when you're debugging is to highlight 'mylistbox.SelectedValue' and right-click, then select 'Add Watch'. You can then track the value of that property in the Watch window.
You can do this with any variable, and any time it shows null and you're trying to use that value you know it will throw a Null Reference Exception.
It's also good for picking up letters in a string you're trying to convert to an integer, and other similar "d'oh!" moments.

Categories

Resources