How to change comboBox items if i checked radioButton using c# - c#

if i choose radio Button the combo box items change.
if i choose another radio Button the combo box item change .
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
comboBox1.Items.Add("google");
comboBox1.Items.Add("yahoo");
}
How can I do this??

If they are in same group, you can do this just by adding listener to one of them:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if(radioButton_1.Checked)
{
comboBox1.Items.Remove("google");
comboBox1.Items.Add("yahoo");
}
else
{
comboBox1.Items.Remove("yahoo");
comboBox1.Items.Add("google");
}
}

Related

Items in Combobox gets added

I've two radiobuttons and a single checkbox.
Items in checkbox will pop up according to the radiobutton selected.
The problem is when I click the second radiobutton after clicking the first radiobutton, Combobox pops us all the values from both radiobutton 1 and 2.
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
foreach (string apacservers in Properties.Settings.Default.B)
{
comboBox1.Items.Add(ItemsB);
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
foreach (string europeservers in Properties.Settings.Default.A)
{
comboBox1.Items.Add(ItemsA);
}
}
Any help would be much appreciated
you need to add comboBox1.Items.Clear(); before both of your foreachs.

C# - Uncheck a radio button with a command

I have a button which, when clicked, should uncheck/deselect a radio button which was previously checked.
Just like we clear the text from a textbox when a button is clicked, like this:
private void button1_Click(object sender, EventArgs e)
{
textBox1.Clear();
}
Is there a way to uncheck/deselect a radio button that was already checked?
Simple:
private void button1_Click(object sender, EventArgs e)
{
textBox1.Clear();
radioButton.Checked = false;
}

add click event to many control

I have many controls in my form. for example 120 labels in one panel. and i want when user clicked on each label just call same function with same parameter.
Now i used like this :
private void label67_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
private void label66_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
private void label65_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
private void label64_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
now can i make easy way to call ChangeToTextbox function when user clicked in any label?
Add the same OnClick handler for all labels on the panel:
private void Form1_Load(object sender, EventArgs e)
{
panel1.Controls.OfType<Label>().ToList().ForEach(l => l.Click += label_Click);
}
private void label_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
Find all your controls then just add the handler via code;
List<Control> controls = GetAllMyControls();
foreach(Control control in controls)
{
control.OnClick += (o, e) => { ChangeToTextBox(o); }
}
The syntax should be very similar for both web and winform solutions.
It can be easily achieved by using following approach: Pls give a try,
1) Go to Windows Forms Designer and click the first Label control to select it. Then hold down the CTRL key while you click each of the other labels to select them. Be sure that every label is selected.
2) Then go to the Events page in the Properties window. Scroll down to the Click event, and type label_Click in the box
3) Press ENTER. The IDE adds a Click event handler called label_Click() to the code, and hooks it to each of the labels.
private void label_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
Reference: http://msdn.microsoft.com/en-us/library/dd553231.aspx

Update form from shared event

I've got a form I've created with 4 buttons that will modify a listView, each with their own click event. I'm wondering as a way to avoid adding a method call in each of these buttons if it their is an event available for after a button has finished executing its click event?
The reason for this is so I can update the listView which will in turn update a webBrowser
Here's an example which results in "1" and then "all" being displayed to the user when button 1 is clicked, "2" and then "all" when button 2 is clicked etc etc.
Note: you don't want to do it like this - see comments
public Form1()
{
InitializeComponent();
foreach(var b in Controls.OfType<Button>())
b.Click += new EventHandler(AnyButton_Click);
}
void AnyButton_Click(object sender, EventArgs e)
{
MessageBox.Show("all");
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("1");
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("3");
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("2");
}

How to handle image click under Button click event

Hi all i am having 2 imagebuttons a gridview and a button. Now if i clicked on Image button i will show a grid. Now under button click i would like to capture which image button was clicked if 1st image button is clicked i would like to some values and if 2nd one is clicked i would like to show another
You can discern which button was pressed by comparing the sender parameter:
void MyButton_Click(object sender, EventArgs e)
{
if (sender == MyButton1)
{
// 1st image button was clicked — some values
}
else if (sender == MyButton2)
{
// 2nd one was clicked — show another
}
}
Can't you create 2 events and one function?
ex:
//Hook both OnClick events to these!
private void OnButton1Click(object sender, EventArgs e) { BeenClicked(button1); }
private void OnButton2Click(object sender, EventArgs e) { BeenClicked(button2); }
private void BeenClicked(Button ClickedButton)
{
if(ClickedButton.Text == Button1) Console.WriteLine("Hi to you too!");
}
Or alternatively you could use:
//Hook both OnClick events to this!
private void OnButtonClick(object sender, EventArgs e)
{
ClickedButton = (Button)sender;
if(ClickedButton.Text == Button1) Console.WriteLine("Hi to you too!");
}
If I understood you right :)

Categories