Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm programming an application in C# using Visual Studio 2008 and Windows Forms to generate a series of Buttons that represent catagories of features. When each Button is pressed, I need to display submenus which list parameters. The MenuStrip from the ToolBox provides a similar functionality, but I need independent Buttons which can be placed anywhere on the screen.

How can I make this work?
Posted

You say that the 'list' will contain 'parameters' that the user can modify.

Answering helpfully depends on what you mean by 'that the user can modify'.

If it means either selecting or not you could use a ContextMenuStrip[^] and use the CheckOnClick property of the ToolStripMenuItem[^]s that you add to it.

If the user interaction is more involved than that, then a Panel on your form (as suggested earlier) or a separate Dialog would seem to be the way to go.
 
Share this answer
 
v2
Comments
David Del Castillo 25-Jan-11 16:35pm    
Thank you for your response. This is what I mean by "that the user can modify": Let's consider the case where we have one button on the display. The title of this button is "Antenna." When the user clicks this button, I would like to have something (dialog box, list, etc.)appear that contains two new parameters called "Antenna Directivity" and "Antenna Gain." Not only would these labels appear but the current value associated with these paramerters would appear as well. In addition, I would like to change the current value of these two parameters by selecting them and typing in new values.
Henry Minute 25-Jan-11 16:48pm    
In that case, it sounds as if a Dialog would be the best way to go.

You could create and populate the Dialog either directly in the OnClick event handler for each button or use SAKryukov's method and do it in the ButtonHelper class.

There are several good articles on CP on using Dialogs to get information from the user, one of them is mine :) http://www.codeproject.com/KB/dialog/GettingStuffFromDialogs_2.aspx, just in case you need help with that part of it.
If you don't like SAKryukov's answer, take a look at this:
XtraBars[^] - It certainly has the required functionality - there is a free trial.

Regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Jan-11 15:24pm    
Interesting - a 5. Too bad it's no Open Source
Espen Harlinn 25-Jan-11 15:42pm    
Download the trial - once you get the hang of it, you'll wonder why MS just didn't buy the company - be careful, you might get addicted.

btw: you may find this interesting http://www.harlinn.com/createblitzmodel_mssql.zip - it's something I'd like to pull more people into - remember my proposal about DSL's and code generation - this is one part of the solution ...

Sergey Alexandrovich Kryukov 25-Jan-11 16:25pm    
Thank you very much. Fortunately, this is not a kind of things I'm tend to be addicted to -- probably I'm not addictive to anything. As to your site, I find it difficult to read: all letters are familiar, as well as many of the words, but general meaning somehow slips out of my understanding...
Could you explain what is this file for? (I read your readme.) I use SQL approximately two times a decade on average; always thought is is not my business (I even developed software for developing of relational architectures, imagine that)...
Espen Harlinn 26-Jan-11 7:10am    
The sql creates tables and relations for a fundamental database - meaning that defined elements coverings the most common modeling scenarios, parties, roles, accounting, case management, work effort, workflow, and so on. The model is fairly standard, and it's not yet fully SOX compliant – that will probably be left as an exercise for the reader.

Think of it as an example database with the complexity of a real system.

Creating code for such a thing would usually require a fairly large team of developers for several years. Using DSL’s significantly reduce the time spent on trivia – usually about 50-70% of the code can be generated by code generators – and it’s not even rocket science, just good old software engineering based on well-known patterns.
Sergey Alexandrovich Kryukov 26-Jan-11 12:37pm    
Thank you for the explanation.
--SA
Write code like this:

C#
class ButtonHelper { /* ... */ }

void SetupButtonsVertically() {
    //you will need integer layout parameters like
    //buttonLeft, currentY (to of buttons), yGap (between buttons), etc.,
    //source of button names, data on generated list of options
    // (you call it sub-menu but it might be rather list or tree view)
    //...
    //in some loop:
    Button button = new Button();
    button.Click = delegate(object sender, EventArgs eventArgs) {
        //... code of some handler: show group of choices
        ButtonHelper buttonHelper = (ButtonHelper)((sender as Button).Tag);
        //...
    }; //button.Click
    button.Tag = new ButtonHelper(//...
    button.Left = buttonLeft;
    button.Top = currentY;
    currectY += button.Height + yGap;
    button.Text = //...
    this.PanelSomewhereOnMyForm.Control.Add(button);
//...
}


Call SetupButtonsVertically right after a call to InitializeComponent.
You can make more complex layout; this is just the idea.

Note the use of anonymous method: much more convenient way of programming events handlers. From the scope of Button.Click handler you can call any method, but unlike (morally obsolete) non-anonymous approach you're not bound to pass exact parameter list of sender and eventArgs.

Another useful trick is using button.Tag. You can put some useful information on the button function (of some helper class ButtonHelper) in its Tag (see Control.Tag). This Tag can be casted from sender in the handler to be used to implement click effect specific to a button instance.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 25-Jan-11 13:38pm    
OP follow-up question:

"Thank you for your response.

My major concern is about linking the sub-menu to the button click. It looks like the link to the sub-menu or list is handled by the event handler. Since the list will contain parameters that the user can modify, what control(i.e. ToolBox) would best implement the list?
"
Sergey Alexandrovich Kryukov 25-Jan-11 13:42pm    
Yes, you could implement a list even instead of buttons. I already gave similar advice to someone; my answer was well accepted. Imagine a list on your left and a panel on your right. On selection changed event you look at the selected item and re-populate the panel on right with different content. So your list on left will server as a Table of Contents. If you need some implementation detail (Tags not applicable, there is an easy way -- a helper class with overridden ToString), you will need to ask separate question.
Sergey Alexandrovich Kryukov 25-Jan-11 13:45pm    
I moved your "answer" to comment. You're not supposed to post a non-answer as an answer: 1) I would not get notification, 2) this fake answer should be removed later. Please use "Add Comment" or reply icon on existing comment. The notification is send to the author of the answer only or to the commenter (in case of reply) only.
Sergey Alexandrovich Kryukov 25-Jan-11 13:46pm    
Kind of strange vote of "3", I think... Didn't you vote?
The formulation of the question is difficult; if this is your vote you should explain why you're not satisfied first -- I'm not abandoning your question yet... :-)
fjdiewornncalwe 25-Jan-11 14:01pm    
+5 from me. Well answered.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900