I have a Combobox with several items (C# Labels because I want to change individual text colors). Within callbacks, when editing some textboxes, I change the color and/or text of the items. When I click on the combobox I see that the items in the list have the correct color/text. However, the color/text changes of the selected item are not directly reflected in the shown combobox text. How can I achieve that?
I tried to set the Text property of the combobox itself: no effect. Also setting the selected item to an empty Label and then set it back to the correct Label has no effect. If I set SelectedIndex to -1 and then back to the correct selected index it works and the shown text is updated, but this triggers the SelectionChanged callback which I do not want. I could first detach the SelectionChanged callback from the combobox and then attach it again, but this is in my opinion very ugly programming.
Maybe I am missing something simple...
Edit
I tried binding, following the suggestion of SLak:
List<Label> labels;
MyComboBox.ItemsSource = labels;
The result is still the same. Suppose index 0 is selected. When I change the corresponding Label:
labels[0].Contents = "new content";
then it is not reflected in the selected text of the combobox. When I click the combobox I can see the new text in the unfolded list, but only when I change the selection and then go back to index 0 the new text is shown by the combobox as the selected item. That synchronization should be autmatically.
Related
I created a project that collects Personal Informations. I added a combobox to get title of Personal. Combobx items which is in ENUM, added at load. I save informations at listview, everythings Runs Perfectly i have double click event for listview. It returns all information to the their first position like Textboxes. But Combobox doesnt get its value to back. Can you show me how i can do ?
Enums Added ComboBox
cbUnvan.Items.AddRange(Enum.GetNames(typeof(Unvan)));
if Combobox item (which is Enum) selected, goes to personel.Unvan Property. (Personel is Class)
personel.Unvan = (Unvan)Enum.Parse(typeof(Unvan), cbUnvan.SelectedItem.ToString());
When i double click to listview item informations goes to their places. example is at below
txtTC.Text = lvTablo.SelectedItems[0].SubItems[0].Text;
dtpIseGirisTarihi.Value = Convert.ToDateTime(lvTablo.SelectedItems[0].SubItems[3].Text);
MY PROBLEM IS HERE -- But Combobox doesnt do same action with this code below
cbUnvan.SelectedItem = (Unvan)Enum.Parse(typeof(Unvan), lvTablo.SelectedItems[0].SubItems[8].Text);
In ListView there are 3 option of SelectionMode
1.Single - only one item can be selected.
2.Multiple - you can select muliple items, one after the other.
3.Extended - You can select multiple items and used Ctrl or Shift key.
I'm need to select some items in ListView as text in TextBox.
i.e. press with the left button of mouse, until the mouse is up.
and mark all between items as Selected.
How can I make it?
Thanks
1.Single: SelectionMode="Single"?
2 Mutiple : i think use binding
enter link description here
3
enter link description here
Or You try this line SelectionMode="Muti..."
Firstly, the porpose of this problem is to display listView of TextBlocks for allow use ItemsSource of ListView for display text for several reasone.
behind each textBlock that contains a word, there are in the ViewModel class named Word. that contains the text of the word and property of IsSelected.
I solve this problem, by adding 3 EventSetter event to the ListViewItem,
1.PreviewMouseLeftDown
2.MouseEnter
3.PreviewMouseLeftUp
and adding a flag of IsInSelection, and two object Word that present the control in the view,1.firstSelectionWord, 2.lastSelectionWord.
and, when the first event raise, i update the current control to be Selected.
and set a flag IsInSelection to true. and set firstSelectionWord = lastSelectionWord = current word pressed.
in the MouseEnter event i checked if IsInSelection is true, and them mark also the current control to Selected=true. set the lastSelectionWord = current word pressed.
and call a method that mark all the Word between them as selected.
in the PreviewMouseLeftUp function, i set the IsInSelection = false.
I'm coding a combobox in C# and for some reason the items in the drop down don't have text. When I have selected an item, it is shown in the combo box text field (the drop down list is always blank whenever I click the drop down button). The datasource seems bound properly because the proper values are being returned when I select items, and the size of the drop down list will change depending on how many items the datasource has. Everything looks fine except for the fact that it seems like my drop down is populated with a bunch of empty strings, which it clearly isn't since as soon as an item is selected the proper text will display.
This is the relevant code:
if (list.Count > 0)
{
cboCustomers.DisplayMember = "Name";
cboCustomers.DataSource = list;
cboCustomers.ValueMember = "ID";
cboCustomers.SelectedIndex = 0;
}
I have looked for an answer to this but can't find it anywhere...I'm sure it's something really simple, but I can't figure it out. The closest problem I found had an answer suggested to set the display member before the data source, which clearly didn't work.
The list is populated from a database query. This will run on keyUp, the idea is that the list is populated as the person is typing based on the info given. So if I wrote 'S' I'd get a combobox with a dropdown that had all the clients starting with 'S'.
Given you don't have any anomalies in your binding, you are probably being affected by DrawMode property of your ComboBox, which may be set to OwnerDrawFixed or OwnerDrawVariable. Set it to Normal and things should get better.
as soon as an item is selected the proper text will display.
A foreground color the same as the background color will produce the same results you are seeing.
I use VS2008 C# + Windows Forms. I can't understand why comboBox does not behave the way it should.
In Design mode, I added a comboBox to my form, and edit Items to add "A" and "B". Double-clicking brings me to SelectedIndexChanged event, which I edit to display the selected text with MessageBox.
private void comboBoxImageSet_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(comboBoxImageSet.SelectedText);
}
When I run, and select "A" or "B" in the comboBox, the MessageBox appears, but nothing is written.
Why?
Thanks.
Here the differences between the selection properties of a ComboBox control.
SelectedIndex;
SelectedItem;
SelectedText;
SelectedValue.
The SelectedIndex property :
Gets or sets the index specifying the currently selected item.
Simply indicates the index of the selected item in the selection list. (Information provided for your kind information only. =))
The SelectedItem property :
Gets or sets currently selected item in the ComboBox.
The SelectedItem represents the element that is currently selected as per the ListControl of the ComboBox. That is why this is what you want to use, to answer your question.
The SelectedText property :
Gets or sets the text that is selected in the editable portion of a ComboBox.
That is, when you edit the TextBox portion of the ComboBox, the text that might be selected when you enter for edit, or any other type of text selection. This indeed does include any selection made through the ListControl portion of the ComboBox. For instance, if your ComboBox.DropDownStyle property is set to ComboBoxStyle.DropDownList, then you will never be able to select any text in the editable portion of the ComboBox. Despite, you're able to select another item within the its list. That is why it is not the right property to use to serve your purpose.
The SelectedValue property :
Gets or sets the value of the member property specified by the ValueMember property.
Only used when using DataBinding, in conjunction with the DisplayMember property. For instance, when you want to display the name of a customer, and select him by his database Id, then the DisplayMember should display the customer's name, and the ValueMember the Id. This way, when you select one customer, the SelectedValue changes and raises the SelectedValueChanged event inherited from the ListControl. (Information provided for your kind information only. =))
The SelectedText property returns the text that is marked in the combobox, not the selected item. If the combobox is editable you can mark a part of the text and the SelectedText property will return the marked text. Look here.
What you are interested in is the SelectedItem property or the SelectedValue property.
ComboBox.SelectedText
A string that represents the currently
selected text in the combo box. If
DropDownStyle is set to DropDownList,
the return value is an empty string
("").
Use SelectedItem instead of SelectedText
SelectedText:
Gets or sets the text that is selected
in the editable portion of a ComboBox.
That is, it gets the text that is currently marked.
You want to use SelectedItem.ToString().
How can I prevent bindingSource Current item from changing?
(there is no changing event with cancel argument...)
This is scenario:
I have a dataGridView, and text-boxes on the same form.
I am using text-boxes to change values in the datasource (with standard databinding)
Bindings are written manually (After save button is clicked)
When user selects another row using DataGridView, bindingSource.Current propery is changed, and text boxes are updated with values from selected row. Changes that user entered are lost.
Is there any way to prevent this problem?
Can I prevent bindingSource.Current property from changing?
Is there any better option to prevent this behaviour?
(disabling dataGridView is unfortinutelly not an option)
It sounds like you don't want to not change bindingSource.Current, but rather you want to save the contents of the text boxes before you change the current row? If you've bound a collection to the bindingSource, then don't the text boxes refer to properties in the current element in that collection?
I'm not really sure what you're trying to do, but a shot in the dark might be to bind the same DataSource to two different BindingSource objects, something like this:
gridBindingSource.DataSource = theDataSource;
textBoxBindingSource.DataSource = theDataSource;
myDataGrid.DataSource = gridBindingSource;
firstNameTextBox.Bindings.Add (new Binding ("Text", textBoxBindingSource, "FirstName"));
but this would be weird, because if theDataSource is appropriate for a grid control, then it's a collection of things that have a FirstName property. Maybe if you were more specific in your question.
ETA: If you want to save the text box contents to the current row, call ValidateChildren () on the container before the bindingSource.Current property changes.
I have a somewhat similar framework with both grid and textboxes on same form. When user clicks the EDIT button (or Add), I just disable the gridview control itself...
MyDataGrid.Enabled = false;
continue editing..
Then in the SAVE, if all is ok,
MyDataGrid.Enabled = true;