473,238 Members | 1,909 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,238 software developers and data experts.

Can I inherit controls from another class at runtime??

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
5 1547
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Earl Teigrob | last post by:
I am writing an application that dynamically loads user controls at run time based on user options. I would like to give my users the ability to build their own user controls and add them to my...
4
by: David | last post by:
I have trying to have a webform inherit controls from another form and can't get it to work Say I have a form that saves the person's demographic info. ****one.aspx**** //I have an object...
3
by: Jill Graham | last post by:
Hi folks, The pages of my website are built dynamically and are based on templates. A template can look like this : <table> <tr><td>This is the page header</td></tr> <tr><td>This is the...
5
by: Steve | last post by:
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...
8
by: mark.norgate | last post by:
I've run into a few problems trying to use generics for user controls (classes derived from UserControl). I'm using the Web Application model rather than the Web Site model. The first problem...
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
3
by: Yoavo | last post by:
Hi, I have a dialog with 2 TextBox controls. I want to add some functionality to one of the them. I created a class MyTextBox which inherits from TextBox. How can I connect one of the TextBox...
2
by: Joe | last post by:
Is it possible to inherit from a UserControl? If I try my user control class is not recognized. Thanks, Joe
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.