Activating / deactivating buttons from a C # config file (WinForms)

Let's say that my program has a maximum of 8 buttons and I should be able to turn them on / off (visible / hidden) according to my settings in the config file. My selected buttons should then be visible on two lines (if greater than 4) or one line (cnt <= 4) in the form, leaving no gap between them. (I mean, when 1,2,3,4 are active, they will be on the first line the same 1,2,5,8 is active, so 5 will go from button 3 and 8 will take 4th place)

<add key="butactive" value="1;3;4;8"/>
<add key="but1" value="START"/>
<add key="but2" value="END"/>
<add key="but3" value="PAUSE"/>
<add key="but4" value="RESET"/>
...

      

The program should be able to determine from the "butactive" key which of these buttons will be visible, and then change its Text property and add a specific action, which is also taken from the configuration file.

 private void Form2_Load(object sender, EventArgs e)
 {
     radButton1.Text = ConfigurationManager.AppSettings["but1"];
     radButton2.Text = ConfigurationManager.AppSettings["but2"];
     radButton3.Text = ConfigurationManager.AppSettings["but3"];
     radButton4.Text = ConfigurationManager.AppSettings["but4"];
     ...
     radButton1.Click += getAction(ConfigurationManager.AppSettings["but1a"]);
     radButton2.Click += getAction(ConfigurationManager.AppSettings["but2a"]);
     ...
 }

      

Any idea how to select the right buttons when the form loads and place them in the right positions?

+3


source to share


2 answers


You can use FlowLayoutPanel as a container for your buttons. If you're evaluating correctly, all you need to do is set the buttons to be visible and they will be positioned as needed:

radButton1.Visible = (bool)ConfigurationManager.AppSettings["butactive1"];
radButton2.Visible = (boll)ConfigurationManager.AppSettings["butactive2"];
...

      



As far as the action is concerned, you can use the same event handler for all buttons and do whatever you want based on the button you pressed and what actions you defined:

radButton1.Click += buttonClickHandler;
radButton2.Click += buttonClickHandler;

private void buttonClickHandler(object sender, EventArgs e)
{
    var button = sender as Button;
    var action = getAction(ConfigurationManager.AppSettings[button.Text + "a"];

    //Execute whatever you want here based on action
}

      

+1


source


This piece of code you have there is not very good, you are creating dependencies on config names with hardcoded strings, which is not good practice at all if you can access the .Default properties. I suggest you go this way.

For the position of the buttons, you can randomly navigate with the positions, for example you can switch to 3 and 5 position buttons like this in window forms:

radButton5.Position = radButton3.Position

      



But , which is not a good approach , I advise you to shorten this dependency in button numbers, you can for example create all hidden buttons and always show the first buttons, the "getAction" method will be of great help in this approach and it will not be difficult to implement the behavior.

The Scratch (pseudocode) of what you should be doing would be something like this:

 // Get the active buttons from configuration (eg var will be a list of the active buttons names)
 var activeButtons = getActiveButtons();
 var textBoxNumber = 0
 foreach(var button in activeButtons)
 {
    textBoxNumber++;
    TextBox tbx = this.Controls.Find("radButton" + textBoxNumber.ToString(), true).FirstOrDefault() as TextBox;

    if(tbx != null)
       tbx.Text = ConfigurationManager.AppSettings[button];
 }

      

+1


source







All Articles