473,387 Members | 3,801 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,387 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 1550
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
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.