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).
Related
I have a VB6 application that has been running for quite sometime. Currently I'm trying to update one of the form that has a combobox 2.0. Because the combobox is populated with hundreds of items - I'm trying to update it so that users are able to click on a look up button next to it, where another window opens up with all the items from the combobox. User will be able to search by keyword and/or select an item and double click on it and have it appear in the combobox. The issue I'm having is with trying to pass no value or "" when CANCEL is clicked. I'm able to pass the value if I in the properties window my STYLE Is set to COMBO rather than list. However, the issue I come across is that with COMBO the value (text) in the combobox sometimes is not aligned properly. Is there a way to pass a "" value to a combobox 2.0 without changing the style to COMBO?
If they hit cancel set the ListIndex of your combo to -1 rather than setting the text property. This is the value for no item being selected.
I want to create a ComboBox having Checkboxes as children using Codename one.
I am not using the UIBuilder
For reusability I created a Container having three Checkboxes in it:
OverviewCheckBoxContainer
- Checkbox1
- Checkbox2
- Checkbox3
and this works already.
As it takes too much space on the screen, I now tried to add the CheckBoxContainer into a Combobox, like this:
ComboBox
- OverviewCheckBoxCont
-...
but it does not work, the ComboBox contains a single entry only and it's not a checkbox, but a text:
OverviewCheckBoxCont[x=...
(cannot see further on the screen)
How can I solve this issue, so there is a dropdown menu containing the three Checkboxes, that toggle onClick?
ps:
In the main form I added the CheckBoxesComboBox instead of the CheckBoxesCont:
this.add(BorderLayout.CENTER, checkBoxesComboBox)
instead of
this.add(BorderLayout.CENTER, checkBoxesCont)
1.You can use simple combobox as shown below
ArrayList al = new ArrayList();
findComboBox().setModel(new DefaultListModel(al));
2.And to add checkbox in combobox , you have to customize the combobox
3.Instead of customizing combobox, You can use button which shows and hides OverviewCheckBoxContainer which contains list of checkboxs
See this for customizing the ComboBox with the generic list cell renderer: https://www.codenameone.com/manual/components.html#_combobox
The problem with using checkboxes in a combo is that you would assume they would all appear in the combo as a set and the combo wasn't designed to do that. I would instead just use a Button and show a dialog with a set of checkboxes then set the text of the Button to match the result. You can style the button to look like a ComboBox if that is your preference.
lets say I have combobox and I have set data source (string list) to it. I don't want to include - Select - to the list. is it possible to display - Select - on dropdown without adding it to datasource. (Is it possible to display text which is not in the list?)
You can make combobox editable and readonly and then set its text.
.IsEditable = True
.IsReadOnly = True
.Text = "----Select----"
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.
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