ComboBox got new SelectedValue on SelectionChangeCommitted but doesn't got new SelectedText - c#

I have this code
private void FrmNovedadMedidas_SelectionChangeCommitted(object sender, EventArgs e)
{
ComboBox c = (ComboBox)sender;
CargarMedidasPorIdTipoMedida(Convert.ToInt16(c.SelectedValue));
this.txtBoxNombreTipoMedida.Text = c.SelectedText;
}
in c.SelectedValue got the new value of the selection (the one that the user has selected in the Combo).
But in c.SelectedText I got the old value of the ComboBox (I mean, the one that was before the user change the selection).
Is there any property that can give me the new Selected Text?
I want to avoid to search in the DataSet binded to the ComboBox everytime.
I've read this but doesn't work, I don't have CommitEdit() in ComboBox
edit:
c.Text also gives me the old one

I seem to remember this situation having to do with the DropDownStyle of the ComboBox.
Can you please try different styles and see if the Text property is set to the new value inside SelectionChangeCommited ?
As per your comment, it seems using DropDownList style solves the issue.
Cheers

I found something.
c.GetItemText(c.SelectedItem)
Is there a directly properties, post it please.
Thanks for readme anyway.

Try the SelectedIndexChanged event on the ComboBox versus the SelectionChangeCommited event. Then use c.Text to get the value the user just selected.

c.SelectedValue() returns null for me.
c.GetItemText(c.SelectedItem) works for me though. Changing the dropdownstyle wasn't an option.

Related

Dropdown(list) ComboBox control field options C#

I use a ComboBox DropDown to show a number of items that can be selected. I also use a default text "Select item". It works perfectly apart from the fact that once I picked an item from the list, I can select the text and remove it (using the backspace) so that the area for the selected item is blank (see image).
I would like to prevent this or, in case this is not possible, to have the default text shown again. The latter doesn't work so far, so I tried the DropDownList but then the background of the entire list becomes grey. If I then select OwnerDrawFixed or OwnerDrawVariable in DrawMode, the background turns white again but also the font color becomes white (instead of black).
I found that I have to use DrawItem to change the layout but even that doesn't work. I simply want the same style as I indicated for DropDown. Does anyone know how to do this or where to find a similar question that has already been answered?
How about something like this?
You could also try setting the DropDownStyle to DropDownList and FlatStyle to Flat. This gives you a slightly different result.
private void InitialiseCombo(ComboBox combo)
{
combo.Items.Clear();
combo.Items.AddRange(new object[] { 3391200121, 3391200122 });
combo.DropDownStyle = ComboBoxStyle.DropDown;
combo.Text = "Select Item";
combo.KeyPress += combo_KeyPress;
}
void combo_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
The suggested solution did not do the trick but I found a workaround to the initial problem, which is the manual deletion of the ComboBox Text, so I'll just put it here in case someone else has the same problem. When using
MyComboBox_TextChanged(object sender, EventArgs e)
I can set a restriction myself: if the text is blank, it should show the default text. If the typed text is equal to one of the item in the ComboBox, it should set the selectedItem equal to the typed text.
I also included GotFocus and Leave EventHandlers.

ComboBox showing System.Data.DataRowView after changing selection

I have been looking at all the other questions similar to this one and they just don't seem to help me with my particular problem.
I am using a Combobox with the following properties:
The purpose of the Combobox is simple, it is taking all the values of an unique column in a table, and present them as options. The column's name is "nim".
Upon initialization, the combobox loaded just fine:
The problem occurs after I changed the selected item to the 2nd one in the list, and tried changing it again:
When I tried selecting System.Data.DataRowView, this error appeared:
I have been playing around with the code to no avails. I didn't write any code concerning the combobox. I just assign the DataSource, DisplayMember, and ValueMember from the properties window manually.
The only code concerning combobox is these:
private void comboNIM_SelectedIndexChanged(object sender, EventArgs e)
{
//selectedNIM = ((DataRowView)comboNIM.SelectedItem).Row["nim"] as String;
selectedNIM = comboNIM.SelectedValue.ToString();
}
Any help will be really appreciated! Thank you!
So..., I have found a solution for this particular problem.
I deleted the ComboBox, and then created a new one. Then I just assign the properties programmatically.
cb.DisplayMember = 'nim';
cb.ValueMember = 'nim';
cb.DataSource = mahasiswaBindingSource;
Apparently, leaving the properties window unedited solved the problem!

