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;
}
Related
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();
}
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 not able to delete textbox data with the code below
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(char.IsDigit(e.KeyChar)==false)
{
count++;
}
if (count == 1)
{
textBox1.Text = ("");
count = 0;
}
}
tried using clear method as well the alphabet i entered stays in the textbox and when i type any key it get overwritten but i want the textbox to be empty the second time and the prev data to be removed
you just need to say you've handled the event:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar) == false)
{
count++;
}
if (count == 1)
{
textBox1.Text = ("");
count = 0;
e.Handled = true; // this bit fixes it
}
}
use textBox1.Text = ""; OR textBox1.clear();
This will clear your textbox.
You are doing it wrong. You can just paste in a bunch of letters with Ctrl+V. Delete the KeyDown event and create a TextChanged event. This code should accomplish what you are attempting. Please tell me if there is any more details and I will add to my answer.
private void textBox1_TextChanged(object sender, EventArgs e)
{
foreach (char c in textBox1.Text)
if (!char.IsDigit(c)) { textBox1.Clear(); break; }
}
Add this to your text box key press event your problem will be solved
e.handle = true;
I am having DataGridView in a winforms application with a number of ComboBox control. I converted the ComboBox to make it editable.
However, since then, the changes in the column are not being updated in the underlying class object.
When I debug the code, I noticed that the CellValuePushed() event handler was not called when the changes are made in the ComboBox cell.
Any idea what I am missing ?
Thanks
I noticed, when I enter the value in the ComboBox, the CellValueNeeded() eventhandler is being called.
Notice in the screenshot, when I try to enter the data in the combox cell, CellValueNeeded() eventhandler is called
Update :
private void UserDataTable_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if ((UserDataTable.CurrentCell.ColumnIndex == 4) ||
(UserDataTable.CurrentCell.ColumnIndex == 6) ||
(UserDataTable.CurrentCell.ColumnIndex == 8) ||
(UserDataTable.CurrentCell.ColumnIndex == 11))
{
ComboBox combo = e.Control as ComboBox;
combo.DropDownStyle = ComboBoxStyle.DropDown;
if (combo == null)
return;
}
}
private void UserDataTable_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)
{
IntVector temp1 = this.UserData[e.RowIndex];
switch (e.ColumnIndex)
{
case 0: temp1.DeleteMember = (bool)e.Value;
break;
case 1:
temp1.Date1 = Convert.ToDateTime(e.Value).ToString("dd-MM-yy");
break;
case 2:
temp1.CaseNo = Convert.ToString(e.Value);
break;
case 3:
break;
}
}
I am currently working with clicking count and textBox values. Button1 purpose is to execute an specific function depending on the value that texbox7 has. I am not getting any results when I try firing the button click event. Can someone suggest/help ?
Code
private List<string> messages = new List<string>() { "Option1", "Option2", "Option3", "Option4" };
private void button1_Click(object sender, EventArgs e)
{
if (textBox7.ToString() == "Option1")
{
int min = max;
int n = 0;
string s = "";
sw.Start();
}
else if (textBox7.ToString() == "Option2")
{
}
else if (textBox7.ToString() == "Option3")
{
}
else if (textBox7.ToString() == "Option4")
{
}
else if (textBox7.ToString() == "")
{
MessageBox.Show("Please input information");
}
}
Instead of
if (textBox7.ToString() == "Option1")
it should be
if (textBox7.Text == "Option1")
You should compare with the value inside the TextBox, and you can get that using Text property of the textbox.
Your textBox7.ToString() will give you something like System.Windows.Forms.TextBox, Text: text. Because of that you are not getting into any check. Compare your values against the Text property and it should work.
Check the TextBox.Text property in a switch-case statement:
private void button1_Click(object sender, EventArgs e)
{
switch (textBox7.Text)
{
case "Option1":
//do something
case "Option2":
//do something
case "Option3":
//do some thing
case "Option4":
//do something
break;
// If the value of switch-Expression is not 1, 2, 3 or 4 the
// default case is executed.
default:
MessageBox.Show("Please input information");
break;
}
}
It's Simple Use .Text property to get text value of textBox in code behind file
textBox7.Text property instead of
testBox7.ToString() further mode use either if-else statements or switch statement as these both are conditional statements and execution based on condition.
cheer up!