3

I want to add some checkboxes along with standard toolstrip controls in my C# winform. There is no issue with standard controls provided by toolstrip but problem with my checkbox. In toolstrip, the first control is combobox(toolstrip combobox), second one is label(toolstrip label). There is no problem till now. I have to add two checkboxes and then tollstrip combobox. I am adding first checkbox as below

    System.Windows.Forms.CheckBox c1 = new System.Windows.Forms.CheckBox();
    c1.CheckState = System.Windows.Forms.CheckState.Unchecked;
    System.Windows.Forms.ToolStripControlHost host = new   
    System.Windows.Forms.ToolStripControlHost(c1);
    toolStrip1.Items.Add(host);

If I am running application then I am able to see this check box with toolstrip. Once I saved my form, everything lost. I could not find even c1 in my cs file. There are some additional entries with host in designer.cs file. What I am missing here? How can I add these 2 checkboxes in toolstrip?

1

1 Answer 1

5

First of all

From the point of view of good UI Winforms practices, you should use a ToolStripButton with its Checked, CheckState and CheckOnClick properties instead.

Now about your issues

I suppose that you have write this code in the designer file ? That's really a bad idea, because the IDE is going to overwrite change in this file. The file being hidden (by default) and the warning comment are not here for nothing !

This kind of operation should be done on the code file, within the form constructor (The New() Sub) just after the call to InitializeComponent().

Remarks You won't see the result in the designer but it will display on runtime. If you want designer support, it's another story and not a simple one !

3
  • I am able to do this. May 8, 2017 at 12:51
  • Is there a way to make the ToolStripButton look and work like a checkbox? I am unable to get the display the same like the tick when checked.
    – Darrel K.
    May 8, 2018 at 9:53
  • 1
    The "ToolStripButton" is a really bad UI choice for something that needs a checkbox. It is very unclear to the user of the application as to what "checked" state a "ToolStripButton" is in. Is that boarder around the button there just to show that it is a button and not a label, or does it mean it is "checked"? Really poor UI design. May 25, 2023 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.