Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C# button's name reference of an action

Status
Not open for further replies.

dann11

Full Member level 3
Joined
Oct 22, 2015
Messages
166
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Activity points
2,742
I am trying to control a a counter with button using c# development. I have set different buttons for different counter and has an access authorization first before the counter starts counting. I have already made the authorization and it works but I have problems with buttons. I used the button name's to identify which button is clicked and which counter must operate.

Code:
public void modeOfCounter()
{
//Conditon if button1(main start button) is clicked
if (ss.Name == "button1")
{
counterA.start();
counterB.start();
counterC.start();
counterD.start();
targetSetting.Enabled = false;


button1.Enabled = false;
//disable the rest of the startbutton
aStartBtn.Enabled = false;
bStartBtn.Enabled = false;
cStartBtn.Enabled = false;
//saveButton.Enabled = false;
resetButton.Enabled = false;
}

if (ss.Name == "bc288StartBtn")
{
counterA.start();
targetSetting.Enabled = false;

aStartBtn.Enabled = false;
bStartBtn.Enabled = false;
cStartBtn.Enabled = false;
//saveButton.Enabled = false;
resetButton.Enabled = false;
button1.Enabled = false;	

}
}

I have declared ss as an object. and it keep on giving me error "Object reference not set to an instance of an object."

I believe it has something to do with the object declaration of the ss, how do you think should I declare the button if I will be using its name as a reference of an action?
 
Last edited by a moderator:

if you have a number of Buttons which call an event handler when they are clicked you can identify which Button caused the event
Code:
        private void ButtonClick(object sender, EventArgs e)
        {
            Button clickedButton = (Button)sender;
            textBox1.Text= clickedButton.Name;
displays the Button Name in the TextBox
 
  • Like
Reactions: dann11

    V

    Points: 2
    Helpful Answer Positive Rating

    dann11

    Points: 2
    Helpful Answer Positive Rating
if you have a number of Buttons which call an event handler when they are clicked you can identify which Button caused the event
Code:
        private void ButtonClick(object sender, EventArgs e)
        {
            Button clickedButton = (Button)sender;
            textBox1.Text= clickedButton.Name;
displays the Button Name in the TextBox


yes that is absolutely right, all buttons will call a single event. Does the code means every button will have to be coded as such? or do I have to use an if statement?
 

you can use an if comparing with the button name
Code:
 if (clickedButton.Name == "button1") textBox1.Text = "Button 1 selected";
    else
       if (clickedButton.Name == "button2") textBox1.Text = "Button 2 selected";
or the button text displayed on the form
Code:
 if (clickedButton.Text == "Button 1") textBox1.Text = "Button 1 selected";
     else
         if (clickedButton.Text == "Button 2") textBox1.Text = "Button 2 selected";
I tend to give the button meaningful names ("switchOnMotor" rather than "button1") then check the name in an if() or a switch, e.g.
Code:
switch (clickedButton.Name)
       {
        case "button1": textBox1.Text = "Button 1 selected"; break;
        case "button2": textBox1.Text = "Button 2 selected"; break;
         }
 
  • Like
Reactions: dann11

    V

    Points: 2
    Helpful Answer Positive Rating

    dann11

    Points: 2
    Helpful Answer Positive Rating
I have written the code as below, but it has an error of "No overload for method 'buttonClicked' takes 0 arguments", please also help me with this. How should it be, if there is really no value to be written at the function?

Code:
public void buttonClicked(object sender, EventArgs e)
{
    Button buttonclicked = (Button) sender:

    if(buttonclicked.Name == "Button1")
      {
        counterA.start();
        counterB.start();
        counterC.start();
        counterD.start();
        targetSetting.Enabled = false;
        button1.Enabled = false;
        aStartBtn.Enabled = false;
        bStartBtn.Enabled = false;
        cStartBtn.Enabled = false;
        saveButton.Enabled = false;
        resetButton.Enabled = false;
       }

        if(buttonclicked.Name == "bc288StartBtn")
        {
        counterA.start();
        targetSetting.Enabled = false;
        aStartBtn.Enabled = false; 
        bStartBtn.Enabled = false;
        cStartBtn.Enabled = false;
       //saveButton.Enabled = false;
       resetButton.Enabled = false;
       button1.Enabled = false;
         }
}
 

  • Like
Reactions: dann11

    dann11

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top