Windows Develop Bookmark and Share   
 index > Windows Forms General > C# - Custom ToolStrip
 

C# - Custom ToolStrip

Hi,

I have widely used ToolStrip in my application. I created a UserControl which contains this ToolStrip. I would rather like to use inheritance:

MyCustomControl : ToolStrip

Everything seems to work fine, untill I want to set visibility to one of ToolStripItem - there are maybe 20 buttons and not all of my ChildForms which contains this MyCustomControl need all of them. I open in designer mode in Property window "Items", choose right one and than select "Visibility" > false. In designer mode button dissapear. When compile, designer is reloaded, button is back again.

If I code manually in constructor of ChildForm:

myCustomControl.Items["someButton"].Visible = false;

it worsk OK.

What would you advice me, please?
  • Moved byHarry ZhuMSFTTuesday, September 22, 2009 8:53 AMrelating to custom control (From:Visual C# General)
  •  
lukas43  Saturday, September 19, 2009 7:45 AM
Hello,

I'm not quite sure how the logic is laid out, but how did you handle the designer in your Custom Tooltip ? I mean it's even related ?
Eyal, Regards.

blog.eyalsh.net
Eyal-Shilony  Saturday, September 19, 2009 8:17 AM
I did not. I have not experience with custom controls rendering/design. Could you give me advice, please?
lukas43  Saturday, September 19, 2009 8:24 AM
Can you post some code ? such as the Custom Control, and a sample code that reproduces the problem ? it's hard to guess here.

Compiling the code shouldn't change anything that relates to the designer, unless you handle the designerin a flaw, or it's not even related to the actual compilation, and you set some default value that overrides the visibility value.
Eyal, Regards.

blog.eyalsh.net
Eyal-Shilony  Saturday, September 19, 2009 8:41 AM
Compile did not. Visual Studio did - when I changed through designer property of item and save window, in MyForm.designer.cs I did not found corresponding record, such as:

this.myCustomControl.ToolStripItem1.Visible = false;

I see it like Visual Studio does not create this record in .designer for specific situations and this is one of those. For other custom controls inheriting from .NET such as myTextBox : System.Windows.Forms.TextBox works everything great...
lukas43  Saturday, September 19, 2009 2:16 PM

It's a bit confusing, you were speaking about the ToolStrip, and now this piece of code is actually an item representing a child of the ToolStrip, it's an abstract class which is not likely to be produced by Visual Studio, at least not in that context, however having a collection of type 'ToolStripItem' is very much possible.

Things will be much more easier if you can just describe what you're trying to do, better, providing some code samples, so we can work it out. :)


Eyal, Regards.

blog.eyalsh.net

Eyal-Shilony  Saturday, September 19, 2009 5:16 PM
Definition of control
public partial class FormMenu : ToolStrip, ILoadable


Constructor:

        public FormMenu()
        {
            InitializeComponent();
            
            this.tsiFirst.Enabled       = false;
            this.tsiPrevious.Enabled    = false;

            this.tsiSave.Visible        = false;
            this.tsiBack.Visible        = false;
            this.tsiRestore.Visible     = false;
            this.tsiExport.Visible      = false;
            this.tsiYears.Visible       = false;

            oPaging = new PagingItem(tsiFirst, tsiPrevious, tsiNext, tsiLast, tsiPage, tsiFrom);
        }


Property of object:

        [Browsable(true)]
        public ToolStripItem Trash
        {
            get { return tsiTrash; }
        }


I include this control in my forms. Everything works fine.. In Form, which contains this "menu" I try to change Trash buttons visibility. Property is visible in Visual Studio designer, I use standard methods - go to property window, and change Visible to False. When I save file, open Form.designer.cs I dont see generated record for this change. When I build, VS reloads window and property Visible is back to True.
lukas43  Sunday, September 20, 2009 7:11 AM

If you want Visual Studio to automatically generate code for an object, you will have to handle the designer, and probably serialize, deserialize the object, you may want to check the DesignerSerializationVisibilityAttribute, EditorAttribute and TypeConverter if so needed.

You can try and add this to the property.

[System.ComponentModel.DesignerCategory("code")]
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.StatusStrip)]


Also, have a look here.

1. Design Time Custom Designers
2. Writing Custom Designers for .NET Components
3. .NET Shape Library: A Sample Designer
4. Designer Technical Articles


Eyal, Regards.

blog.eyalsh.net
  • Unproposed As Answer bylukas43 Monday, September 21, 2009 6:15 PM
  • Proposed As Answer byEyal-Shilony Sunday, September 20, 2009 12:05 PM
  •  
Eyal-Shilony  Sunday, September 20, 2009 9:16 AM
Sorry :( Nothing has changed... Designer does not still generate code :(
lukas43  Monday, September 21, 2009 6:15 PM

You can use google to search for other answers

Custom Search

More Threads

• Disabling "tab" event
• Generating and writiing into excel file programatically through C#.net and ODBC Driver
• User control and clipping problem within layout panel
• Windows Forms Loading slow
• My custom control not show up in toolbox
• TopLevel Focus
• Using custom Event Arguments with a System.EventHandler
• Cancel scroll event in the webbrowser control
• PropertyGrid - Emulating the event TypeEditor for a non-event custom type.
• How Managed User control runs in Internet Explorer