0

I am currently making searching platform for materials, and I want to make a button with periodic table. what I am doing now is to make every button for the periodic table, and making every click events for each button.

public void bc(string a)
{
    if (MainEntry.Text == "")
    {
        MainEntry.Text = a;
    }
    else
    {
        MainEntry.Text = MainEntry.Text + "," + a;
    }
}
private void buttonLi_Click(object sender, EventArgs e)
{
    bc("Li");
}
private void buttonBe_Click(object sender, EventArgs e)
{
    bc("Be");
}

I wonder if there are another option for this. such as, if I press the button, system reads what is written on the button, and use the text on the method. So that I can decrease my code lines.

6
  • 3
    Are you using winform ? Commented Jan 29, 2020 at 14:43
  • Start from extracing model (data): let have a single collection of elements. Do not hardcode bc("Li"); Commented Jan 29, 2020 at 14:44
  • You can use the same method as an event handler. sender is the button itself so you can modify your code based on that. Eg, you can use the Text to decide what to send based on the button's text, or the Tag property. All controls have a Tag property that's used to store extra info Commented Jan 29, 2020 at 14:46
  • 2
    The best option is Tag - it's available everywhere, doesn't force you to use a specific Text and far more important, doesn't break localization Commented Jan 29, 2020 at 14:52
  • @Cid Yes, I am using windows form :) Commented Jan 29, 2020 at 15:23

3 Answers 3

2

Yes, you can simply call the same onclick method for every button and get the text within the Onclick Event. Something like this:

private void button_Click(object sender, EventArgs e)
{
    Button button = sender as Button;

    bc(button.Text);
}
Sign up to request clarification or add additional context in comments.

4 Comments

If, for some reasons, the method is used on something else than a Button, You'll get a NullReferenceException
Text is a property of Control
Thank you for your help! your answer helped me and saved my time alot!
@JaehoSong not yet - using Text prevents you from localizing the form. If you do, you'll have to look up the localized label each time to find out which button was used. Use Tag instead
2

You can use the Sender object, but first you need to subscribe all button clicks to the one event, something like this:

public Form1()
{
    InitializeComponent();
    buttonLi.Click += button_Click;
    buttonBe.Click += button_Click;
}

private void button_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    bc(btn.Text);
}

The Sender contains a reference to the control/object that raised the event.

6 Comments

But first you need to listen toClick event all buttons in designer. You can do this with two way, first; with UI Designer select to Button and go to Events tab in Properties, found the Click event and select Event or on form load you can write code something like; buttonLi.Click += Element_Click; buttonBe.Click += Element_Click;
@UmutÇömlekçioğlu you can pick or specify an available event handler in the designer, or explicitly add the handler. In both cases the same code is emmitted
Thank you for your answer! You both really helped me I really do appreciate it
@SalahAkbari Thank you :) But I chose other person's coding since I didn't need initialization and I thought it does fit on my case more. But really thank you for teaching new-bie
@JaehoSong I didn't intent to show you how can you use initialization, I just was trying to show that all of your buttons need to listen to the same event handler in order you can use the sender object in it's right order. Exactly like what Umut said in his comment but this is the code alternative for his suggestion. :)
|
1

I would use the Tag property of a control.

Something like:

private void control_Click(object sender, EventArgs e)
{
    var control = (Control)sender;

    bc(control.Tag.ToString());
}

This way you don't have to create an eventhandler for each button. And using the Tag will offer that the Text that is shown on the button don't have to be equal.


Use (Control)sender over sender as Control, because if something is wrong you don't want the exception "NullReferenceException" but the real exception what went wrong. (Like invalid casts exceptions)


Select the button on the form

Form1 with button

Fill the tag with the right value

Tag property

6 Comments

Tag is a property of Control. By casting to Control you can use this code with every control
A huge benefit of Tag vs Text is that Text will change when the form is localized. Tag doesn't affect how the button or the rest of the form works though
Yep, thats what I had in mind also. I never use Text/Captions values back in code, because of Culture stuff. If someone changes a caption, the application will break otherwise.
@JeroenvanLangen Thank you for your kind comment, I got an error message from it, (System.Windows.Forms.Control.Tag.get returned null.) but I will figure it out myself :) Thank you for your time
@JaehoSong you need to set the Tag first.. I'll update my answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.