• 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.
  • Posts

    • Burrrrn. Bought COD WWII last month when it was on sale for PC. Oh well. Excellent game tho, and the PC version plays/looks amazing (and has it's own PC achievements). ...wondering if this is a lesson on waiting on sales. ...also hoping Rise of the Tomb Raider has it's own PC Achievements
    • Google Chrome 137.0.7151.120 (offline installer) by Razvan Serea The web browser is arguably the most important piece of software on your computer. You spend much of your time online inside a browser: when you search, chat, email, shop, bank, read the news, and watch videos online, you often do all this using a browser. Google Chrome is a browser that combines a minimal design with sophisticated technology to make the web faster, safer, and easier. Use one box for everything--type in the address bar and get suggestions for both search and Web pages. Thumbnails of your top sites let you access your favorite pages instantly with lightning speed from any new tab. Desktop shortcuts allow you to launch your favorite Web apps straight from your desktop. Chrome has many useful features built in, including automatic full-page translation and access to thousands of apps, extensions, and themes from the Chrome Web Store. Google Chrome is one of the best solutions for Internet browsing giving you high level of security, speed and great features. Important to know! The offline installer links do not include the automatic update feature. Google Chrome 137.0.7151.120 changelog: [$7000][420697404] High CVE-2025-6191: Integer overflow in V8. Reported by Shaheen Fazim on 2025-05-27 [$4000][421471016] High CVE-2025-6192: Use after free in Profiler. Reported by Chaoyuan Peng (@ret2happy) on 2025-05-31 [425443272] Various fixes from internal audits, fuzzing and other initiatives Download web installer: Google Chrome Web 32-bit | Google Chrome 64-bit | Freeware Download: Google Chrome Offline Installer 64-bit | 128.0 MB Download: Google Chrome Offline Installer 32-bit | 115.0 MB Download page: Google Chrome Portable Download: Google Chrome MSI Installers for Windows (automatic update) View: Chrome Website | Release Notes Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • -Drop the art style, it's cool but doesn't fit the franchise at all. -Make it a gritty single player game, like Halo -Include deathmatch and all that stuff, extraction can be a separate mode If they don't do that, it's dead on arrival IMO.
    • It's a shame because it's one of the best immersive sims ever made, even though it could have used a bit more monster variety IMO.
  • Recent Achievements

    • One Month Later
      Custom Greek Shirts earned a badge
      One Month Later
    • Week One Done
      Custom Greek Shirts earned a badge
      Week One Done
    • One Year In
      Custom Greek Shirts earned a badge
      One Year In
    • Week One Done
      topantidetectbrowser earned a badge
      Week One Done
    • Explorer
      Jdoe25 went up a rank
      Explorer
  • Popular Contributors

    1. 1
      +primortal
      672
    2. 2
      ATLien_0
      281
    3. 3
      Michael Scrip
      223
    4. 4
      +FloatingFatMan
      190
    5. 5
      Steven P.
      146
  • Tell a friend

    Love Neowin? Tell a friend!