Get value from selected combox and put it on textbox c#

Mabuhay!
Hi! I search this code here but unfortunately didnt get what I am looking for. Any help on this?
I got this form and have a combo box which i uses dataset so I can get the value of description on this table. And I have a textbox after that but I wanted to get the value of labor cost based on what I selected on combo box.
heres for my combobox
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the '_10daliriPayrollDataSet.Description' table. You can move, or remove it, as needed.
this.descriptionTableAdapter.Fill(this._10daliriPayrollDataSet.Description);
}
for my laborcost.Text value
private void description_SelectedIndexChanged(object sender, EventArgs e)
{
laborcost.Text = description.SelectedItem.ToString();
}
I got this error;
System.Data.DataRowView
Any help on this?
Thanks,
Chris
try
description.SelectedItem.Text this will do
The correct way of retrieving the item text is the GetItemText method.
It works for any item, including the selected one. The later can be retrieved from the SelectedItem property.
In your case
laborcost.Text = description.GetItemText(description.SelectedItem);
Please note that SelectedText and SelectedValue properties (suggested by some commenters) have different semantics and cannot be used for this purpose.
Try this,
description.SelectedValue.ToString();
You need to define DataValue member and Text Member field on your combobox. Its binding with DataRowView.
So just add two line after
this.descriptionTableAdapter.Fill(this._10daliriPayrollDataSet.Description);

C# show property in textbox depending on combobox selecteditem

I have maybe an easy question but I would like to ask about possibilities how to bind textbox Text property to combobox SelectedItem property. I do it through combobox SelectedItemChanged event and set text like this:
if(cmbMeasuring.SelectedItem != null)
txtMethod.Text = ((ListBoxItem)cmbMeasuring.SelectedItem).Value;
I have class ListBoxItem which holds 2 strings "Name" and "Value". Then I created BindingList for combobox:
private BindingList<ListBoxItem> lst;
and then set combobox data source in constructor:
cmbMeasuring.DataSource = lst;
cmbMeasuring.DisplayMember = "Name";
This works fine but I dont know if its the best way how to do it. But problem occurs when I change the textbox content. I do it through textbox Leave event:
private void txtMethod_Leave(object sender, EventArgs e)
{
if (cmbMeasuring.SelectedItem != null)
((ListBoxItem)cmbMeasuring.SelectedItem).Value = txtMethod.Text;
}
If textbox lost focus I assign item value. But I have also a menustrip to save input and when I click to it directly this event dont occur so the last input is not saved. I know that this could be done through textbox TextChanged event but it consume a lot of time.
Do you have any better solutions or is it OK? Im not using WPF.
Thanks.
If you have a Click event for the MenuStrip item, you can do the following
MyMenuStripItem.Focus();
This should cause the MenuStrip item to gain focus and therefore causing the TextBox to lose focus.
Try data binding on the TextBox in your form's constructor:
txtMethod.DataBindings.Add("Text", lst, "Value",
false, DataSourceUpdateMode.OnPropertyChanged);

How can I make a ComboBox non-editable in .NET?

I want to have a "select-only" ComboBox that provides a list of items for the user to select from. Typing should be disabled in the text portion of the ComboBox control.
My initial googling of this turned up an overly complex, misguided suggestion to capture the KeyPress event.
To make the text portion of a ComboBox non-editable, set the DropDownStyle property to "DropDownList". The ComboBox is now essentially select-only for the user. You can do this in the Visual Studio designer, or in C# like this:
stateComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
Link to the documentation for the ComboBox DropDownStyle property on MSDN.
To add a Visual Studio GUI reference, you can find the DropDownStyle options under the Properties of the selected ComboBox:
Which will automatically add the line mentioned in the first answer to the Form.Designer.cs InitializeComponent(), like so:
this.comboBoxBatch.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
Stay on your ComboBox and search the DropDropStyle property from the properties window and then choose DropDownList.
Before
Method1
Method2
cmb_type.DropDownStyle=ComboBoxStyle.DropDownList
After
COMBOBOXID.DropDownStyle = ComboBoxStyle.DropDownList;
To continue displaying data in the input after selecting, do so:
VB.NET
Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
e.Handled = True
End Sub
C#
Private void ComboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
for winforms .NET change DropDownStyle to DropDownList from Combobox property

Categories