This should be easy but Windows.Forms surprises me again:
var comboBox2 = new ComboBox();
comboBox2.Items.Insert(0,"Hoi");
comboBox2.Items.Insert(1,"Hoi");
comboBox2.Items.Insert(2,"Hoi");
comboBox2.SelectedIndex = 1;//I want to select the SECOND item
If I openup the combobox (by clicking on it with the mouse) the FIRST item is selected. This does not occur when the items have different texts. How can I select the second item on this combobox?
Use something like this,
comboBox2.Items.Insert(0,"Hoi");
comboBox2.Items.Insert(1,"Hui");
comboBox2.Items.Insert(2,"Hai");
and after this try giving
comboBox2.SelectedIndex = 1;
and also if you want select an item, always the index will starts from 0.
If you have a proper datasource set the following properties:
1. DataSource2. DisplayMember and 3. ValueMember
If you dont have the data source, try setting the following properties:
1. SelectedIndex and
2. Text
Related
I am trying to set a ComboBox's text conditionally - that is, if the text I am setting it to exists in the box's Items, then set it to that. Otherwise, leave it blank.
e.x.
ComboBox's Items:
Green
Blue
Pink
Red
For ComboBox1 I am trying to set to "Red". Because that string exists in the Items, the text property is set to "Red". For ComboBox2 I am trying to set to "Yellow", which doesn't exist, so I want that text empty (but the previous items still are there).
All the comboboxes will have the same Items. However, there are lots of combo's so I'm trying to avoid looping through each boxes owns items and compare each one. In C# you could just do something like:
ComboBox1.ItemIndex := 'My Text';
Which doesn't compile here.
You could use either
ComboBox1.ItemIndex := ComboBox1.Items.IndexOf('My Text');
or
ComboBox1.Text := 'My Text';
The second version requires that you have ComboBox's Style set to csDropDownList (otherwise the string would be shown in the combobox even if it is not in the list).
I have a combo box that I would like it to show a
category name ("Cat1" , "Cat2"...)
But when clicked or just displayed I would like to get the ID of this category. Any idea how to do this in Foxpro?
To populate your combobox:
Thisform.Combo1.AddItem("Cat1")
Thisform.Combo1.AddItem("Cat2")
You may use rowsource to add the entire item list into the combobox.
Get the selected value of combobox:
Thisform.Combo1.DisplayValue
Please refer to this article
Create a cursor that contains what you want to display in the first column and the ID in the second. Then, set up the combo like this:
RowSourceType = 2-Alias
RowSource = <your cursor name>
BoundColumn = 2
BoundTo = .T. && so you can have a numeric controlsource
Tamar
I am having a Datagrid that gets populated with values from a DataTable. In my program i have four buttons: Goto First, Last, Next and Previous, as the name name indicates i have to select the rows based on the selection made using these buttons. Everything seems well if i use the below code to get the row (for example first row).
DataGridRow row =(DataGridRow)userControl.m_DataGrid.ItemContainerGenerator.ContainerFromIndex(0);
row.IsSelected = true;
But the code throws null value when there is more rows than the height of the Datagrid(When scrollbar comes into picture).
Please help me out of this issue. I think this is because of the view problem.
Due to virtualization the containers are only created when the object is in view, so you could first scroll the item into view using the respective method, wait for the creation of the container and then select it.
As this is rather messy i would suggest binding the IsSelected to a property on your item using a style for DataGridRow (set it as ItemContainerStyle). Then you can just set the property to true and scroll the item into view if need be.
I want to retrieve data from 2 columns of my database table and bind those 2 columns to the combobox item index and item value properties respectively. I retrieve my data from the samples table of my database with this anonymous query:
var result = from obj in context.Samples
select new { obj.ID , obj.Name };
I want to set the default index value of each item in my combobox to obj.ID and the value of each respective combobox item to obj.Name so that rather than the items in my combo box having default values starting from 0, 1, 2.... their index values will have the value of obj.ID returned by my LINQ query and the actual value of the item will be obj.Name.
Sorry if this is a silly/amateurish question but I've spent a few hours trying to fix it and had no luck. Thanks in advance.
The SelectedIndex property always goes from -1 (no item is selected) to count-1, you can't change that. If you want to store the ID and Name for each item in the ComboBox, you can do that:
Create non-anonymous type Sample that contains those two properties. Then assign (or bind) collection of Samples to the ComboBox's ItemSource and set DisplayMemberPath toName. Then you can access the ID of the selected item using ((Sample)yourComboBox.SelectedItem).ID.
I have a ComboBox and its items source is an SQL table.
I would like that the user will choose the value he likes and if the value is not in the list, he would be able to type the value into the ComboBox. Is it possible?
Thanks.
You need to set the IsEditable property of the ComboBox to True. This will allow you to edit the contents as well as select any of the populated items in the combo box.