By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,474 Members | 966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,474 IT Pros & Developers. It's quick & easy.

Can I inherit controls from another class at runtime??

P: n/a
Visual Studio 2003 .Net / C#

I have a 2 page Tab Control for users to add a Job in my application. The
first page is for them to choose which type of Job they would like. The type
of Job determines what information is asked for on the second page, and so
will determine the layout of the second page. So this second page needs to
be determined at runtime really when they select the Job Type on page 1. I
thought I could maybe define all the controls (Labels, Buttons, TextBoxes,
ComboBoxes etc) in seperate class file, then when they make their selection
on page 1, I instantiate this class and create the objects on my tabpage.

Is this possible? If so how?

I have a PlumbJob.cs which has in it 2 TextBoxes and 2Labels defined, these
have their appropriate locations, sizes names etc defined in the PlumbJob.cs,
and then I have public ArrayList with this objects in it.

When I select the JobType on TabPage1 I initialize this class, and scroll
through the Controls in this ArrayList, and try and create instances of them
on my TabPage like this:

foreach(Control ctTemplate in PlumbJob.ObjectArray)
{
Control obj = new
Control(tpWizard2,ctTemplate.Text,ctTemplate.Left, ctTemplate.Top,ctTemplate.Width,ctTemplate.Height) ;
obj.Name = ctTemplate.Name;
obj.Visible = true;
}

but I cant see the objects on my tab page. Any ideas?? Is this a viable way
of doing what I am trying to achieve?

Thanks

Jul 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Steve <St***@discussions.microsoft.com> wrote:
I have a 2 page Tab Control for users to add a Job in my application. The
first page is for them to choose which type of Job they would like. The type
of Job determines what information is asked for on the second page, and so
will determine the layout of the second page. So this second page needs to
be determined at runtime really when they select the Job Type on page 1. I
thought I could maybe define all the controls (Labels, Buttons, TextBoxes,
ComboBoxes etc) in seperate class file, then when they make their selection
on page 1, I instantiate this class and create the objects on my tabpage.
Sure.
Is this possible? If so how?

I have a PlumbJob.cs which has in it 2 TextBoxes and 2Labels defined, these
have their appropriate locations, sizes names etc defined in the PlumbJob.cs,
and then I have public ArrayList with this objects in it.

When I select the JobType on TabPage1 I initialize this class, and scroll
through the Controls in this ArrayList, and try and create instances of them
on my TabPage like this:

foreach(Control ctTemplate in PlumbJob.ObjectArray)
{
Control obj = new
Control(tpWizard2,ctTemplate.Text,ctTemplate.Left, ctTemplate.Top,ctTemplate.Width,ctTemplate.Height) ;
obj.Name = ctTemplate.Name;
obj.Visible = true;
}

but I cant see the objects on my tab page. Any ideas?? Is this a viable way
of doing what I am trying to achieve?


That's creating controls, but not adding them to anything. You probably
want to use Controls.Add for each newly created control, to add them to
the current form's list of controls.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

P: n/a
hmm, still no joy. These objects are to sit on a TabPage, on my main form, so
I presume as I create them I need to add them to the controls collection for
that tabpage, yes? So I did this:

foreach(Control ctTemp in ProcessAO.ObjectArray){
Control ctObj = new
Control(ctTemp.Text,ctTemp.Left,ctTemp.Top,ctTemp. Width,ctTemp.Height);
tpWizard2.Controls.Add(ctObj);
}

when I run it though I can see no controls at all on this page. If I debug
and foreach through each control on my tpWizard page I can see that they are
all there, but they are not visible. Am I forgetting something? Or am I
doing things in the wrong order?

Thanks

Steve

"Jon Skeet [C# MVP]" wrote:
Steve <St***@discussions.microsoft.com> wrote:
I have a 2 page Tab Control for users to add a Job in my application. The
first page is for them to choose which type of Job they would like. The type
of Job determines what information is asked for on the second page, and so
will determine the layout of the second page. So this second page needs to
be determined at runtime really when they select the Job Type on page 1. I
thought I could maybe define all the controls (Labels, Buttons, TextBoxes,
ComboBoxes etc) in seperate class file, then when they make their selection
on page 1, I instantiate this class and create the objects on my tabpage.


Sure.
Is this possible? If so how?

I have a PlumbJob.cs which has in it 2 TextBoxes and 2Labels defined, these
have their appropriate locations, sizes names etc defined in the PlumbJob.cs,
and then I have public ArrayList with this objects in it.

When I select the JobType on TabPage1 I initialize this class, and scroll
through the Controls in this ArrayList, and try and create instances of them
on my TabPage like this:

foreach(Control ctTemplate in PlumbJob.ObjectArray)
{
Control obj = new
Control(tpWizard2,ctTemplate.Text,ctTemplate.Left, ctTemplate.Top,ctTemplate.Width,ctTemplate.Height) ;
obj.Name = ctTemplate.Name;
obj.Visible = true;
}

but I cant see the objects on my tab page. Any ideas?? Is this a viable way
of doing what I am trying to achieve?


That's creating controls, but not adding them to anything. You probably
want to use Controls.Add for each newly created control, to add them to
the current form's list of controls.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3

P: n/a
Steve <St***@discussions.microsoft.com> wrote:
hmm, still no joy. These objects are to sit on a TabPage, on my main form, so
I presume as I create them I need to add them to the controls collection for
that tabpage, yes?
Yup.
So I did this:

