calling a event handler within another event handler? - c#

Here is the short sample code:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
button1_Click(object sender, EventArgs e); //can I call button1 event handler?
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(txtbox1.Text);
}
I wonder if it would be okay to code in the above way?

You can do that - although the code you provide can't be compiled. It should look like this:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
button1_Click(sender, e);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(txtbox1.Text);
}
But for best practice and code readability, you're probably better off doing this, especially as you are not making use of sender and e:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
ShowMessageBox();
}
private void button1_Click(object sender, EventArgs e)
{
ShowMessageBox();
}
private void ShowMessageBox()
{
MessageBox.Show(txtbox1.Text);
}

Yes you can do that; an event handler is just another method.
However it might be worth creating a new method that shows the message box, and having both Click event handlers call that:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
ShowTextboxMessage();
}
private void button1_Click(object sender, EventArgs e)
{
ShowTextboxMessage();
}
private void ShowTextboxMessage()
{
MessageBox.Show(txtbox1.Text);
}

An event handler is nothing more than a method, so you can call it like any other.

Related

How to perform Click-Event on a hidden button

In my C# form I have two buttons
button1.Hide()
private void button2_Click(object sender, EventArgs e)
{
button1.PerformClick();
}
The button1 is hidden at form loading, I want the logic behind button1 to be perfomed when it's hidden too.
Just let the function outside become another function, then you can call function although you hidden the button1.
private void button1(object sender, EventArgs e)
{
_button1();
}
private void button2(object sender, EventArgs e)
{
_button1();
}
//Here is the function
void _button1()
{
...
}
If your Button is hidden, it seems that you need the functionality behind not or just in special cases. Keeping functionality out of events is often a simple solution to avoid problems in the future.
private void btn_Reload_Click(object sender, EventArgs e)
{
// reload here - maybe you reload all your employees from a datasource
}
private void btn_Reload_With_Calculation_Click(object sender, EventArgs e)
{
// you can use functionality here from a another button and call the
btn_Reload_Click(this, EventArgs.Empty); // DON'T DO THIS IN MY OPINION
// ....
}
Maybe this solution is better even if you need the functionality at other workflows.
private void btn_Reload_Click(object sender, EventArgs e)
{
Reload();
}
private void btn_Reload_With_Calculation_Click(object sender, EventArgs e)
{
Reload();
Calculate();
}
void Reload() { }
void Calculate() { }

How do I call the PerformClick() function in C#?

I have three different buttons but now I want to call each even at the same time using one button.
I am unable to find the button.PerformClick() option, instead I have button.PerformLayout().
This is what I have:
private void ButtonTagStart_Click(object sender, EventArgs e)
{
//Something
}
private void ButtonLoadxHTMLTags_Click(object sender, EventArgs e)
{
//Do Something
}
private void btnStrt_Click(object sender, EventArgs e)
{
//Do Something
}
I want:
private void Button2_Click(object sender, EventArgs e)
{
ButtonTagStart.PerformClick();
ButtonLoadxHTMLTags.PerformClick();
btnStrt.PerformClick();
}
What shall I do?
you can create a method for each button
private void Something1(){}
private void Something2(){}
private void Something3(){}
private void ButtonTagStart_Click(object sender, EventArgs e)
{
Something1();
}
private void ButtonLoadxHTMLTags_Click(object sender, EventArgs e)
{
Something2();
}
private void btnStrt_Click(object sender, EventArgs e)
{
Something3();
}
in the last button invoke them all:
private void Button2_Click(object sender, EventArgs e)
{
Something1();
Something2();
Something3();
}

Clicked 2 Button At Once

Hello :) I Actually want to ask something
So my aim here is "If I click the button 1, the button 2 will be clicked too "
"Is it possible ? Click a one button so the other button will be clicked ?"
Here is my Code:
Button btn1 = sender as Button;
if (btn1 == button1){
button2.PerformClick();
}
It actually does not work it seems there is something wrong
I suggest extracting methods.
Before:
private void button1_Click(object sender, EventArgs e)
{
Routine 1 code ...
Routine 2 code ... // <- do not copy yourself; copy + paste is evil!
}
private void button2_Click(object sender, EventArgs e)
{
Routine 2 code ...
}
After:
//TODO: think over the right name
private void Routine1()
{
Routine 1 code ...
}
//TODO: think over the right name
private void Routine2()
{
Routine 2 code ...
}
...
private void button1_Click(object sender, EventArgs e)
{
Routine1();
Routine2();
}
private void button2_Click(object sender, EventArgs e)
{
Routine2();
}
It is very simple.
private void button1_Click(object sender, EventArgs e)
{
if ((sender as Button) == button1)
{
button2_Click(sender, e);
}
}
private void button2_Click(object sender, EventArgs e)
{
}
Unless you have a weird reason for doing this, don't!
You should prefer something like this :
void button1_Click(object sender, EventArgs e)
{
DoWork1();
DoWork2();
}
void button2_Click(object sender, EventArgs e)
{
DoWork2();
}

Can't put more than one number in the textbox. in my Simple Calculator program

