More effective way to implement big amount of events - c#

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);
}

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() { }

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;
}

How to execute the same code for different events

I have to work with touch monitors and sometimes with mouse and normal monitors.
So for drag and drop the for the first would be
private void lvAllowedPPtab2_StylusButtonDown(object sender, StylusButtonEventArgs e)
and for the second
private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
after that I have to execute the same code using sender and e.
I didn't get to make a common code routine.
The two event are similar and both have the GetPosition event.
I might have taken the wrong road but I have tought to something like:
Type eventType;
if (_e is StylusButtonEventArgs)
eventType = typeof (StylusButtonEventArgs);
else
eventType = typeof(MouseEventArgs);
but then I don't know how to cast e to event type.
Thank you
you can call them both with that
private void listView_StylusButtonDown(object sender, StylusButtonEventArgs e) { CommonCode(sender, e); }
private void listView_PreviewMouseLeftButtonDown(object sender, MouseEventArgs e) { CommonCode(sender, e); }
and then tell inside common code
private void CommonCode(object sender, object _e)
{
//Sender is common
ListView parent = (ListView)sender;
string strListViewButtonName = (sender as ListView).Name;
if (_e is StylusButtonEventArgs)
... (_e as StylusButtonEventArgs).GetPosition(parent));
else
... (_e as MouseEventArgs).GetPosition(parent));
}
Better implementation (thanks to Eli Arbel):
private void listView_StylusButtonDown(object sender, StylusButtonEventArgs e) { CommonCode(sender, e.GetPosition((ListView)sender)); }
private void listView_PreviewMouseLeftButtonDown(object sender, MouseEventArgs e) { CommonCode(sender, e.GetPosition((ListView)sender)); }
private void CommonCode(object sender, Point p)
{
//Sender is common
ListView parent = (ListView)sender;
string strListViewButtonName = (sender as ListView).Name;
//you don't need getPosition since P is known
}

Dynamic method in c#

I have many labels on the form, and every label invokes same method with different argument(which belongs to label text/name). Here is the code:
//"res" is an array
private void label1_Click(object sender, EventArgs e)
{
checkresult(res[0]);
}
private void label2_Click(object sender, EventArgs e)
{
checkresult(res[1]);
}
private void label3_Click(object sender, EventArgs e)
{
checkresult(res[2]);
}
private void label4_Click(object sender, EventArgs e)
{
checkresult(res[3]);
}
private void label5_Click(object sender, EventArgs e)
{
checkresult(res[4]);
}
private void label6_Click(object sender, EventArgs e)
{
checkresult(res[5]);
}
private void label7_Click(object sender, EventArgs e)
{
checkresult(res[6]);
}
private void label8_Click(object sender, EventArgs e)
{
checkresult(res[7]);
}
private void label9_Click(object sender, EventArgs e)
{
checkresult(res[8]);
}
I just want to precise my code by defining only one method for all labels. How can i do it?
A pseudocode may look like this:
label1.Click += label_Click(object sender, EventArgs e);
label2.Click += label_Click(object sender, EventArgs e);//SAME HANDLER
label3.Click += label_Click(object sender, EventArgs e);//SAME HANDLER
....
and after
private void label_Click(object sender, EventArgs e)
{
if(sender == label1)
checkresult(res[0]);
else if(sender == label2)
checkresult(res[1]);
...
...
}
First let all of your labels use the same Label_Click event.
private void Label_Click(object sender, EventArgs e)
{
Label temp = sender as Label;
if (temp != null)
{
string labelName = temp.Name;
string labelId = labelName.Substring(5, labelName.Length);
int id = int.Parse(labelId) - 1;
checkresult(res[id]);
}
}
You could set anonymous delegates in when you make the event handler
label1.Click += (s,e) => {checkresult(res[0]); };
label2.Click += (s,e) => {checkresult(res[1]); };
label3.Click += (s,e) => {checkresult(res[2]); };
In WinForms, set your Index to Tag of Label and set each OnClick event to same EventHandler
private void lbl_Click(object sender, EventArgs e)
{
checkresult(res[Convert.ToInt32((sender as Label).Tag)]);
}

calling a event handler within another event handler?

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.

Categories

Resources