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;
}
Related
I have a simple ASP.NET Application. And On one of my pages all dropdownlist_SelectedIndexChanged events trigger only after I click save button.
This is part of my code behidn:
protected void ddlTimekeeperOffice_SelectedIndexChanged(object sender, EventArgs e)
{
CheckUniqueCombination();
}
protected void ddlTkprDepartment_SelectedIndexChanged(object sender, EventArgs e)
{
CheckUniqueCombination();
}
protected void ddlTkprSummaryTitle_SelectedIndexChanged(object sender, EventArgs e)
{
CheckUniqueCombination();
}
this is 3 dropdowns
protected void btnSave_Click(object sender, EventArgs e)
{
trTimekeeperRow.Visible = true;
}
and a save button.
When I select anything in dropdowns > Nothing happens
Then I click save button and before save_click it goes through every ddl_selectedindexchanged.
Why?
Make sure AutoPostBack property of the dropdown controls is true.
I have a label working as a button. I would like when I press a button the click event to this label to take action. for example
private void Label1_Click(object sender, EventArgs e)
{
MessageBox.Show("I pressed this label and showed me this messagebox");
}
Now I want when I press this button, the label1 click event to be performed
private void button1_Click(object sender, EventArgs e)
{
// I want when I press this button something like this happens
Label1.PerformClick();
}
private void button1_Click(object sender, EventArgs e)
{
Label1_Click(sender, e);
}
now if you want to show a message of which control was clicked all in one method do the following
private void label1_Click(object sender, EventArgs e)
{
Control control = (Control)sender;
var name = control.Name;
MessageBox.Show(string.Format("I pressed this {0} and showed me this messagebox",name));
}
Two ways to do this.
First:
private void button1_Click(object sender, EventArgs e)
{
Label1_Click(sender, e); // Just call the Label's click handler
}
Second:
// Bind the Label1_Click handler to the button1 Click event, as they both use the same delegate
button1.Click += new EventHandler(Label1_Click);
With the second approach, note that in C# delegates are multi-cast, so both the button1_Click handler and the Label1_Click handler will be called when the button is clicked, in the order they were bound.
private void button1_Click(object sender, EventArgs e)
{
//What the label click do:
MessageBox.Show("I pressed this label and showed me this messagebox");
}
Is that not easier?
Why do you want to do it ?
I think it would be easier for you to just include the lable click functionality with the button click. Maybe even separate each piece in their own method and call them from the button click. Here is how you'd call another click event.
private void button1_Click(object sender, EventArgs e)
{
label1_Click(sender, e);
}
public class MyLabel:Label
{
public void PerformClick()
{
OnClick(new EventArgs());//InvokeOnClick(this,new EventArgs());
}
}
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");
}
I have a textBox and search button, I would ask about how can I make the user can click Enter to start searching without need to go and click the search button?
This would be best practice
private void txtSearch_Enter(object sender, EventArgs e)
{
AcceptButton = btnSearch;
}
private void txtSearch_Leave(object sender, EventArgs e)
{
AcceptButton = null;
}
The Form has a property called "AcceptButton" that identifies a button that should be associated to the "Enter" keypress. Its considered the "default action" for the form.
More info here:
Windows Form - AcceptButton property
If you want to use something other than Enter/Return, you could also try:
private void EnterKeyAction()
{
// Search...
}
private void btnEnter_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
EnterKeyAction();
}
private void btnEnter_Click(object sender, EventArgs e)
{
EnterKeyAction();
}
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 :)