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!
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 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;
}
I m Working On A windows Form.. I Need my TextBox Not To Accept negative Values ..How Can I Do this..
IS There Any Property Availiable For Doing The same...
You need to write keypress event of textbox like :
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
You can also user numeric updown control to prevent negetive values.
UPDATE :
Ref: Sai Kalyan Akshinthala
My code will not handle the case of copy/paste. User can enter negative values by copy/paste. So I think Sai Kalyan Akshinthala's answer is correct for that case except one small change of Length >= 2.
private void textBox1_TextChanged(object sender, EventArgs e)
{
if(textBox1.Text.Length >= 2)
{
int acceptednumber = Convert.ToInt32(textBox1.Text);
if(acceptednumber < 0)
{
textBox1.Text = "";
MessageBox.Show("-ve values are not allowed");
}
else
{
textBox1.Text = textBox1.Text;
}
}
}
yes you can do write the following code part in textchanged event of textbox
if(textBox1.Text.Length >= 2)
{
int acceptednumber = Convert.ToInt32(textBox1.Text);
if(acceptednumber < 0)
{
textBox1.Text = "";
MessageBox.Show("-ve values are not allowed");
}
else
{
textBox1.Text = textBox1.Text;
}
}
just use min and pattern will not allow to enter a minus value
min="0" pattern="^[0-9]+$" in input type
if (textBox1.Text != "") // this forces user to enter something
{
// next line is supposed to allow only 0-9 to be entered but should block all...
// ...characters and should block a backspace and a decimal point from being entered....
// ...but it is also allowing characters to be typed in textBox1
if(!IsNumberInRange(KeyCode,48,57) && KeyCode!=8 && KeyCode!=46) // 46 is a "."
{
e.Handled=true;
}
else
{
e.Handled=false;
}
if (KeyCode == 13) // enter key
{
TBI1 = System.Convert.ToInt32(var1); // converts to an int
Console.WriteLine("TBI1 (var1 INT)= {0}", var1);
Console.WriteLine("TBI1= {0}", TBI1);
}
if (KeyCode == 46)
{
MessageBox.Show("Only digits...no dots please!");
e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
}
}
else
{
Console.WriteLine("Cannot be empty!");
}
// If I remove the outer if statement and skip checking for an empty string, then
// it prevents letters from being entered in the textbox. I need to do both, prevent an
// empty textbox AND prevent letters from being entered.
// thanks, Sonny5
You didn't specify where this code runs, but my assumption would be it runs on key down. Since key down is received before the character is processed and the Text property is updated, your check for .Text == "" will prevent the rest of the validation running, at least for the first character.
You should move the check for empty value on a different event than the check for the key pressed.
I think you could use the IsDigit function.
Something along these lines:
string textBoxText = "12kj3";
if (!textBoxText.Equals(String.Empty)) // this forces user to enter something
{
foreach (char c in textBoxText.ToArray())
{
if (!Char.IsDigit(c))
{
//return false;
}
}
//return true;
}
else
{
Console.WriteLine("Cannot be empty!");
}
Hope you get the idea.
You can use the following RegEx to check that it is a number "^\d+$" and required.
bool bV=false;
private void textBox1_Validated(object sender, EventArgs e)
{
TextBox textBoxText = sender as TextBox;
if (!textBoxText.Equals(String.Empty))
{
foreach (char c in textBoxText.Text.ToArray())
{
if (!Char.IsDigit(c))
{
if (!bV)
{
MessageBox.Show("Input value not valid plase Insert Integer Value");
bV = true;
textBox1.Text = String.Empty;
break;
}
}
}
}
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
bV = false;
}