Transfer a method to another method - c#

I'm new here and I have a doubt. It is possible to transfer a method to another method?
private void button1_Click(object sender, EventArgs e)
{
}
private void c_Click(object sender, EventArgs e)
{
MessageBox.Show("Transfer OK!!!");
}
private void button2_Click(object sender, EventArgs e)
{
// c_Click ????? ------> button1_Click
}
By clicking on Button2, via code is included c_Click the content within the button1.
Finally, clicking on button1, I need to bring up the "Transfer OK" message. Is this possible?

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Default Message");
c.PerformClick();
}
private void c_Click(object sender, EventArgs e)
{
MessageBox.Show("Transfer OK!!!");
}
Button.PerformClick Method used to call button click event in any method

I think you can use something like this inside your button2_Click:
button1.PerformClick();
or
c_Click(sender, e);

private void button1_Click(object sender, EventArgs e)
{
c_Click(sender, e);
}

I honestly don't know if I understand your question but based on your comments:
I do not want to run "PerformClick ()" or anything similar. Basically,
it would delete the content of the button1_Click and include C_Click
content within the button1_Click.
Clicking on button1, I need to bring up the "Transfer OK" message.
Button2 will have code that will perform the deletion of button1
content and will include the contents of C_Click event. I guess it's
something using "Delegates" or similar.
You're probably not using the word "Content" in the Windows Forms sense of the word. What I get from that is that when you click button2, you want button1 to start acting like the c_Click button, correct?
If I get you correctly you need to simply remove button1's EventHandler for button1_Click and replace it with c_Click, like so:
private void button1_Click(object sender, EventArgs e)
{
}
private void c_Click(object sender, EventArgs e)
{
MessageBox.Show("Transfer OK!!!");
}
private void button2_Click(object sender, EventArgs e)
{
// c_Click ????? ------> button1_Click
button1.Click -= new EventHandler(button1_Click);
button1.Click +=new EventHandler(c_Click);
}
So that after you click button2, the next time you click on button1, it will do c_Click() instead of button1_Click()

Related

How to make buttons display their text in a label using one click event

Is it possible to have a click event that displays the text of buttons in a label in c#.
I want to write single code that will work for:
public button1_Click(Object sender, EventArgs e){
label1.Text = button1.Text;
}
public button2_Click(Object sender, EventArgs e){
label2.Text = button2.Text;
}
Is this what you are looking for?
On a button click, you can handle setting text as below
you can add two buttons. Have same click events for both buttons.
Take help of parameter sender to get which button is clicked.
code (sender as Button) gives you all the details of clicked button.
private void button1_Click(object sender, EventArgs e)
{
this.label1.Text = (sender as Button).Text;
}
1) Add a Button and a Label to the blank Form in a new project.
2) Double-click the Button.
This will generate the following Click event method...
private void button1_Click(object sender, EventArgs e) {
}
3) Put the following line of code in the button1_Click method body:
label1.Text = button1.Text;
4) Profit.

C# How to set button text of different buttons to the same textbox

I have a couple of buttons and a textbox. I want to make it so that when I press button1 the text from the textbox goes to button and when I press button2 the text goes to button2 and so on. I now have this:
protected void Button1_Click(object sender, EventArgs e)
{
Button1.Text = TextBox1.Text;
}
protected void Button2_Click(object sender, EventArgs e)
{
Button2.Text = TextBox1.Text;
}
protected void Button3_Click(object sender, EventArgs e)
{
Button3.Text = TextBox1.Text;
}
Edit: Is there a shorter way to do this?
If you point the Click event for each button to the same method you can have this in one method like so;
protected void Button_Click(object sender, EventArgs e)
{
((Button)sender).Text = TextBox1.Text;
}
You can change the method that is used for a button event in the designer by clicking on the button, going to the Properties window and clicking on the little lightening symbol for events and selecting the Button_Click method for the Click event.

How do I make a button show the listbox when clicked?

