In my winform data entry application , I have a combo box set to "Dropdown List" so the user couldn't add any values other than items present in my combo box.
but while saving if the user doesn't select any item in the combobox I need to save the form with default value("Mg") , but whenever I try to save it shows the field can't be null as it doesn't accept null values:
what I've tried during (save button click event) 1:
string unitcb = unitComboBox.Text;
finishUOMComboBox.Text = uomtxt;
try2:
finishUOMComboBox.SelectedText = "Mg";
try3:
finishUOMComboBox.ValueMember = "Mg";
try4:
finishUOMComboBox.Text = "Mg";
try5:
finishUOMComboBox.selctedindex = 0;
please help how to save "mg" in the combbox when nothing is selected or it is null while saving.
Try this way to get ComboBox selected item:
finishUOMComboBox.selectedItem.ToString();
This code returns your selection's value
Related
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
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].
I was just playing with combo box value and was trying something new. I just wanted to know how should I remove or hide the already selected value of a combo box the next time i use this combo box. I mean once I have selected a value from a combo box, I would not be able to see that selected value again when I click the combo box the next time. I have the following values in the combo box A
List<string> comboList = new List<string>();
comboList .Add("--Please Select--");
comboList .Add("ABC");
comboList .Add("DEF");
comboList .Add("GHI");
comboList .Add("JKL");
comboList .Add("MNO");
cmbNewComboBox.DataSource = comboList.DeepCopy();
if I select value "ABC" from the cmbNewComboBox and click ok, then I would not be getting this value again in this combobox. i.e, the values that will be displayed in the combobox will be only "DEF, GHI, JKL, MNO" BUT NOT "ABC"
Thanks in advance
if(cmbNewComboBox.SelectedIndex != -1)
comboList.Remove(cb01.SelectedItem.ToString());
try this
private void combobox1_SelectedValueChanged(object sender, EventArgs e)
{
int iIndex;
if (int.tryParse(combobox1.Tag, out iIndex))
{
if (iIndex > -1)
((List<string>)combobox1.DataSource).RemoveAt(iIndex);
}
combobox1.Tag = combobox1.SelectedIndex;
}
I implemented a drop down in my ASP.net page .I put data sourse and bind drop down in page load event . I get the data in the drop down without any problem . But when i selected a value it always send index 1 value . I tried different values but it always sends the index 1 value to the backend
string[] parity = conData.GetParity();
ddlParity.DataSource = parity.ToList();
ddlParity.DataBind();
modemDetailsObj.Parity = ddlParity.SelectedValue;
You are probably binding the dropdown in postback again. You need to bind it in !Postback
if(!Page.IsPostBack)
{
string[] parity = conData.GetParity();
ddlParity.DataSource = parity.ToList();
ddlParity.DataBind();
}
i have bounded DataSource to a combox box and then set this Code
Combox1.ValueMember = "CapacityID";
Combox1.DisplayMember = "Capacitys";
it show Data with no problem , but when i want gain selectedtext it returned me "" and using selectedItem , return name of combo box !
selectedvalue return correct data .
Combox1.SelectedItem.ToString(); //return "Combox1"
Combox1.SelectedValue.ToString(); //Work Correctly
Combox1.SelectedText.ToString(); // return ""
Combox1.SelectedItem retunrs you selected ListItem object not text value of selected item
its should be like :
ListItem li = Combox1.SelectedItem;
or
Object selectedItem = comboBox1.SelectedItem;
MessageBox.Show("Selected Item Text: " + selectedItem.ToString() );
From MSDN : http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selecteditem.aspx
Combox1.SelectedText - Check Msdn for this : http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedtext.aspx
FROM MSdn why its return empty string - if you retrieve the SelectedText value in a button Click event handler, the value will be an empty string. This is because the selection is automatically cleared when the input focus moves from the combo box to the button.
ComboBox.Text.Tostring() returned selected text and solved my problem
String status = "The status of my combobox is " + comboBoxTest.Text
SelectedText property from MSDN
Gets or sets the text that is selected in the editable portion of a ComboBox.
while Text property from MSDN
Gets or sets the text associated with this control.
Use
Combox1.SelectedItem.Text // To get SelectedText
Combox1.SelectedItem.Value // To get SelectedValue
Instead of
Combox1.SelectedItem.ToString()
So altough, your question is not really gramatically clear, to get the calue of your selected item always use
Combox1.SelectedValue
Why?
Because:
Combox1.SelectedItem
returns 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 ("").