themightymanuel
themightymanuel

Reputation: 135

Create an event handler for control created programmatically

I am working on a WPF application and what I want is if a specific Radio Button from group A is selected the program creates a second set of Radio Buttons call them group B and at the same time a disabled textbox. Now I want to create an event so if the user Selects option 3 from Group B the text box becomes enabled and when it is unselected it disables again. Now I have the working programmatically but when I try to create the event handlers I am referencing a non-existent control and it won't build. I am trying to use this.textBox.IsEnabled = true/false; to enable/disable the textbox. So just to make sure the actual question is clear. How can I enable/disable a textbox which doesn't exist at build/runtime?

My Events:

private void AllowRegion(object sender, RoutedEventArgs e)
        {
            this.SpecRegionText.IsEnabled = true;
        }
private void DisallowRegion(object sender, RoutedEventArgs e)
        {
            this.SpecRegionText.IsEnabled = false;
        }

Creating the controls:

private void AddSpecificControls()
        {
            Grid grid = (Grid)this.SpecsGrid;
            RadioButton recruitmentEnabled = (RadioButton)this.RecruitmentEnabled;
            if ((bool)recruitmentEnabled.IsChecked)
            {
                RadioButton newNations = new RadioButton();
                newNations.Margin = new Thickness(10, 10, 0, 0);
                newNations.HorizontalAlignment = HorizontalAlignment.Left;
                newNations.Name = "NewNations";
                newNations.GroupName = "RecruitType";
                newNations.Content = "New Nations";
                newNations.Width = 124;
                grid.Children.Add(newNations);
                RadioButton reFound = new RadioButton();
                reFound.Margin = new Thickness(10, 30, 0, 0);
                reFound.HorizontalAlignment = HorizontalAlignment.Left;
                reFound.Name = "Refound";
                reFound.GroupName = "RecruitType";
                reFound.Content = "Refounded Nations";
                reFound.Width = 124;
                grid.Children.Add(reFound);
                RadioButton specRegionRadio = new RadioButton();
                specRegionRadio.Margin = new Thickness(10, 50, 0, 0);
                specRegionRadio.HorizontalAlignment = HorizontalAlignment.Left;
                specRegionRadio.VerticalAlignment = VerticalAlignment.Top;
                specRegionRadio.Name = "SpecRegionRadio";
                specRegionRadio.GroupName = "RecruitType";
                specRegionRadio.Content = "Specific Region";
                specRegionRadio.Width = 124;
                specRegionRadio.Height = 23;
                specRegionRadio.Checked += new RoutedEventHandler(AllowRegion);
                specRegionRadio.Unchecked += new RoutedEventHandler(DisallowRegion);
                grid.Children.Add(specRegionRadio);
                TextBox specRegionText = new TextBox();
                specRegionText.Margin = new Thickness(139, 50, 0, 0);
                specRegionText.HorizontalAlignment = HorizontalAlignment.Left;
                specRegionText.VerticalAlignment = VerticalAlignment.Top;
                specRegionText.Name = "SpecRegionText";
                specRegionText.Text = "Region";
                specRegionText.Width = 120;
                specRegionText.Height = 23;
                specRegionText.IsEnabled = false;
                grid.Children.Add(specRegionText);
            }
        }

Upvotes: 0

Views: 177

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81243

You can hook delegate inline using lambda but for that you have to declare textBox before radioButton.

TextBox specRegionText = new TextBox();
RadioButton specRegionRadio = new RadioButton();
specRegionRadio.Checked += (s,e) => specRegionText.IsEnabled = true;
specRegionRadio.Unchecked += (s,e) => specRegionText.IsEnabled = false;

Upvotes: 1

Related Questions