• 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

    • Revo Uninstaller Free 2.6.0 by Razvan Serea Revo Uninstaller helps you to uninstall software and remove unwanted programs installed on your computer easily! Even if you have problems uninstalling and cannot uninstall them from "Windows Add or Remove Programs" control panel applet. With its advanced and fast algorithms, Revo Uninstaller analyzes an application's data before uninstall and scans for remnants after the uninstall of a program. After the program's regular uninstaller runs, you can remove additional unnecessary files, folders and registry keys that are usually left over on your computer. Revo Uninstaller offers you some simple, easy to use, but effective and powerful methods for uninstalling software like tracing the program during its installation. Revo Uninstaller has a very powerful feature called Forced Uninstall. Forced Uninstall is the best solution when you have to remove stubborn programs, partially installed programs, partially uninstalled programs, and programs not listed as installed at all! To remove a program completely, and without leaving a trace, you can monitor all system changes made during its installation, and then use that information to uninstall it with one click only – simple and easy! Revo Uninstaller is a much faster and more powerful alternative to "Windows Add or Remove Programs" applet! It has very powerful features to uninstall and remove programs. No more stubborn programs No more installation errors No more upgrade problems Remove programs easily Revo Uninstaller Free 2.6.0 changelog: Improved – Scanning algorithms for leftovers Fixed minor bugs Updated language files Download: Revo Uninstaller Free 2.6.0 | Portable ~10.0 MB (Freeware) View: Revo Uninstaller Website | Revo Uninstaller Pro | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • I do agree with your point on the lack of ability to reach a human support agent, it's highly frustrating and sadly not uncommon these days. 100% - if a company breaches a SLA, that's not acceptable, but if you breach their ToS then the SLA is invalidated. Why the guy was locked out? - who knows, its looks like it's breach of ToS, but who knows, maybe it's an error on Microsoft's side in this instance, maybe it's an accident, maybe it's a flagrant breach - pointless speculating. However the point here is by uploading all your data to a single point you have backed yourself into a corner where you don't have a recovery plan and that is 100% on you. If you have all your data on hard disk and it fails - do you blame the manufacturer for the data loss? What if the provider goes bust What if you forget to update a payment method and the account is terminated because you miss the email because you're busy, change the address, whatever What if the provider has a catastrophic failure (unlikely with the bigger players, but nothing is impossible) Point being however you store data - be it cloud or locally, if you only have one copy it should be viewed as data at risk, and you are the one who must manage the risk.
    • Rematch, Warcraft, another Call of Duty, FBC: Firebreak, and more hit Xbox Game Pass by Pulasthi Ariyasinghe Microsoft has unveiled the games that will be available to Xbox Game Pass subscribers in the second half of June. The latest wave touts several more games from the coffers of Activision Blizzard, including the three remastered Warcraft games and the 2017-released Call of Duty: WWII. Three day-one drops are a part of this wave. This includes Remedy Entertainment's first multiplayer-focused co-op entry, FBC: Firebreak, the hugely anticipated soccer game from Sifu developers, Rematch, and the indie roguelike Lost in Random: The Eternal Die. Here are all the games announced for Game Pass today and their arrival dates: FBC: Firebreak (Cloud, PC, and Xbox Series X|S) – Available today Crash Bandicoot 4: It’s About Time (Console and PC) – Available today Lost in Random: The Eternal Die (Cloud, PC, and Xbox Series X|S) – Available today Star Trucker (Xbox Series X|S) – June 18 Wildfrost (Console) – June 18 Rematch (Cloud, PC, and Xbox Series X|S) – June 19 Volcano Princess (Cloud, Console, and PC) – June 24 Against the Storm (Cloud and Console) – June 26 Warcraft I: Remastered (PC) – June 26 Warcraft II: Remastered (PC) – June 26 Warcraft III: Reforged (PC) – June 26 Call of Duty: WWII (Console and PC) – June 30 Little Nightmares II (Cloud, Console, and PC) – July 1 Rise of the Tomb Raider (Cloud, Console, and PC) – July 1 Just as new games arrive, six will be leaving the Game Pass programs on June 30. These are Arcade Paradise, Journey to the Savage Planet, My Friend Peppa Pig, Robin Hood: Sherwood Builders, SteamWorld Dig, and SteamWorld Dig 2 across both PC and Xbox consoles. With June reveals out of the way, expect the next Game Pass announcement to arrive in early July, revealing what's coming in the first half of the new month. Don't forget that the Xbox Games Showcase also revealed more titles for Game Pass like The Outer Worlds 2, Grounded 2, Black Ops 7, and At Fate's End.
    • context menu before it was instantly, now you need to click twice and the old context menu sometimes have to load
    • Is NAD a legitimate court? Nope, it's part of the BBB. So they can allege whatever they want. Guilt is the result of being convicted by an actual recognized legitimate court. Just sayin.
  • Recent Achievements

    • Week One Done
      Rhydderch earned a badge
      Week One Done
    • Experienced
      dismuter went up a rank
      Experienced
    • One Month Later
      mevinyavin earned a badge
      One Month Later
    • Week One Done
      rozermack875 earned a badge
      Week One Done
    • Week One Done
      oneworldtechnologies earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      698
    2. 2
      ATLien_0
      272
    3. 3
      Michael Scrip
      214
    4. 4
      +FloatingFatMan
      186
    5. 5
      Steven P.
      144
  • Tell a friend

    Love Neowin? Tell a friend!