• 0

[C#] Inherting TabPage & TabControl


Question

I hit a wall...

In a nut shell, I am trying to inherit System.Windows.Forums.TabControl & ...TabPage. I order to add a custom prop field. Now all indications point to it working. (IntelliSence displays custom prop, and the form designer indicates the inherited control is correctly being used)

How ever <tabcontrolername>.SelectedTab.<customprop> is always blank (null).

In order to spare you all the ins and outs, I created a generic project describing what I'm trying to do. In the sample the custom prop is a string (to keep it simple), however in the real app it's an instance of a custom struct.

So, any ideas as to what I'm doing wrong?

Base form code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TabStorage
{
	public partial class fmMain : Form
	{
		public fmMain()
		{
			InitializeComponent();
		}

		private void btNew_Click(object sender, EventArgs e)
		{
			NTabPage np = new NTabPage();

			np.Text = "New Tab";
			np.PageMode = "Custom Tab Prop String";

			tcTabControl.TabPages.Add(np);

			np = null;
		}

		private void btInfo_Click(object sender, EventArgs e)
		{
			MessageBox.Show(tcTabControl.SelectedTab.PageMode);
		}

	}
}

Win Forms Designer code:

namespace TabStorage
{
	partial class fmMain
	{
		/// &lt;summary&gt;
		/// Required designer variable.
		/// &lt;/summary&gt;
		private System.ComponentModel.IContainer components = null;

		/// &lt;summary&gt;
		/// Clean up any resources being used.
		/// &lt;/summary&gt;
		/// &lt;param name="disposing"&gt;true if managed resources should be disposed; otherwise, false.&lt;/param&gt;
		protected override void Dispose(bool disposing)
		{
			if (disposing &amp;&amp; (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code

		/// &lt;summary&gt;
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// &lt;/summary&gt;
		private void InitializeComponent()
		{
			this.btNew = new System.Windows.Forms.Button();
			this.btInfo = new System.Windows.Forms.Button();
			this.tcTabControl = new TabStorage.NTabControl();
			this.SuspendLayout();
			// 
			// btNew
			// 
			this.btNew.Location = new System.Drawing.Point(0, 12);
			this.btNew.Name = "btNew";
			this.btNew.Size = new System.Drawing.Size(75, 23);
			this.btNew.TabIndex = 0;
			this.btNew.Text = "New Tab";
			this.btNew.UseVisualStyleBackColor = true;
			this.btNew.Click += new System.EventHandler(this.btNew_Click);
			// 
			// btInfo
			// 
			this.btInfo.Location = new System.Drawing.Point(0, 41);
			this.btInfo.Name = "btInfo";
			this.btInfo.Size = new System.Drawing.Size(75, 23);
			this.btInfo.TabIndex = 1;
			this.btInfo.Text = "Get Info";
			this.btInfo.UseVisualStyleBackColor = true;
			this.btInfo.Click += new System.EventHandler(this.btInfo_Click);
			// 
			// tcTabControl
			// 
			this.tcTabControl.Location = new System.Drawing.Point(81, 12);
			this.tcTabControl.Name = "tcTabControl";
			this.tcTabControl.PageMode = null;
			this.tcTabControl.SelectedIndex = 0;
			this.tcTabControl.Size = new System.Drawing.Size(229, 328);
			this.tcTabControl.TabIndex = 2;
			// 
			// fmMain
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(322, 352);
			this.Controls.Add(this.tcTabControl);
			this.Controls.Add(this.btInfo);
			this.Controls.Add(this.btNew);
			this.Name = "fmMain";
			this.Text = "Tab Storage";
			this.ResumeLayout(false);

		}

		#endregion

		private System.Windows.Forms.Button btNew;
		private System.Windows.Forms.Button btInfo;
		private NTabControl tcTabControl;
	}

	public class NTabControl : System.Windows.Forms.TabControl
	{
		private string m_PageMode;
		private NTabPage m_TabPage = new NTabPage();

		public string PageMode
		{
			get { return m_PageMode; }
			set { m_PageMode = value; }
		}

		public new NTabPage SelectedTab
		{
			get { return m_TabPage; }
			set { m_TabPage = value; }
		}
	}


	public class NTabPage : System.Windows.Forms.TabPage
	{
		private string m_PageMode;

		public string PageMode
		{
			get { return m_PageMode; }
			set { m_PageMode = value; }
		}
	}
}

The full poroject in a zip.

TabStorage.zipFetching info...

Link to comment
https://www.neowin.net/forum/topic/442553-c-inherting-tabpage-tabcontrol/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

In your NTabPage definition, you're SelectedTab property isn't correct.

	public class NTabControl : System.Windows.Forms.TabControl
	{
		private string m_PageMode;

		public string PageMode
		{
			get { return m_PageMode; }
			set { m_PageMode = value; }
		}

		public new NTabPage SelectedTab
		{
			get { return base.SelectedTab as NTabPage; }
			set { base.SelectedTab = value; }
		}
	}

  • 0

LMAO, you also helped me fix another problem.

tcMain.TabPages.Remove(tcMain.SelectedTab);

Wouldn't work, and I couldn't figure out why.

But now it makes much more since base.SelectedTab adds all the pase properties of the tab control to the SelectedTab prop.

Thanks again!

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

    • No registered users viewing this page.