I am making a form where if a button is clicked it will go to my list box and run the functions I have in there though I am a bit confused on how to make it realise when the button has been clicked and for the listbox to work. Here is my code >_>
private void button1_Click(object sender, EventArgs e)
{
}
public void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
Ping.PlayConsole();
}
You just need:
private void button1_Click(object sender, EventArgs e)
{
Ping.PlayConsole();
}
It's ok to call the same function under different handlers.
Try this :
private void button1_Click(object sender, EventArgs e)
{
ListBox_SelectedIndexChanged(sender,e);
}
Good Luck !!
private void button1_Click(object sender, EventArgs e)
{
ListBox.Focus();
Ping.PlayConsole();
}
Both event handlers share the same signature void (object , EventArgs), so they are call-compatible.
If you are connecting the event visually using the form designer:
Go to the Property Inspector's Events pane and instead of double-clicking it to create an event handler stub for button1.Click, click the dropdown box icon that appears on the right side. Visual Studio will show all the event handlers present in the form that have a compatible signature, you should be able to choose ListBox_SelectIndexChanged for the button1.Click handler. They will be sharing the same handler.
If you are connecting the handler by code then this should work too:
ListBox1.SelectIndexChanged += new System.EventHandler(ListBox_SelectedIndexChanged);
button1.Click += new System.EventHandler(ListBox_SelectedIndexChanged);

Button1 not being clicked on startup

I am having an issue with my WinForm application.
Below I have my code for my button that I want to click.
private void button1_Click(object sender, EventArgs e)
{
// Do code.
}
Now I want to run the program on start up, so I have this code below:
private void form_Load(object sender, EventArgs e)
{
this.button1_Click(object sender, EventArgs e);
}
But this does not work. It has red lines under the words: "sender", "EventArgs e"
What am I doing wrong, Please help me?
Any help would be greatly appreciated, thanks!
First if you want to click button that way you should do:
private void form_Load(object sender, EventArgs e)
{
button1.PerformClick();
}
Second,
it is not a good idea to do it anyway, better approach is to create common method that is call in button_click event and form_load event.

Calling click event of a button serverside

How can I click or load the click() event of a button in codebehind?
I already tried
btn.Click();
but this gives an error. I am using ASP.NET
I will assume that you have a button called Button1
and that you have double clicked it to create an event handler.
To simulate the button click in code, you simply
call the event handler:
Button1_Click(object sender, EventArgs e).
e.g. in your Page Load Event
protected void Page_Load(object sender, EventArgs e)
{
//This simulates the button click from within your code.
Button1_Click(Button1, EventArgs.Empty);
}
protected void Button1_Click(object sender, EventArgs e)
{
//Do some stuff in the button click event handler.
}
If you don't need the context provided by the sender and eventargs then you can just refactor to create a method (eg DoStuff()) and make your event handler just call that. Then when you want to call the same functionality from elsewhere you can just call DoStuff(). It really depends on whether you want to actually simulate a click or not. If you want to simulate a click then other methods are better.
Do you wish to call all the event handlers attached to the button, or just one?
If just one, call the handler:
btn.btn_Click(btn, new EventArgs());
If all of them, trigger the event:
var tmpEvent = btn.Click;
if (tmpEvent != null)
tmpEvent(btn, new EventArgs());
Try this:
YourButton_Click(sender, e);
Edit try this:
protected void Page_Init(object sender, EventArgs e)
{
Button1.Click += new EventHandler(Button1_Click);
}
void Button1_Click(object sender, EventArgs e)
{
}
in the .cs page
Just call the buttons click function.
http://forums.asp.net/t/1178012.aspx
Instead of trying to call the event just call the function that event uses.
Example: btn.Click() calls
btn_Click(object sender, EventArgs e)
so you should call the function btn_Click(btn,new
EventArgs());
It's been a long time ago, but here is my solution
You can use:
btn.PerformClick();
protected void Page_Load(object sender, EventArgs e)
{
functionName();
}
protected void btn_Click(object sender, EventArgs e)
{
functionName();
}
public void functionName()
{
//function code goes here, and call this function where ever you want
}
try this one.......
YourButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
This works for me.

Categories

Resources