foreach(Control ctTemp in ProcessAO.ObjectArray){
Control ctObj = new
Control(ctTemp.Text,ctTemp.Left,ctTemp.Top,ctTemp. Width,ctTemp.Height);
tpWizard2.Controls.Add(ctObj);
}

when I run it though I can see no controls at all on this page. If I debug
and foreach through each control on my tpWizard page I can see that they are
all there, but they are not visible. Am I forgetting something? Or am I
doing things in the wrong order?


Not sure, to be honest.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4

P: n/a
I have a simple form, with a tabcontrol on it, on the first page there is a
button, behind this button is this code:

private void button1_Click(object sender, System.EventArgs e)
{
ProcessAsset ProcAO = new ProcessAsset();
ProcAO.InitializeComponent();
foreach(Control ctTmp in ProcAO.ObjectArray)
{
Control ctObj = new
Control(ctTmp.Text,ctTmp.Left,ctTmp.Top,ctTmp.Widt h,ctTmp.Height);
ctObj.Visible = true;
this.Page2.Controls.Add(ctObj);
}
tabControl1.SelectedTab = Page2;
}

I have a second .cs file in this solution called ProcessAsset.cs, in there I
have this code:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace SWOG
{
public class ProcessAsset
{
public System.Windows.Forms.Label siteLabel;
public System.Windows.Forms.TextBox txtSite;
public System.Windows.Forms.Button btnNext;
public ArrayList ObjectArray = new ArrayList();

public ProcessAsset()
{
InitializeComponent();
ObjectArray.Add(this.siteLabel);
ObjectArray.Add(this.txtSite);
ObjectArray.Add(this.btnNext);
}

public void InitializeComponent()
{
this.siteLabel = new System.Windows.Forms.Label();
this.txtSite = new System.Windows.Forms.TextBox();
this.btnNext = new System.Windows.Forms.Button();
this.siteLabel.Location = new System.Drawing.Point(88, 32);
this.siteLabel.Name = "siteLabel";
this.siteLabel.Size = new System.Drawing.Size(48, 23);
this.siteLabel.TabIndex = 0;
this.siteLabel.Text = "Site";
this.siteLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.siteLabel.Visible = true;
this.txtSite.Location = new System.Drawing.Point(144, 32);
this.txtSite.Name = "txtSite";
this.txtSite.TabIndex = 1;
this.txtSite.Text = "";
this.txtSite.Visible = true;
this.btnNext.Location = new System.Drawing.Point(448, 272);
this.btnNext.Name = "btnNext";
this.btnNext.TabIndex = 2;
this.btnNext.Text = "Next >>";
this.btnNext.Visible = true;
}
}
}
When I click the button on page1 i want the objects created in
ProcessAsset.cs to be created on Page 2. At the moment all that happens is
that the tab changes page.
Thanks

Jul 21 '05 #5

P: n/a
SOLVED

If I create a tabpage control first then add the controls to this before
finally adding that tabpage to my tabcontrol then it works fine.

But I think the real answer to my problem is UserControls. I can create a
UserControl for each one of my JobTypes, then at runtime I just add the
relevant UserControl onto the tabpage.

Thanks anyway.

"Steve" wrote:
I have a simple form, with a tabcontrol on it, on the first page there is a
button, behind this button is this code:

private void button1_Click(object sender, System.EventArgs e)
{
ProcessAsset ProcAO = new ProcessAsset();
ProcAO.InitializeComponent();
foreach(Control ctTmp in ProcAO.ObjectArray)
{
Control ctObj = new
Control(ctTmp.Text,ctTmp.Left,ctTmp.Top,ctTmp.Widt h,ctTmp.Height);
ctObj.Visible = true;
this.Page2.Controls.Add(ctObj);
}
tabControl1.SelectedTab = Page2;
}

I have a second .cs file in this solution called ProcessAsset.cs, in there I
have this code:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace SWOG
{
public class ProcessAsset
{
public System.Windows.Forms.Label siteLabel;
public System.Windows.Forms.TextBox txtSite;
public System.Windows.Forms.Button btnNext;
public ArrayList ObjectArray = new ArrayList();

public ProcessAsset()
{
InitializeComponent();
ObjectArray.Add(this.siteLabel);
ObjectArray.Add(this.txtSite);
ObjectArray.Add(this.btnNext);
}

public void InitializeComponent()
{
this.siteLabel = new System.Windows.Forms.Label();
this.txtSite = new System.Windows.Forms.TextBox();
this.btnNext = new System.Windows.Forms.Button();
this.siteLabel.Location = new System.Drawing.Point(88, 32);
this.siteLabel.Name = "siteLabel";
this.siteLabel.Size = new System.Drawing.Size(48, 23);
this.siteLabel.TabIndex = 0;
this.siteLabel.Text = "Site";
this.siteLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.siteLabel.Visible = true;
this.txtSite.Location = new System.Drawing.Point(144, 32);
this.txtSite.Name = "txtSite";
this.txtSite.TabIndex = 1;
this.txtSite.Text = "";
this.txtSite.Visible = true;
this.btnNext.Location = new System.Drawing.Point(448, 272);
this.btnNext.Name = "btnNext";
this.btnNext.TabIndex = 2;
this.btnNext.Text = "Next >>";
this.btnNext.Visible = true;
}
}
}
When I click the button on page1 i want the objects created in
ProcessAsset.cs to be created on Page 2. At the moment all that happens is
that the tab changes page.
Thanks

Jul 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.