I want to display certain value into a label depending of the item selected in the comboBox, each item in the comboBox will display a different value, problem is the comboBox has a lot of items and each one needs to display a different value
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox.SelectedIndex)
{
case 0:
if (comboBox.SelectedIndex == 0)
{
Label.Text = "8";
}
break;
case 1:
if (comboBox.SelectedIndex == 1)
{
Label.Text = "15";
}
break;
case 2:
if (comboBox.SelectedIndex == 2)
{
Label.Text = "60";
}
break;
}
}
How can I improve this and do it shorter? I was told to use an object array but how to verify which item is selected?
This is an example of using List to make your code shorter:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
IList<string> lstString = new List<string>();
lstString.Add("Hello");
lstString.Add("World");
lstString.Add("Foo");
lstString.Add("C#");
lstString.Add("StackOverflow");
label1.Text = lstString[comboBox1.SelectedIndex];
}
Since list starts at index zero and combobox starts at index zero, you can just call the index of the list to match with the index of your combobox.
You can do an initialization of your combobox.(put it on load event of form or somewhere else depends on your needs.)
var listCombo = new List<int>();
listCombo.Add(8);
listCombo.Add(15);
listCombo.Add(60);
listCombo.ForEach(m => comboBox1.Items.Add(m.ToString()));
Then you can just assign the selected item in the label in the event code of combobox
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label1.Text = comboBox1.SelectedItem.ToString();
}
Related
I am really confused on thinking which control to use for my purpose.
I am having list of items say item1 to item10. User can select 4 or 5 items in any order.
Now user selected items has to be separated in same order.
For example, if the user selected the items in following order, item4, item8, item3 and item2.
I want it in the same order. item4,item8,item3,item2.
How do I achieve this in winforms control?
It is not a very nice solution but I wrote it as you asked.
Set the SelectionMode of your ListBox to MultiExtended or MultiSimple as you need.
Then write this code in SelectedIndexChanged event of your ListBox:
List<string> orderedSelection = new List<string>();
bool flag = true;
private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
{
if (flag)
{
flag = false;
var list1 = listBox3.SelectedItems.Cast<string>().ToList();
if (listBox3.SelectedItems.Count > orderedSelection.Count)
{
orderedSelection.Add(list1.Except(orderedSelection).First());
}
else if (listBox3.SelectedItems.Count < orderedSelection.Count)
{
orderedSelection.Remove(orderedSelection.Except(list1).First());
}
var list2 = listBox3.Items.Cast<string>().Except(list1).ToList();
listBox3.Items.Clear();
for (int i = 0; i < list1.Count; i++)
{
listBox3.Items.Add(list1[i]);
listBox3.SelectedIndex = i;
}
foreach (string s in list2)
{
listBox3.Items.Add(s);
}
flag = true;
}
}
When user selects an item, It comes to first of the list and the rest of the items comes next.
Also, there is an alternative way. You can use a CheckedListBox with two extra button for moving selected items up and down. So user can change the order of selected items.
This solution uses CheckListBox's ItemCheck event along with a private List to keep track of the click items order.
protected List<string> clickOrderList = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
// Populate the checked ListBox
this.checkedListBox1.Items.Add("Row1");
this.checkedListBox1.Items.Add("Row2");
this.checkedListBox1.Items.Add("Row3");
this.checkedListBox1.Items.Add("Row4");
}
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (sender != null && e != null)
{
// Get the checkListBox selected time and it's CheckState
CheckedListBox checkListBox = (CheckedListBox)sender;
string selectedItem = checkListBox.SelectedItem.ToString();
// If curent value was checked, then remove from list
if (e.CurrentValue == CheckState.Checked &&
clickOrderList.Contains(selectedItem))
{
clickOrderList.Remove(selectedItem);
}
// else if new value is checked, then add to list
else if (e.NewValue == CheckState.Checked &&
!clickOrderList.Contains(selectedItem))
{
clickOrderList.Insert(0, selectedItem);
}
}
}
private void ShowClickOrderButton_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (string s in clickOrderList)
{
sb.AppendLine(s);
}
MessageBox.Show(sb.ToString());
}
I'm new to programming and I'm trying to change the value of a textbox depending of a selected value on a combo box, since the values are numerical 1 to 20, and depending on the selection it will be the number of text boxes visible. I'm using the event selected index changed.
Here is my code:
private void cbxPIN_SelectedIndexChanged(object sender, EventArgs e)
{
int pines = Convert.ToInt32(cbxPIN.SelectedItem.ToString());
if (pines == 1)
{
textbox1.visible = true;
}
else if (pines == 2)
{
textbox1.visible = true;
textbox2.visible = true;
}
...
else if (pines == n)
{
textbox1.visible = true;
textbox2.visible = true;
...
textboxn.visible = true;
}
}
since there are like 25 different numeric values on the combo box is there an easier way of doing this? asides from comparing each different value?
Something like a loop.
At the least, I'd rewrite it like this, to reduce duplication:
private void cbxPIN_SelectedIndexChanged(object sender, EventArgs e)
{
int pines = Convert.ToInt32(cbxPIN.SelectedItem.ToString());
if (pines >= 1)
textbox1.Visible = true;
if (pines >= 2)
textbox2.Visible = true;
...
if (pines >= n)
textboxn.Visible = true;
}
Actually, I'd add all the TextBox controls to a collection, possibly in the constructor:
List<TextBox> TextBoxes = new List<TextBox> { textbox1, textbox2, ... textboxn };
Then use LINQ's Take() method to grab the first xx number of controls and iterate through them:
foreach (var tb in TextBoxes.Take(pines))
textBox.Show();
You want to use a loop structure. You should validate that the number of loops to perform is > 0 and < the number of textboxes you have available, but I'll leave error handling to you.
private void cbxPIN_SelectedIndexChanged(object sender, EventArgs e)
{
int pines = Convert.ToInt32(cbxPIN.SelectedItem.ToString());
TextBox textBox;
for (int i = 1; i <= pines; i++)
{
// get the control from the form's controls collection by the control name
textBox = this.Controls["textbox" + pines.ToString()] As TextBox
textBox.Visible = true;
}
}
I have two items from an array in my combobox.
comboBox1.Items.Add(boylar[0]);
comboBox1.Items.Add(boylar[1]);
I want to put them in an if statement,
if (comboBox1.ItemSelected.boylar[0] == true)
{
//do this..
}
else if (comboBox1.ItemSelected.boylar[1] == true)
{
//do that..
}
How do I select it? I have read all other topics but couldn't do it. thanks.
Actually, you would create the event handler for the ComboBox.SelectedIndexChanged event. See more here
private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
ComboBox comboBox = (ComboBox) sender;
switch((string) comboBox.SelectedItem)
{
case "Option1":
//TODO: do something
break;
case "Option2":
//TODO: do something
break;
default:
//TODO: do something
break;
}
}
Your current code checks for both ComboBoxes not having an item selected (a SelectedIndex == -1 means no item selected).
Try this instead:
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex > -1 && comboBox2.SelectedIndex > -1)
{
MessageBox.Show("Bravo");
}
}
If you want the first item selected in both:
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0 && comboBox2.SelectedIndex = 0)
{
MessageBox.Show("Bravo");
}
}
I would suggest SelectedIndex or SelectedItem.
Your code as it currently is compiles, but is not related to your question. You are handling a button click and then checking whether the SelectedIndex is -1 (that means nothing is selected in .NET).
If you want to set the ComboBox to its first item, this is the code you will use:
comboBox1.SelectedIndex = 0;
Remember that C# is 0-based indexing, so 0 is the first item, 1 is the second item, etc.
I'm wondering how to restrict my checkbox from adding to my listbox. At the moment when the user checks the checkbox it will add "Anchovies" to the listbox. What I don't want to happen is when the user deselects the checkbox and re selects it again, "Anchovies" is added to the listbox again (showing two lots of "Anchovies").
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
listBox1.Items.Add("Anchovies");
double total = Convert.ToDouble(textBox2.Text);
total = total + .5;
textBox2.Text = Convert.ToString(total);
}
}
The key is to check if Anchovies already exists on the listBox1 items.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
//If the item is already there, we don't do anything.
if (!listBox1.Items.Contains("Anchovies")) {
listBox1.Items.Add("Anchovies");
double total = Convert.ToDouble(textBox2.Text);
total = total + .5;
textBox2.Text = Convert.ToString(total);
}
}
}
Do it this way
if (checkBox1.Checked)
{
if(!listBox1.Items.Contains("Anchovies"))
listBox1.Items.Add("Anchovies");
double total = Convert.ToDouble(textBox2.Text);
total = total + .5;
textBox2.Text = Convert.ToString(total);
}
To fix this issue, you need to check your list box(for this value, either it is already there or not) before inserting any value in it.
e.g
foreach (var item in listBox1.Items )
{
if(item.ToString() != "Anchovies")
{
listBox1.Items.Add("Anchovies");
}
// other code here.
}
I have sevral text entries in a listbox, we'll call it ListBox1.
Ive been searching google, social.msdn.microsoft.com, and here. I cant figure out how to have each text entry change something when selected.
i.e
string1 causes ((value1 + value2) / 2)
string2 cuases ((value3 + value4) / 2)
string3 causes ((value5 + value6) / 2)
Im obviously new.
You need to handle the ListBox.SelectedValueChanged event.
In main, or by using the designer, register the event handler:
listBox1.SelectedValueChanged += listBox1_SelectedValueChanged;
Then, your event handler:
void listBox1_SelectedValueChanged(object sender, EventArgs e) {
string value = listBox1.SelectedValue as string;
if (value == null) return;
// What to do now?
switch(value) {
case "string1":
// Do something...
break;
case "string2":
// Do something...
break;
case "string3":
// Do something...
break;
}
}
You can use the SelectedIndexChanged event to execute code when items are selected. You can either test SelectedIndex or SelectedItem to see which item has been selected.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItems.Count == 0)
return;
int selectedItemIndex = listBox1.SelectedIndex;
string selectedItemText = listBox1.SelectedItem.ToString();
// E.g.
this.Text = selectedItemText;
}