TheSwamp

Code Red => .NET => Topic started by: Draftek on January 31, 2006, 11:28:37 AM

Title: Control Arrays
Post by: Draftek on January 31, 2006, 11:28:37 AM
I'm building a C# application using the Model-View-Controller technique.

I've built a form (with no code) that looks like Windows explorer with a menu, tool menu, address combo box, and treeview / listview splitter.

Anyway, in the controller app which contains the code to add and trap events of the items in the form. The menu has some items which are checked menu items that should act like a radio button optioin list. For instance, I'm going to allow the user to view drawing previews in the listview control at a large size, small size or details just like explorer does. When the user selects one of these options the others should be unchecked. In addtition, the toolbar strip that contains a dropdownbutton control should also by synced accordingly.

In order to not have a gazillion event handlers, I'm using a single event handler for the menu items and select case on the tag (set by me in code) to determine which menu item was selcted. The problem is, there is a fair amount of code to sync the checked items.
Here is an example of a two-toggle menu item for local details / web details to sync the menu buttons and the toolbar button:
Code: [Select]
                case "File1":
                    // menu File Locals details was selected
                   
                    // toggle this along with File2 menu item
                    if (mnuItem.Checked)
                    {
                        mnuFileWeb.Checked = false;
                        mnuFileLocal.Checked = true;
                        // sync the tool strip button too
                        toolConnect.Text = "Local Details";
                        toolConnect.Image = Properties.Resources.File1.ToBitmap();
                    }
                    else
                    {
                        mnuFileWeb.Checked = true;
                        mnuFileLocal.Checked = false;
                        toolConnect.Text = "Web Details";
                        toolConnect.Image = Properties.Resources.File2.ToBitmap();
                    }
                    break;
                case "File2":
                    // menu file Web details was selected
                   
                    if (mnuItem.Checked)
                    {
                        mnuFileLocal.Checked = false;
                        mnuFileWeb.Checked = true;
                        toolConnect.Text = "Web Details";
                        toolConnect.Image = Properties.Resources.File2.ToBitmap();
                    }
                    else
                    {
                        mnuFileLocal.Checked = true;
                        mnuFileWeb.Checked = false;
                        toolConnect.Text = "Local Details";
                        toolConnect.Image = Properties.Resources.File1.ToBitmap();
                    }
                    break;

You can imagine this could get complex with more than a couple of options.

How are you guys simulating control arrays for .Net development?

Title: Re: Control Arrays
Post by: Kerry on January 31, 2006, 04:23:20 PM
Haven't played with Control Arrays or done much Menu work, but I cant Imagine how to reduce the code required to make them work ... after all, there can be a lot of decision logic involved with menu's.
Title: Re: Control Arrays
Post by: MickD on January 31, 2006, 04:45:37 PM
Can you handle the checking/setting using the control array itself?
ie. if you pick 'large' setting, use a function passing in the index of the 'large' control to check that item then uncheck the other items in the control array in a loop.
rough outline eg.

uncheckItems(int controlIndex)
{
    for(int i = 0; i < array.size(); i++)
        if( i == array)
            checkitem
        else
            uncheckitem
}
Title: Re: Control Arrays
Post by: Draftek on January 31, 2006, 05:31:53 PM
hmm. That might work if I use the tag to store the index and query the text for the event handler.

Thanks. I'll ponder that some.
Title: Re: Control Arrays
Post by: MickD on January 31, 2006, 05:42:16 PM
updated rough outline to include the array -

uncheckItems(int controlIndex, Array controlarray)
{
    for(int i = 0; i < controlarray.size(); i++)
        if( i == array)
            checkitem
        else
            uncheckitem
}