1

I am creating a menu with big buttons containing an image, and text. When selected a border is around the button.

The button text is not always the same, and the result of the button click neither.

I have the image name, and text per button set in a struct like this: (there are four of them, but i'll show 2)

struct ConfigDevSubmenu
    {

        public const string SubMenuBtnText1 = "";
        public const string SubMenuBtnText2 = "text submenu 3 button 1";
        public const string SubMenuBtnText3 = "text submenu 3 button 2";
        public const string SubMenuBtnText4 = "";
        public const string SubMenuBtnImg1 = null;
        public const string SubMenuBtnImg2 = "Settings.png";
        public const string SubMenuBtnImg3 = "LoadFirmware.png";
        public const string SubMenuBtnImg4 = null;
        public const string SubMenuBtnBorder1 = "Borderstyle.None";
        public const string SubMenuBtnBorder2 = "Borderstyle.FixedSingle";
        public const string SubMenuBtnBorder3 = "Borderstyle.FixedSingle";
        public const string SubMenuBtnBorder4 = "Borderstyle.None";
    }
    struct AdvancedSubmenu
    {
        public const string SubMenuBtnText1 = "text submenu 4 button 1";
        public const string SubMenuBtnText2 = "text submenu 4 button 2";
        public const string SubMenuBtnText3 = "text submenu 4 button 3";
        public const string SubMenuBtnText4 = "text submenu 4 button 4";
        public const string SubMenuBtnImg1 = "GenerateEncKey.png";
        public const string SubMenuBtnImg2 = "Monitoring.png";
        public const string SubMenuBtnImg3 = "AdvancedSettings.png";
        public const string SubMenuBtnImg4 = "GenerateConfigFile.png";
        public const string SubMenuBtnBorder1 = "Borderstyle.FixedSingle";
        public const string SubMenuBtnBorder2 = "Borderstyle.FixedSingle";
        public const string SubMenuBtnBorder3 = "Borderstyle.FixedSingle";
        public const string SubMenuBtnBorder4 = "Borderstyle.FixedSingle";
    }

I do not think this can be done much easier without using database files.

To create the buttons I have this function which has as argument the which struct it should use, and in a switch case structure each button is created. But I've found myself copy-pasting alot in these functions so this must be possible easier. Therefore I tried something like below, but that does not work. I'd like to know whether it is possible to make that work, and how I should do that.

    private void createButtons(string Struct)
    {
        for (int i = 1; i < 5; i++)
        {
            SubBtnText[i].Text = Struct.SubMenuBtnText[i];
            pictureBoxSubBtn[i].Image = Image.FromFile(Struct.SubMenuBtnImg[i]);
            panelSubBtn[i].BorderStyle = Struct.SubMenuBtnBorder[i];

        }
    }

Any suggetions?

2 Answers 2

1

Create a class to hold the button text, image name and border styles - say ButtonData.

Create several lists (or arrays) of ButtonData, one per menu.

You can then iterate over the lists and extract the data.

public class ButtonData
{
   public ButtonData(string text, string image, BorderStyle border)
   {
       Text = text;
       Image = image;
       Border = border;
   }

   public string Text { get; private set; }
   public string Image { get; private set; }
   public BorderStyle Border { get; private set; }
}

var devMenuData = new List<ButtonData> { 
                                  new ButtonData("", null, "Borderstyle.None"), 
                                  new ButtonData("text submenu 3 button 1",
                                                 "Settings.png",
                                                 Borderstyle.FixedSingle), 
                                  ...
                                       };

Your function would something like:

private void CreateButtons(IEnumerable<ButtonData> data)
{
    foreach (var buttonData in data)
    {
        SubBtnText[i].Text = buttonData.Text;
        pictureBoxSubBtn[i].Image = Image.FromFile(buttonData.Image);
        panelSubBtn[i].BorderStyle = buttonData.Border;
    }
}

The above amended function will not work as such, as .NET doesn't have control arrays. You could create another list/array to iterate over or index through for this to work.

Sign up to request clarification or add additional context in comments.

1 Comment

Some tweaks on this did the trick, I am not really sure this is better than I had, but I learned from it :)
0

reflection

class ButtonParameters
{
    public string SubMenuBtnText = string.Empty;
    public string SubMenuBtnImg = string.Empty;
    public string SubMenuBtnBorder = string.Empty;
}

public Dictionary<int, ButtonParameters> CreateParameters(Type type)
{
    FieldInfo[] fieldInfos = type.GetFields(
        BindingFlags.Public | BindingFlags.Static);
    Dictionary<int, ButtonParameters> parameters = new Dictionary<int, ButtonParameters>();
    foreach (FieldInfo fieldInfo in fieldInfos)
    {
        if (fieldInfo.Name.Contains("SubMenuBtnText"))
        {
            int index = Convert.ToInt32(fieldInfo.Name.Substring(14));
            if (!parameters.ContainsKey(index))
            {
                parameters.Add(index, new ButtonParameters());
            }
            parameters[index].SubMenuBtnText = (string)fieldInfo.GetValue(null);
        }
        else if (fieldInfo.Name.Contains("SubMenuBtnImg"))
        {
            int index = Convert.ToInt32(fieldInfo.Name.Substring(13));
            if (!parameters.ContainsKey(index))
            {
                parameters.Add(index, new ButtonParameters());
            }
            parameters[index].SubMenuBtnImg=  (string)fieldInfo.GetValue(null);
        }
        else if (fieldInfo.Name.Contains("SubMenuBtnBorder"))
        {
            int index = Convert.ToInt32(fieldInfo.Name.Substring(16));
            if (!parameters.ContainsKey(index))
            {
                parameters.Add(index, new ButtonParameters());
            }
            parameters[index].SubMenuBtnBorder= (string)fieldInfo.GetValue(null);
        }
    }
    return parameters;
}

private void createButtons()
{
    Dictionary<int, ButtonParameters> buttons = CreateParameters(typeof(AdvancedSubmenu));
    for (int i = 1; i < 5; i++)
    {
        SubBtnText[i].Text = buttons[i].SubMenuBtnText;
        pictureBoxSubBtn[i].Image = Image.FromFile(buttons[i].SubMenuBtnImg);
        panelSubBtn[i].BorderStyle = buttons[i].SubMenuBtnBorder;
    }
}

Comments

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.