I know the answer is probably really obvious, but I'm having a complete brain fart here.
What I want to do is have a simple form with two buttons. The first button shows a message saying "1". After clicking the second button, the first button says "2" when clicked. After clicking the second button again, it goes back to saying "1".
Having learned programming with javascript, I'm still thinking in javascript. My first train of thought was:
myButton.onclick=...
After that the only thing I could think of was to have a conditional inside the first button's click function and use the second button's click to change a boolean...
Is there a better way to do this?
Make button2 change the method which is called when button1 is clicked:
private void button1_Click(object sender, EventArgs e)
{
Text = "1";
}
private void button1_Click2(object sender, EventArgs e)
{
Text = "2";
button1.Click -= button1_Click2;
button1.Click += button1_Click;
}
private void button2_Click(object sender, EventArgs e)
{
button1.Click -= button1_Click;
button1.Click += button1_Click2;
}
I understood, what happens when one click the button2 first time and for the second time. But if you want to say that its the pattern it has to follow, then:
Bool bln = true;
private void button2_Click(object sender, EventArgs e)
{
if(bln)
text1();
else
text2();
bln = !bln;
}
private void text1()
{
// Whatever
}
private void text2()
{
// Whatever
}
Populate the method with whatever you are looking for. In this, These methods can be reused for button1 click event also.
Hope it helps.
Have the second button's click handler set a message variable, and simply have the first button's click handler display it.
edit: I wrote this for asp.net. Converting to winforms shouldn't be difficult.
Declare a string variable:
public static mystring = "1";
on button2_click you can change that value. I am showing the simple case of changing it once permanently, but you can write code to alternate in any way you like:
protected void btn2_Click(object sender, EventArgs e)
{
mystring = "2";
}
For example, to alternate between 1 and 2, you can set the value of int n = 1, and have btn2 set the value to (3-n).ToString();
Then do:
protected void btn1_Click(object sender, EventArgs e)
{
Response.Write(mystring);
}
You have to do myButton.onclick-=function_name
+= is to add function
-= is to remove function
You can always make it work this way:
private void button1_Click(object sender, EventArgs e)
{
button1.Text = "Button 1 Text";
MessageBox.Show("B1 Function1");
button1.Click -= button1_Click;
button1.Click += button1_Click2;
}
private void button1_Click2(object sender, EventArgs e)
{
button.Text = "Button 1 Text 2";
MessageBox.Show("B1 Function2");
button1.Click -= button1_Click2;
button1.Click += button1_Click;
}
Related
I have multiple button click events:
private void button1_Click(object sender, EventArgs e)
{
Procedure(1);
}
private void button16_Click(object sender, EventArgs e)
{
Procedure(16);
}
However, I want to achieve something like this:
private void button[i]_Click(object sender, EventArgs e)
{
Procedure(i);
}
In Winforms, there is a call to InitializeComponent() in the class constructor.
In that method (it will take you to the form designer if you put the mouse cursor on the method and right click > goto definition or F12) you will see how events are hooked up:
button1_Click += button1_Click...
You can simply subscribe the buttons click event to your Procedure method.
button1_Click += CallToProcedure;
How do you work out which button was clicking then? You simply get it off the sender argument in the parameter:
private void CallToProcedure(object sender, EventArgs e)
{
Button btn = sender as Button;
int i = Convert.ToInt32(btn.Name.Replace("button", string.Empty));
Procedure(i);
}
You can store the parameter you're passing to your button (i in your example) in the Button's Tag property then use something like this:
var senderButton = sender as Button;
if (sender != null)
{
Procedure((int) senderButton.Tag);
}
If you can rely on the naming, then simple:
private void button_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
int i = Convert.ToInt32(btn.Name.Replace("button", ""));
Procedure(i);
}
use button.Tag property, is designed for such cases.
read is doc / MSDN
i need some help what is wrong in this code
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
Button1.ID = "Button4";
Button2.ID = "Button1";
Button1.Click -= Button1_Click;
Button1.Click +=new EventHandler( Button2_Click);
//Button2.ID = "Button1";
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Write("success");
}
When i click on button1, it calls button1_click and when i click on button2, it also calls button1_click instead of button2_click.
I believe this could be your problem:
Button2.ID = "Button1";
According to the MSDN Documentation setting Control.ID will "provide you with programmatic access to the server control's properties, events, and methods.". You literally seem to assign Button1 to Button2, making Button2 fire the event subscribed by Button1.
Remove the Button2.ID line and I think it should work.
If it still doesn't work I recommend you to look through the .Designer.cs file and see if you've subscribed to the Button2_Click event.
I have a Windows Form, DataGridView and two buttons.
When I will press the button1 it changes a value of RowHeadersVisible to true.
When I will press the button2 it changes a value of RowHeadersVisible to false.
public Form1()
{
InitializeComponent();
dataGridView1.RowHeadersVisible = false;
}
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.RowHeadersVisible = true;
}
private void button2_Click(object sender, EventArgs e)
{
dataGridView1.RowHeadersVisible = false;
}
I cannot find any kind of events about "RowHeadersVisible" value changing in "DataGridView" class. As I mentioned "CellFormatting" event works for this action but it appears often, almost for all kind of action made in datagridview1.
I think we might create a custom event handler in order to make different decisions.
When "RowHeadersVisible" changes the value to false I need to call another function inside "CustomEvent".
private void CustomEvent(object sender, EventArgs e)
{
SomeFunction();
}
On the other hand "DataGridTableStyle" class has the event "RowHeadersVisibleChanged".
So How to solve this problem?
.NET 4.5, you should get help.
Dissolve it in the following way.
Represents the table drawn by the System.Windows.Forms.DataGrid control at run time.
// Instantiate the EventHandler.
public void AttachRowHeaderVisibleChanged()
{
myDataGridTableStyle.RowHeadersVisibleChanged += new EventHandler (MyDelegateRowHeadersVisibleChanged);
}
// raise the event when RowHeadersVisible property is changed.
private void MyDelegateRowHeadersVisibleChanged(object sender, EventArgs e)
{
string myString = "'RowHeadersVisibleChanged' event raised, Row Headers are";
if (myDataGridTableStyle.RowHeadersVisible)
myString += " visible";
else
myString += " not visible";
MessageBox.Show(myString, "RowHeader information");
}
// raise the event when a button is clicked.
private void myButton_Click(object sender, System.EventArgs e)
{
if (myDataGridTableStyle.RowHeadersVisible)
myDataGridTableStyle.RowHeadersVisible = false;
else
myDataGridTableStyle.RowHeadersVisible = true;
}
I want to make a click event for a bunch of buttons. The problem is that I want to use the button's Text, and pass it to a function. Now the click event is passed a object sender. When I tried changing that to Button sender, it gave errors. But I don't know how else I can work with the senders Text.
Here is the normal code, which gave a single error:
private void guess_Click(object sender, EventArgs e)
{
guess(sender.Text);
}
I changed it to this, which gave errors:
private void guess_Click(Button sender, EventArgs e)
{
guess(sender.Text);
}
Question:
How can I work with the Button's Text property within this click event, which is a single click_event for multiple buttons?
Step 1: You need to subscribe to the Button Click event of all your buttons to the same EventHandler. so that button click on all your Buttons will fire the same `Event Handler.
Step 2: You need to cast the object sender into Button and then access its Text property to get the Button Text.
Try This:
button1.Click += new System.EventHandler(MyButtonClick);
button2.Click += new System.EventHandler(MyButtonClick);
button3.Click += new System.EventHandler(MyButtonClick);
private void MyButtonClick(object sender, EventArgs e)
{
Button btnClick = (Button)sender ;
guess(btnClick.Text);
}
Cast sender to type button.
Example:
private void guess_Click(object sender, EventArgs e)
{
guess(((Button)sender).Text);
}
You need to cast the sender object to the Button type and use that:
private void guess_Click(object sender, EventArgs e)
{
Button senderBtn = senderBtn as Button;
if(senderBtn != null)
{
guess(senderBtn.Text);
}
}
I need to dynamically create buttons (one for loop) and add "onClick" and "doubuleClick" events on it.
I did it like this:
Button bt = new Button();
bt.Click += bt_Click;
bt.DoubleClick += bt_DoubleClick;
private void bt_Click(object sender, EventArgs e)
{
label1.Text = this.Text;
}
private void bt_DoubleClick(object sender, EventArgs e)
{
//some code
}
First: My "bt_Click" method gets "main form" text in "label1". In debugger I see that sender is a button. What is wrong with it?
Second: My "bt_DoubleClick" event do not react at all, am I doing something wrong here?
Any help is appreciated.
You should cast sender to Button to get the bt.Text:
Button bt = new Button();
bt.Click += bt_Click;
bt.Text = "click me";
bt.Location = new Point(100,100);
this.Controls.Add(bt);
private void bt_Click(object sender, EventArgs e)
{
label1.Text = (sender as Button).Text;
}
Buttons doesn't react to double click event. You can read it here in detail.
In response to the first question, if I understand you correctly, in this.Text, this refers to the form because the method bt_Click is a member of the Main Form class. I think you might have meant to do:
private void bt_Click(object sender, EventArgs e)
{
label1.Text = (Button)sender.Text;
}
Second: Is this just a case of the bt_Click handler firing twice?
The easiest way to do this it is to use "datagrid".
Datagread has the great support for all events and for organization of items (image, text and so on).
I have made "save" or "open" dialog form to browse content from remote SFTP server, very easy with datagrad, but I had a problem to do it with buttons or labels.