PC Review


Reply
Thread Tools Rate Thread

Can I inherit controls from another class at runtime??

 
 
=?Utf-8?B?U3RldmU=?=
Guest
Posts: n/a
 
      23rd Jun 2005
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

 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      23rd Jun 2005
Steve <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?Utf-8?B?U3RldmU=?=
Guest
Posts: n/a
 
      23rd Jun 2005
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 <(E-Mail Removed)> 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 - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      23rd Jun 2005
Steve <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?Utf-8?B?U3RldmU=?=
Guest
Posts: n/a
 
      24th Jun 2005
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.Width,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

 
Reply With Quote
 
=?Utf-8?B?U3RldmU=?=
Guest
Posts: n/a
 
      24th Jun 2005
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.Width,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
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Managed class inherit from unmanaged class ttc Microsoft C# .NET 5 10th Dec 2007 10:39 AM
Can it possible redefine class with subclass when inherit base class with sub class? ABC Microsoft C# .NET 4 11th Jan 2006 12:13 AM
Web service class, inherit from asmx class free.teranews.com Microsoft Dot NET 1 18th Feb 2005 01:31 AM
Web service class, inherit from asmx class free.teranews.com Microsoft C# .NET 1 18th Feb 2005 01:31 AM
Accessing session data in a class file- which class do I inherit from? Kevin Spencer Microsoft ASP .NET 6 23rd Apr 2004 01:32 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:22 PM.