1

i am working on a project where a new button is created x times (for the sake of this question: x is defined by a user input at the moment. In the final version this will be handled by the amount of entries in an SQL-Databank). These buttons then go into a ScrollViewer. I need every single dynamically created button to have a unique event on click. So if button 1 is clicked, event 1 is being executed and so on... This of course means that if for instance 5 buttons are created, every single one of them needs to trigger their own function. The names of the functions could be named BtnClick + i. So what would be a viable solution to that? Could not get the EventHandler to work the way i need it to.

Here is the code:

    private void TextEnter(object sender, KeyEventArgs e)
    {
        if (Keyboard.IsKeyDown(Key.Enter))
        {
            TextBox testBox = (TextBox)sender;
            int entry = Int32.Parse(testBox.Text);
            Console.WriteLine(entry);

            for (int i = 1; i < entry + 1; i++)
            {
                Button testBtn = new Button();
                testBtn.Content = i;
                testBtn.FontSize = 20;
                testBtn.Foreground = new SolidColorBrush(Colors.White);
                testBtn.Width = ListPanel.ActualWidth;
                testBtn.Height = 60;
                testBtn.FontWeight = FontWeights.Bold;
                testBtn.Background = new SolidColorBrush(Colors.Transparent);
                testBtn.Click += new EventHandler();
                ListPanel.Children.Add(testBtn);
            }
1

1 Answer 1

2

You could use the same event handler and switch on the Button's Content:

private void TextEnter(object sender, KeyEventArgs e)
{
    if (Keyboard.IsKeyDown(Key.Enter))
    {
        ...
        for (int i = 1; i < entry + 1; i++)
        {
            Button testBtn = new Button();
            testBtn.Content = i;
            testBtn.FontSize = 20;
            testBtn.Foreground = new SolidColorBrush(Colors.White);
            testBtn.Width = ListPanel.ActualWidth;
            testBtn.Height = 60;
            testBtn.FontWeight = FontWeights.Bold;
            testBtn.Background = new SolidColorBrush(Colors.Transparent);
            testBtn.Click += TestBtn_Click;
            ListPanel.Children.Add(testBtn);
        }
    }
}

private void TestBtn_Click(object sender, RoutedEventArgs e)
{
    Button button = (Button)sender;
    int content = Convert.ToInt32(button.Content);
    switch (content)
    {
        case 1:
            //do something for the first button
            break;
        case 2:
            //do something for the second button...
            break;
    }
}

Or create the event handler using an anonymous inline function:

testBtn.Click += (ss,ee) => { ShowPage(i); };
Sign up to request clarification or add additional context in comments.

10 Comments

Yeah seems good. But in theory 1000 buttons could be created. Hardcoding all the cases is not something i look forward to. Is there any way of creating a new case for the amount of buttons?
So instead of using a switch statement in a single method you want to create 1000 different methods...? Hmm. Why do you even need to handle each button click differently?
What's the difference between clicking on button 1 and button 900 for example?
Each button in the end will show a different page containing different sets of data. In the end, the user can add a new entry containing a name, date and so on. This data will then be saved to an sql server. When the program is started, it checks how many of these entries are in the databank and create an adequate amount of buttons - one for each. When clicking the button, a page is revealed that shows the data of that entry saved in the databank.
Use an anonymous inline function then? See my edit?
|

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.