Need help in making a simple calculator. i can't put more than one number in my calculator's textbox. Everytime i put a second number it replaces the first one need help!
I can't exceed more than one input number in my Calculator's Textbox instead it replaces the first number with a second number input
namespace Calculator_Project
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void InputOutputArea_TextChanged(object sender, EventArgs e)
{
}
private void One_Click(object sender, EventArgs e)
{
int Input = 1;
InputOutputArea.Text = Input.ToString();
}
private void Two_Click(object sender, EventArgs e)
{
int Input = 2;
InputOutputArea.Text = Input.ToString();
}
private void Three_Click(object sender, EventArgs e)
{
}
private void Four_Click(object sender, EventArgs e)
{
}
private void Five_Click(object sender, EventArgs e)
{
}
private void Six_Click(object sender, EventArgs e)
{
}
private void Seven_Click(object sender, EventArgs e)
{
}
private void Eight_Click(object sender, EventArgs e)
{
}
private void Nine_Click(object sender, EventArgs e)
{
}
private void Eql_Click(object sender, EventArgs e)
{
}
private void AddB_Click(object sender, EventArgs e)
{
}
private void Minus_Click(object sender, EventArgs e)
{
}
private void MultiplyB_Click(object sender, EventArgs e)
{
}
private void DivideB_Click(object sender, EventArgs e)
{
}
private void Zero_Click(object sender, EventArgs e)
{
}
private void ResetB_Click(object sender, EventArgs e)
{
InputOutputArea.Clear();
}
}
}
You should use
InputOutputArea.Text += Input.ToString();
(note the '+') in order to append to a text box.
private void Two_Click(object sender, EventArgs e)
{
int Input = 2;
InputOutputArea.Text += Input.ToString();
}
You must use += to add other text to next of first text
Here is your problem:
InputOutputArea.Text = Input.ToString();
This replaces the content of the textbox instead of adding to it.
InputOutputArea.Text += Input.ToString();
the above code should do as you ask.
Good to remember is that concatenating strings with + is rather inefficient, so don't do this in performance critical code unless absolutely necessary. In those cases a String-builder is almost always better.
Every answers talking about the Concatenation of the previous text with the current, But I would like to suggest something more than that;
You need not to create separate event handlers for all your buttons that are doing same tasks, Hope that the Text of each button will be the number that you need to display in the textBox(say btnOne will holds 1 and btnTwoholds 2 and so on). By make use of this Text we can reuse the handlers like the following, Let btnNumber_Click be the handler and which is defined like the following:
private void btnNumber_Click(object sender, EventArgs e)
{
Button currentButton = sender as Button;
InputOutputArea.Text += currentButton.Text;
}

More effective way to implement big amount of events

I am new to Visual C#, I am implementing a WPF application but I want to use a more effective way for events since I have another method for every single tool.
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
KeyUp(textBox1, e);
}
private void textBox2_KeyUp(object sender, KeyEventArgs e)
{
KeyUp(textBox2, e);
}
private void textBox3_KeyUp(object sender, KeyEventArgs e)
{
KeyUp(textBox3, e);
}
private void textBox4_KeyUp(object sender, KeyEventArgs e)
{
KeyUp(textBox4, e);
}
private void textBox5_KeyUp(object sender, KeyEventArgs e)
{
KeyUp(textBox5, e);
}
private void TextChanged(int x, TextBox txt)
{
int i = dataGridView1.CurrentCell.RowIndex;
dataGridView1.Rows[i].Cells[x].Value = txt.Text;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
TextChanged(1, textBox2);
}
private void textBox3_TextChanged_1(object sender, EventArgs e)
{
TextChanged(2, textBox3);
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
TextChanged(3, textBox4);
}
private void textBox5_TextChanged_1(object sender, EventArgs e)
{
TextChanged(4, textBox5);
}
and so on. Instead of occupying so much lines, I am looking for a shorter way for all these events. Seems like I have to use Mapping, but I could not manage it. Any help would be highly appreciated
You could use one event handler for all textbox keyup events as follows;
private void textBox_KeyUp(object sender, KeyEventArgs e)
{
KeyUp((TextBox)sender, e);
}
you could use same kind of logic for rest of the events once you get the idea.
You need two event handlers (they are reusable you know)
First:
private void OnTextBoxKeyUp(object sender, KeyEventArgs e)
{
KeyUp((Textbox)sender, e);
}
Second:
private void OnTextChanged(object sender, EventArgs e)
{
var textBox = (Textbox)sender;
var i = dataGridView1.CurrentCell.RowIndex;
var x = (int)textbox.Tag
dataGridView1.Rows[i].Cells[x].Value = textBox.Text;
}
For the second part you need to set the FrameworkElement.Tag property in code like so:
<Textbox Tag="1" />
For completeness sake here is the xaml part for your textboxes:
<Textbox x:Name="textBox1" Tag="1" TextChanged="OnTextChanged" KeyUp="OnTextBoxKeyUp"/>
<Textbox x:Name="textBox2" Tag="2" TextChanged="OnTextChanged" KeyUp="OnTextBoxKeyUp"/>
And so on. Note that x:Name part is probably not needed as you don't need to reference the textboxes by name in the code behind with this solution.
First give your textboxes name as
TextBox1, TextBox2, TextBox3 ... and add a common textBoxAll_TextChanged event handler for all textboxes.
Then execute the below code :
private void textBoxAll_TextChanged(object sender, EventArgs e)
{
var tb = sender as TextBox;
int x = int.Parse(tb.Name.Substring(7,1)) - 1;
TextChanged(x, tb);
}

Categories

Resources