C# ComboBox Usage [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Google has not helped me on this one..
Say I have a combobox, with the values "X" and "Y".
What is the syntax to say..
"If the user selected X, do this, else do that."
I've tried several ways.. none work.
Thanks in advance.

I'll assume you're using WinForms, the property you're looking to use is ComboBox.Text.
Something like:
if (xyCombo.Text == "X")
// Do something
else (xyCombo.Text == "Y")
// Do something else

You have to subscribe to ComboBox's SelectedIndex changed event. Please refer to the below link.
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged.aspx

Try combining the above answers, like this.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.Text == "X")
//Action
else
//Other Action
}
or use a switch statement
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox1.Text)
{
case "X":
//Action
break;
case "Y":
//Another Action
break;
default:
break;
}
}

Related

Windows Form: Display unique panel based on list selection [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Using a Windows Form I am trying to replicate the functionality of a "Tab Control" but instead of the panel selection being controlled by a tab selection it would be controlled by a list. Is there a built in way to produce this using neatly like the tab control or should I just check if a list value is equal to some value and if yes display panel else don't?
This is probably a dumb question so sorry for wasting anyone's time and thanks in advance!
Besides, you can also use tabpages in tabcontrol as "panel". Just hide the header via the code
private void Form1_Load(object sender, EventArgs e)
{
tabControl1.Appearance = TabAppearance.FlatButtons;
tabControl1.ItemSize = new Size(0, 1);
tabControl1.SizeMode = TabSizeMode.Fixed;
foreach (TabPage tab in tabControl1.TabPages)
{
tab.Text = "";
}
}
Then select the tabpage like,
private void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
if(((ListBox)sender).SelectedItem.ToString() == "tabPage2")
{
tabControl1.SelectedTab = tabPage2;
}
//...
}

simple calculator that shows the different between 2 numbers in C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
first of all sorry for my broken language. i would like to create a calculator that shows the different between 2 numbers in C#
i tried to make it but i always end up with adding the textbox 1 to the textbox 2 and displaying the total in text box 3, which i don't want to do
please see the GUI , it's simple
i also would like to make the number in green color if the different is positive and red if the different is negative. see the picture
thanks!
simple gui
Write this code in the click event of calculate button
private void Calculate_Click(object sender, EventArgs e)
{
if (textBox1.Text != "" && textBox2.Text != "")
{
int diff = (Convert.ToInt32(textBox1.Text) - Convert.ToInt32(textBox2.Text));
textBox3.Text = diff.ToString();
if (diff >= 0)
{
textBox3.ForeColor = Color.Green;
}
else
textBox3.ForeColor = Color.Red;
}
else
{
MessageBox.Show("PLZ Enter the balances");
}
}

How to perform an action while a key is pressed [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
How to make that when pressing a key do an action and when you press it again, stop doing it in C#.
What I want to do: I have a program which the user must press F1 to activate any action, well; so that the user does not press another key, but the same one, how would this be done in C #? Since I currently stop the action by doing the following:
if (GetAsyncKeyState(Keys.F1) == -32767)
{
timer1.Start();
}
if (GetAsyncKeyState(Keys.F2) == -32767)
{
timer1.Stop();
}
You can use System.Timers.Timer.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Key.A)
{
if(!timer1.Enabled)
timer1.Start();
else
timer1.Stop();
}
}

Disable Enter Key in Textboxes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am developing a chatting system. And I have a Button to send the text in the text box to the chat log. How am I going to stop the user from press Enter key and to disable the Enter key. I know there are many posts out there like this, but the solutions haven't worked for me.
You can try something like this:-
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
}
I think you do not need to stop the user from pressing enter but instead send the chat to the other person on press of enter.
Also if you have any other shortcuts to be allowed then you can have a look at this C#: How to make pressing enter in a text box trigger a button, yet still allow shortcuts such as "Ctrl+A" to get through?
Using the same you can also block
private void textBoxToSubmit_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress=true;
}
}
Your question is a little ambiguous to say the least; however, the textbox control has an event called KeyDown : http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx
This way, you can capture whenever Enter is pressed and modify and behavior as needed, here is an example
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (Keys.Enter == e.KeyCode)
{
MessageBox.Show("Enter Was Pressed");
textBox1.Text = new String(textBox1.Text.Where((ch, i) => i < textBox1.Text.Length - 2).ToArray());
textBox1.SelectionStart = textBox1.Text.Length;
}
}

Simple Condition For Void [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i have 2 controls for highlighting, and separated void control like:
public void yee()
{
rtb.ForeColor = Color.Red;
}
public void yoo()
{
rtb.ForeColor = Color.Blue;
}
then I have a combobox which have an item of:
yee and yoo .
when I select yee the void yee should be selected but when I select yoo the void yoo should be selected.
I know its kinda easy but i need the condition to be "case" instead of "if" since I want to have break, and if it's possible make the void = false if one got selected .
Thanks a lot! and sorry for this newbie question .xD
It is still unclear what you are trying to do, but if you are wanting to add a switch statement to determine which method that you need to run, assign it to your ComboBox's SelectedIndexChanged or SelectedValueChanged EventHandlers.
See if something like this is what you are wanting.
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
switch ((sender as ComboBox).SelectedItem.ToString().ToLower()) //Have to get the string representation
{ //since Selected Item is an object
case "yee":
yee();
break;
case "yoo":
yoo();
break;
}
}
It's not totally clear what you're trying to do, but this is my best stab at it. Note DropDownList1 is the ID of your DDL.
if(DropDownList1.SelectedItem.Text == "yee"){
yee();
}
if(DropDownList1.SelectedItem.Text == "yoo"){
yoo();
}
Well, you can just do a bool function
such as
public boolean yeeOrYooPressed() {
{
if(//yee is selected) {
return true;
}
else {
return false;
break;
}
}

Categories