• 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

    • I guess more Windows users are switching on their desktops/laptops than people buying new Steam Decks. I wouldn't be surprised. Deck doesn't have the mainstream appeal of other handhelds, usually people who would want one already have one. Glad to see Linux go up!
    • Any news about Windows and Microsoft always brings up crazy people saying; - Windows 11 is terrible, it destroys my hardware, it had sex with my wife, it told the FBI that I visit shemale sites, it collects my data where I get illegal anime. Never use Windows 11, go for Linux and it will never have sex with your wife.
    • QOwnNotes 25.6.0 by Razvan Serea QOwnNotes is a open source (GPL) plain-text file notepad with markdown support and todo list manager for GNU/Linux, Mac OS X and Windows, that (optionally) works together with the notes application of ownCloud (or Nextcloud). So you are able to write down your thoughts with QOwnNotes and edit or search for them later from your mobile device (like with CloudNotes) or the ownCloud web-service. The notes are stored as plain text files and you can sync them with your ownCloud sync client. Of course other software, like Dropbox, Syncthing, Seafile or BitTorrent Sync can be used too. Features: the notes folder can be freely chosen (multiple note folders can be used) sub-string searching of notes is possible and search results are highlighted in the notes application can be operated with customizable keyboard shortcuts external changes of note files are watched (notes or note list are reloaded) older versions of your notes can be restored from your ownCloud server trashed notes can be restored from your ownCloud server differences between current note and externally changed note are showed in a dialog markdown highlighting of notes and a markdown preview mode notes are getting their name from the first line of the note text (just like in the ownCloud notes web-application) and the note text files are automatically renamed, if the the first line changes compatible with the notes web-application of ownCloud and mobile ownCloud notes applications compatible with ownCloud's selective sync feature by supporting an unlimited amount of note folders with the ability to choose the respective folder on your server manage your ownCloud todo lists (ownCloud tasks or Tasks Plus / Calendar Plus) or use an other CalDAV server to sync your tasks to encryption of notes (AES-256 is built in or you can use custom encryption methods like Keybase.io (encryption-keybase.qml) or PGP (encryption-pgp.qml)) dark mode theme support theming support for the markdown syntax highlighting all panels can be placed wherever you want, they can even float or stack (fully dockable) support for freedesktop theme icons, you can use QOwnNotes with your native desktop icons and with your favorite dark desktop theme support for hierarchical note tagging and note subfolders support for sharing notes on your ownCloud server portable mode for carrying QOwnNotes around on USB sticks Evernote import QOwnNotes is available in many different languages like English, German, French, Polish, Chinese, Japanese, Russian, Portuguese, Hungarian, Dutch and Spanish Changes in QOwnNotes 25.6.0: QOwnNotes now builds with with Botan 3, when built with the system Botan library (cmake build parameter -DBUILD_WITH_SYSTEM_BOTAN=ON), because of the end of life of Botan 2 (for #2786) Keep in mind that Botan 3 needs C++20, which causes issues on Qt5 QMake and Qt5 will still use the internal Botan 2 amalgamation Creating an amalgamation Botan 3 did work, but caused major issues with various build processes The filename in checksum files of the Qt6 AppImages in the releases on GitHub was fixed (for #3286) Download: QOwnNotes 25.6.0 | 37.3 MB (Open Source) Download: QOwnNotes for Other Operating Systems View: QOwnNotes Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • There are lots of older PC's that are now going to be unsupported on W10, but for modern systems, you should be able to update to W11. Even so, there are ways to bypass the official hardware requirements as well as the MSA (which imo is the bigger annoyance). My only gripe with W11 is all the telemetry collection / bloatware - yes, it is a problem compared to W10 and Linux.
  • Recent Achievements

    • Week One Done
      Epaminombas earned a badge
      Week One Done
    • Week One Done
      Prestige Podiatry Care earned a badge
      Week One Done
    • Week One Done
      rollconults earned a badge
      Week One Done
    • One Month Later
      lilred1938 earned a badge
      One Month Later
    • Week One Done
      lilred1938 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      148
    2. 2
      Xenon
      132
    3. 3
      ATLien_0
      123
    4. 4
      +Edouard
      103
    5. 5
      snowy owl
      96
  • Tell a friend

    Love Neowin? Tell a friend!