• 0

[C#] Adding buttons/tabs to a ToolStrip/TabControl in my user control


Question

I have this custom user control that has a ToolStrip and a TabControl and I'm adding my own designer to the user control so at design-time I can automate some actions. The smart tag panel as a verb called "Insert New Page", this verb is going to add a button the ToolStrip and a Tab to the TabControl every time you click on it.

Well, part of this works, the other part, doesn't. When you click the "Insert New Page" verb, the button will be added to the ToolStrip and the tab will be added to the TabControl, also, if you open "Form1.Designer.cs", you will see that the code for the button/tab, was generated.

However, if you run the application after adding one or more buttons/tabs, the Form will be pratically empty. Only the user control will be there, but not the added button/page. And the code is on the "Form1.Designer.cs" file.

I'm pasting below the most important code about this (I think) but I am also attaching a "DesignerTest.zip" file which is a complete solution with the above description of my control and what I'm tyring to do so you can test for yourself without having to recreate everything.

Just launch the solution in VS, select the only control on the Form, click "Insert New Page" and F5 the application and see what happens...

internal class MyUCDesigner : ControlDesigner
{
	private DesignerActionListCollection alColletion;

	public override DesignerActionListCollection ActionLists
	{
		get
		{
			if (alColletion == null)
			{
				alColletion = new DesignerActionListCollection();
				alColletion.Add(new MyUCDesignerActionList((MyUserControl)Control));
			}

			return alColletion;
		}
	}
}

internal class MyUCDesignerActionList : DesignerActionList
{
	private MyUserControl myUserControl;

	public MyUCDesignerActionList(MyUserControl control) : base(control)
	{
		myUserControl = control;
	}

	private void InsertNewPage()
	{
		IComponentChangeService ICCS = (IComponentChangeService)GetService(typeof(IComponentChangeService));
		IDesignerHost IDH = (IDesignerHost)GetService(typeof(IDesignerHost));
		ToolStripButton tsButton;
		TabPage tPage;

		DesignerTransaction dTransaction = IDH.CreateTransaction("Insert New Page");

		tsButton = (ToolStripButton)IDH.CreateComponent(typeof(ToolStripButton));
		tPage = (TabPage)IDH.CreateComponent(typeof(TabPage));

		ICCS.OnComponentChanging(myUserControl, null);

		tsButton.Image = Properties.Resources.DefaultButton_Large;
		tsButton.ImageAlign = ContentAlignment.BottomCenter;
		tsButton.Text = tsButton.Name;
		tsButton.TextAlign = ContentAlignment.BottomCenter;
		tsButton.TextImageRelation = TextImageRelation.ImageAboveText;

		tPage.Text = tPage.Name;

		myUserControl.toolStrip.Items.Add(tsButton);
		myUserControl.tabControl.Controls.Add(tPage);

		ICCS.OnComponentChanged(myUserControl, null, null, null);
		dTransaction.Commit();
	}
}

DesignerTest.zipFetching info...

3 answers to this question

Recommended Posts

  • 0

I have made some new findings but haven't yet found a final solution for this. The above code and stuff is all good (I think) but probably missing something...

I mean, let's say you add my control to a form, open the smart tag panel and click "Insert new page", you will then have a button and a tab added to the ToolStrip/TabControl and if you open the "Form1.Designer.cs" file you will see the code there... You will also see the code for myUserControl1 like this:

// 
// myUserControl1
// 
this.myUserControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.myUserControl1.Location = new System.Drawing.Point(0, 0);
this.myUserControl1.Name = "myUserControl1";
this.myUserControl1.Size = new System.Drawing.Size(236, 189);
this.myUserControl1.TabIndex = 0;

But it's missing 2 lines, which are these:

this.myUserControl1.tabControl.Controls.Add(this.tabPage1);
this.myUserControl1.toolStrip.Items.Add(this.toolStripButton1);

If those 2 lines were in the code, it would work... The "Insert new page" smart tag panel verb adds the necessary code for the each added ToolStrip/TabControl ToolStripButton/TabPage, but does not add the necessary code to add those controls to the ToolStrip/TabControl.

So, how do I make that happen in design time?

PS: For the above to work, I had to change both the ToolStrip and TabControl controls in my custom user control to public.

  • 0

Solved. The solution was something like this:

[DesignerSerializationVisibility( DesignerSerializationVisibility.Content)]
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public System.Windows.Forms.TabControl.TabPageCollection Pages
{
  get { return this.tabControl.TabPages; }

}

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public ToolStripItemCollection Buttons
{
  get { return this.toolStrip.Items; }
}

This worked as I wanted, however, there's a tiny little problem...

I added the code and then replaced:

this.myUserControl1.tabControl.Controls.Add(this.tabPage1);

this.myUserControl1.toolStrip.Items.Add(this.toolStripButton1);

By:

myUserControl.Buttons.Add(tsButton);

myUserControl.Pages..Add(tPage);

Then, let's say I test the designer implementation, open the smart tag panel and click "Insert new page", well, it works fine and if I undo, it also works. BUT, it doesn't work if I press redo.

Code after "Insert new page":

//
// myUserControl1
//
this.myUserControl1.Buttons.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripButton1});
this.myUserControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.myUserControl1.Location = new System.Drawing.Point(0, 0);
this.myUserControl1.Name = "myUserControl1";
this.myUserControl1.Size = new System.Drawing.Size(236, 189);
this.myUserControl1.TabIndex = 0;
//
// toolStripButton1
//
this.toolStripButton1.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton1.Image
this.toolStripButton1.ImageAlign = System.Drawing.ContentAlignment.BottomCenter;
this.toolStripButton1.Name = "toolStripButton1";
this.toolStripButton1.Size = new System.Drawing.Size(98, 51);
this.toolStripButton1.Text = "toolStripButton1";
this.toolStripButton1.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
this.toolStripButton1.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText;

Code after "undo":

//
// myUserControl1
//
this.myUserControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.myUserControl1.Location = new System.Drawing.Point(0, 0);
this.myUserControl1.Name = "myUserControl1";
this.myUserControl1.Size = new System.Drawing.Size(236, 189);
this.myUserControl1.TabIndex = 0;

Code after "redo":

//
// myUserControl1
//
this.myUserControl1.Buttons.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripButton1});
this.myUserControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.myUserControl1.Location = new System.Drawing.Point(0, 0);
this.myUserControl1.Name = "myUserControl1";
this.myUserControl1.Size = new System.Drawing.Size(236, 189);
this.myUserControl1.TabIndex = 0;
//
// toolStripButton1
//
this.toolStripButton1.Name = "toolStripButton1";
this.toolStripButton1.Size = new System.Drawing.Size(23, 22);

PS: The variable declarations and the new() instantiations are created/deleted just fine with the undo/redo.

PS2: This code only shows the toolStripButton1 for example purposes.

So, if anyone knows how to fix this "redo" problem, it would be very helpful!

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.