Click here to Skip to main content
15,908,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, i am trying to reference buttons which are named Btn1, Btn2, Btn3.... by converting the names of the button pressed into int and adding a number to get the button i want and then adding one more tag to the already existing tags on that button, help would be much appreciated.

What I have tried:

private void Btn_clicked(object sender, RoutedEventArgs e)
{

    if (sender is Button btn)
    {
             string SelectButton = btn.Name.Remove(0, 3);
             //removes the Btn part and leaves the int

             int BtnNum = Convert.ToInt32(SelectButton);
             var Destination = Convert.ToString(BtnNum + 8);
             Destination.Tag = Destination.tag + "active";
    }

}
Posted
Updated 18-Apr-20 6:06am

Instead of faffing about like that, create a collection in your Form class and have it hold the buttons - an array, or List<Button> would do.

Then all you have to do is find the button in the collection, and you're off.
C#
private List<Button> buttons = new List<Button>();
private void Btn_clicked(object sender, RoutedEventArgs e)
    {
    if (sender is Button btn)
        {
        int index = buttons.IndexOf(btn);
        Button destination = buttons[(index + 8) % buttons.Count];
        destination.Tag += "active";
        }
    }
And it doesn't depend on the name of the button - so your code can be more readable and maintainable in future as well.
 
Share this answer
 
Comments
QuantumNova 19-Apr-20 2:19am    
Hi, this is great thanks. But just out of curiosity, would there be a way to refer to a button directly using its name in string form as i have tried above?
OriginalGriff 19-Apr-20 3:20am    
You can, but it's a lot of faffing, and it's horribly easy to break with other changes in your code later.
You could do it - just recursively traverse the Controls array looking for a Control that is a Button that has a specific Name property.
But ... add another button in the middle, or change the name of a button to improve your self documentation, or even obfuscate your code and there is a good risk of it breaking. That's why I'd suggest a collection - it's much clearer what you are trying to do, and more reliable / maintainable.
QuantumNova 19-Apr-20 3:26am    
Ok thanks, will use the method you provided then, much apprciated :)
OriginalGriff 19-Apr-20 3:48am    
You're welcome!
Maciej Los 19-Apr-20 5:24am    
5ed!
Assuming what you really want is to keep track of the current active Button:
C#
private Button ActiveButton = null;

private void Buttons_Click(object sender, EventArgs e)
{
    ActiveButton = sender as Button;

    if (ActiveButton is null) throw new NullReferenceException("that's not a Button");

    // calculate the nexf Button ?
}
To go beyond this, I would need to know more about what the buttons do. Can more than one Button be active ? Right now, your code could keep adding "active" to a button tag with each click: that makes no sense.

If a Button loses focus, do you need to track that in code ?

Suggestion: use a Dictionary:
Dictionary<string, Button> BtnNameToButton = new Dictionary<string, Button>();

private void Form1_Load(object sender, EventArgs e)
{

    foreach (Button btn in this.Controls.OfType<Button>())
    {
        BtnNameToButton.Add(btn.Name, btn);
    }
}
 
Share this answer
 
v2
Comments
QuantumNova 19-Apr-20 2:15am    
No, I have used "active" as a tag just to tell me which i can use later on in the program, it is only a placeholder at the moment, the main thing i would like to know is how i would refer to a button using its name in string form and add a additional tag to that button. If you could help out, it would be much appreciated :)
BillWoodruff 19-Apr-20 2:19am    
see revised post.
QuantumNova 19-Apr-20 2:47am    
Thanks, Much appreciated :)
BillWoodruff 19-Apr-20 2:49am    
sure. still not clear what your intent is in modifying button tags.
Maciej Los 19-Apr-20 5:24am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900