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();
}
Related
I am extremely new to C# and any help would be appreciated. I am trying to create a simple multiple option menu with User Controls and I have got it in a semi functional state. I am trying to get the user controls to show in a certain location because as of now they are only showing from the top left down and that is not where I would like them. Any help would be appreciated. My Form1 code is below:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Menu_V2
{
public partial class Form1 : Form
{
StartScreen u1;
Softaim u2;
Triggerbot u3;
Macro u4;
Misc u5;
public static bool flag = true;
public Form1()
{
u1 = new StartScreen();
u2 = new Softaim();
u3 = new Triggerbot();
u4 = new Macro();
u5 = new Misc();
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void toolStripContainer1_ContentPanel_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void label5_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void label4_Click(object sender, EventArgs e)
{
u1.Hide();
u2.Hide();
u3.Hide();
u4.Hide();
u5.Show();
u5.Dock = DockStyle.Fill;
this.Controls.Add(u5);
}
private void insert(object sender, KeyEventArgs e)
{
}
private void bunifuThinButton21_Click(object sender, EventArgs e)
{
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F8)
{
}
}
private void button1_Click(object sender, EventArgs e)
{
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Insert)
{
this.Hide();
}
}
private void bunifuThinButton21_KeyUp(object sender, KeyEventArgs e)
{
}
private void bunifuThinButton22_Click(object sender, EventArgs e)
{
}
private void bunifuThinButton22_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F8)
{
this.Show();
}
}
private void misc1_Load(object sender, EventArgs e)
{
}
private void label6_Click(object sender, EventArgs e)
{
u1.Hide();
u2.Hide();
u3.Hide();
u4.Show();
u5.Hide();
u4.Dock = DockStyle.Fill;
this.Controls.Add(u4);
}
private void button1_Click_1(object sender, EventArgs e)
{
}
private void startScreen1_Load(object sender, EventArgs e)
{
}
private void label2_Click_1(object sender, EventArgs e)
{
u1.Hide();
u2.Show();
u3.Hide();
u4.Hide();
u5.Hide();
u2.Dock = DockStyle.Fill;
this.Controls.Add(u2);
}
private void label3_Click(object sender, EventArgs e)
{
u1.Hide();
u2.Hide();
u3.Show();
u4.Hide();
u5.Hide();
u3.Dock = DockStyle.Fill;
this.Controls.Add(u3);
}
}
}```
I have been able to get my exit button to work, but none of the others. I tried coding the other buttons the same way, but I get an error. The program also keeps restarting, even after I removed that code. Can someone tell me where I am going wrong on the other buttons and the restart problem?
namespace Module10Project
{
public partial class frmRadioStar : Form
{
public frmRadioStar()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
txtBx1.Text = "";
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
txtBx2.Text = "";
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
listView1.Text = "";
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void rdoAdd_CheckedChanged(object sender, EventArgs e)
{
}
private void btnCalc_Click(object sender, EventArgs e)
{
}
private void btnReset_Click(object sender, EventArgs e)
{
{
Application.Reset();
}
}
private void rdoSub_CheckedChanged(object sender, EventArgs e)
{
}
}
}
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;
}
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)]);
}
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.