J
Jeffrey Tan[MSFT]
Hi Jason,
Sorry, I still can not reproduce your problem, here is my sample code:
private void button1_Click(object sender, System.EventArgs e)
{
Assembly assem = Assembly.GetAssembly(typeof(Form));
Type t = assem.GetType("System.Windows.Forms.TextBox");
object obj = Activator.CreateInstance(t);
Control tb = (Control)obj;
this.Controls.Add(tb);
tb.Font = this.Font;
this.optionsGroupBox.Controls.Add(tb);
tb.Name = "dynamictextbox";
object tag=(object)"textboxtag";
tb.Tag = tag;
if (tb.GetType() == typeof(TextBox))
{
TextBox tbox = (TextBox)tb;
tbox.Text = "textboxtag";
}
}
private void button2_Click(object sender, System.EventArgs e)
{
foreach(Control c in optionsGroupBox.Controls)
{
MessageBox.Show(c.Tag.ToString());
}
}
I have set the control's tag property and show it up. It works well on my
machine.
I think you can try this code on your machine.
If I misunderstand you, please feel free to let me know.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
| From: "Jason" <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<#U6#[email protected]>
<[email protected]>
<eB07oT#[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: Help Please
| Date: Wed, 5 Nov 2003 09:05:15 -0700
| Lines: 249
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 64.207.45.37
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:196957
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks for your post. When I loop through the groupbox Controls that I
| created some of them are undefined. I get an error based on some of the
| controls I created (textboxes and combo boxes and not labels) when I check
| the tag value. Here is the error I get:
|
| An unhandled exception of type 'System.NullReferenceException' occurred in
| VeraDrill.exe
| Additional information: Object reference not set to an instance of an
| object.
|
| When I debug I place the cursor over the control when it gets to the combo
| box I created and it says ct = undefined value where it should say ct =
| System.Windows.Forms.TextBox.
|
| I do not know why this is happening! If the controls are getting created
| correctly and showing up on the form why would some of the controls be
| undefined?
|
| Thanks
|
| | >
| > Hi Jason,
| >
| > I am glad your application finally works.
| > It seems that your new problem related to the tag and loop through the
| > groupbox.
| > As you said: "For some reason the textboxes and comboboxes are undefined
| > controls but the labels are not.", what does undefined mean?
| > Does this sentence mean that textbox and combobox do not have the tag
| > property, while the label has the tag?
| >
| > If I did not misunderstand you, I think you should specify a special
value
| > to tag property for the control that does not have tag property.(Such
as 0
| > or null)
| > Then, when you loop through the groupbox, you can determine the tag's
| > value, and use these controls that have the valid tag value.
| >
| > If I misunderstand you, please feel free to tell me.
| >
| > Best regards,
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | From: "Jason" <[email protected]>
| > | References: <[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <#[email protected]>
| > <#U6#[email protected]>
| > <[email protected]>
| > <eB07oT#[email protected]>
| > <[email protected]>
| > <#[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <[email protected]>
| > | Subject: Re: Help Please
| > | Date: Tue, 4 Nov 2003 09:54:46 -0700
| > | Lines: 149
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <[email protected]>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: 64.207.45.37
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.languages.csharp:196636
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > | Thanks for all your help. Yes, you did help me solve my problem.
| > However,
| > | I am having some problems. The controls are being created correctly
and
| > | displaying correctly in a group box. However, I am trying to loop
| through
| > | each control in the groupbox and looking at each tag. For some reason
| the
| > | textboxes and comboboxes are undefined controls but the labels are
not.
| > | Here is the code I use to create the control:
| > |
| > | Assembly assem = Assembly.GetAssembly(typeof(Form));
| > | Type t = assem.GetType(controlType);
| > | object obj = Activator.CreateInstance(t);
| > | Control tb = (Control)obj;
| > | this.Controls.Add(tb);
| > | tb.Font = this.Font;
| > | this.optionsGroupBox.Controls.Add(tb);
| > | tb.Name = controlName;
| > | tb.Tag = tag;
| > | if (tb.GetType() == typeof(Label))
| > | {
| > | Label label = (Label)tb;
| > | label.Text = controlText;
| > | label.TextAlign = ContentAlignment.MiddleLeft;
| > | }
| > | else if(tb.GetType() == typeof(ComboBox))
| > | {
| > | ComboBox cbo = (ComboBox)tb;
| > | cbo.DropDownStyle = ComboBoxStyle.DropDownList;
| > | Delegate d = Delegate.CreateDelegate(typeof(EventHandler), this,
| > | controlEvent);
| > | cbo.Click += (EventHandler)d;
| > | }
| > | tb.SetBounds(locX, locY, sizeX, sizeY);
| > |
| > | And I just use a simple foreach(Control ct in
| > this.optionsGroupBox.Controls)
| > | to loop through each control.
| > |
| > | Any ideas?
| > |
| > | Thanks
| > |
| > | | > | >
| > | > Hi Dan,
| > | >
| > | > Is your problem resolved?
| > | > If you still have any question, please feel free to tell me.
| > | >
| > | > Best regards,
| > | > Jeffrey Tan
| > | > Microsoft Online Partner Support
| > | > Get Secure! - www.microsoft.com/security
| > | > This posting is provided "as is" with no warranties and confers no
| > rights.
| > | >
| > | > --------------------
| > | > | From: "Dan" <[email protected]>
| > | > | References: <[email protected]>
| > | > <[email protected]>
| > | > <[email protected]>
| > | > <#[email protected]>
| > | > <#U6#[email protected]>
| > | > <[email protected]>
| > | > <eB07oT#[email protected]>
| > | > <[email protected]>
| > | > <#[email protected]>
| > | > <[email protected]>
| > | > | Subject: Re: Help Please
| > | > | Date: Thu, 23 Oct 2003 15:38:05 -0600
| > | > | Lines: 52
| > | > | X-Priority: 3
| > | > | X-MSMail-Priority: Normal
| > | > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | > | Message-ID: <[email protected]>
| > | > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | > | NNTP-Posting-Host: 64.207.45.37
| > | > | Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| > | > | Xref: cpmsftngxa06.phx.gbl
| > | microsoft.public.dotnet.languages.csharp:193654
| > | > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > | > |
| > | > | Thanks for the response. For my problem I do not think I need to
| use
| > | > | CodeDOM. I rather not make it so complicated that it is hard to
| > | maintain.
| > | > | Saving C# code in the table and then using CodeDOM to run the code
| at
| > | > | runtime will make it database driven however isnt there a better
way
| > to
| > | do
| > | > | this? I understand I could create one method which I add to each
| > control
| > | > and
| > | > | then have a case statement which points to the right method.
| > However, I
| > | > was
| > | > | hoping to have methods created at design which would then be
called
| > via
| > | > the
| > | > | controlEvent name. However, I cannot figure this out. I am
looking
| > at
| > | > the
| > | > | Invoke method and create a delegate to point to the right method.
I
| > | could
| > | > | create a hashtable of delegates mapping to each method. Any other
| > | > | suggestions would be appreciated. Thanks again for all your help.
| > | > |
| > | > | Thanks
| > | > |
| > | > |
| > | > | | > | > | > Dan wrote:
| > | > | > > Jeffery,
| > | > | > >
| > | > | > > Your example was very helpful. I do have one more question.
| How
| > | can
| > | > I
| > | > | add
| > | > | > > an event to the control by having the event name in the
| database.
| > I
| > | > | > > understand how to add event to a control however since each
| > control
| > | > | needs a
| > | > | > > different method to handle each event. I decided to create in
| the
| > | > table
| > | > | a
| > | > | > > field to have the event name which I will create the method in
| > | design
| > | > | view
| > | > | > > and I want to assign it at runtime. The code below gives me
an
| > | error
| > | > | > > (control event does not exist) :
| > | > | > >
| > | > | > > cbo.Click += new EventHandler(controlEvent);
| > | > | > >
| > | > | > > With controlEvent being a string of the method name.
| > | > | > >
| > | > | > > Any ideas how to accomplish this?
| > | > | > >
| > | > | > > Thanks again for all your help.
| > | > | > >
| > | > | > </snip>
| > | > | > I dont think you want to do that. You might be better off having
| the
| > | > | > controlEvent be "one" method and within it, you can do the
| different
| > | > | > things based on a table data. You can probably store C# code or
| > | JScript
| > | > | > or some custom language code in the table which you can in turn,
| > using
| > | > | > CodeDOM compiler classes, compile and execute on event..
| > | > | >
| > | > | > --
| > | > | > Girish Bharadwaj
| > | > | >
| > | > |
| > | > |
| > | > |
| > | >
| > |
| > |
| > |
| >
|
|
|
Sorry, I still can not reproduce your problem, here is my sample code:
private void button1_Click(object sender, System.EventArgs e)
{
Assembly assem = Assembly.GetAssembly(typeof(Form));
Type t = assem.GetType("System.Windows.Forms.TextBox");
object obj = Activator.CreateInstance(t);
Control tb = (Control)obj;
this.Controls.Add(tb);
tb.Font = this.Font;
this.optionsGroupBox.Controls.Add(tb);
tb.Name = "dynamictextbox";
object tag=(object)"textboxtag";
tb.Tag = tag;
if (tb.GetType() == typeof(TextBox))
{
TextBox tbox = (TextBox)tb;
tbox.Text = "textboxtag";
}
}
private void button2_Click(object sender, System.EventArgs e)
{
foreach(Control c in optionsGroupBox.Controls)
{
MessageBox.Show(c.Tag.ToString());
}
}
I have set the control's tag property and show it up. It works well on my
machine.
I think you can try this code on your machine.
If I misunderstand you, please feel free to let me know.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
| From: "Jason" <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<#U6#[email protected]>
<[email protected]>
<eB07oT#[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: Help Please
| Date: Wed, 5 Nov 2003 09:05:15 -0700
| Lines: 249
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 64.207.45.37
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:196957
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks for your post. When I loop through the groupbox Controls that I
| created some of them are undefined. I get an error based on some of the
| controls I created (textboxes and combo boxes and not labels) when I check
| the tag value. Here is the error I get:
|
| An unhandled exception of type 'System.NullReferenceException' occurred in
| VeraDrill.exe
| Additional information: Object reference not set to an instance of an
| object.
|
| When I debug I place the cursor over the control when it gets to the combo
| box I created and it says ct = undefined value where it should say ct =
| System.Windows.Forms.TextBox.
|
| I do not know why this is happening! If the controls are getting created
| correctly and showing up on the form why would some of the controls be
| undefined?
|
| Thanks
|
| | >
| > Hi Jason,
| >
| > I am glad your application finally works.
| > It seems that your new problem related to the tag and loop through the
| > groupbox.
| > As you said: "For some reason the textboxes and comboboxes are undefined
| > controls but the labels are not.", what does undefined mean?
| > Does this sentence mean that textbox and combobox do not have the tag
| > property, while the label has the tag?
| >
| > If I did not misunderstand you, I think you should specify a special
value
| > to tag property for the control that does not have tag property.(Such
as 0
| > or null)
| > Then, when you loop through the groupbox, you can determine the tag's
| > value, and use these controls that have the valid tag value.
| >
| > If I misunderstand you, please feel free to tell me.
| >
| > Best regards,
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | From: "Jason" <[email protected]>
| > | References: <[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <#[email protected]>
| > <#U6#[email protected]>
| > <[email protected]>
| > <eB07oT#[email protected]>
| > <[email protected]>
| > <#[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <[email protected]>
| > | Subject: Re: Help Please
| > | Date: Tue, 4 Nov 2003 09:54:46 -0700
| > | Lines: 149
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <[email protected]>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: 64.207.45.37
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.languages.csharp:196636
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > | Thanks for all your help. Yes, you did help me solve my problem.
| > However,
| > | I am having some problems. The controls are being created correctly
and
| > | displaying correctly in a group box. However, I am trying to loop
| through
| > | each control in the groupbox and looking at each tag. For some reason
| the
| > | textboxes and comboboxes are undefined controls but the labels are
not.
| > | Here is the code I use to create the control:
| > |
| > | Assembly assem = Assembly.GetAssembly(typeof(Form));
| > | Type t = assem.GetType(controlType);
| > | object obj = Activator.CreateInstance(t);
| > | Control tb = (Control)obj;
| > | this.Controls.Add(tb);
| > | tb.Font = this.Font;
| > | this.optionsGroupBox.Controls.Add(tb);
| > | tb.Name = controlName;
| > | tb.Tag = tag;
| > | if (tb.GetType() == typeof(Label))
| > | {
| > | Label label = (Label)tb;
| > | label.Text = controlText;
| > | label.TextAlign = ContentAlignment.MiddleLeft;
| > | }
| > | else if(tb.GetType() == typeof(ComboBox))
| > | {
| > | ComboBox cbo = (ComboBox)tb;
| > | cbo.DropDownStyle = ComboBoxStyle.DropDownList;
| > | Delegate d = Delegate.CreateDelegate(typeof(EventHandler), this,
| > | controlEvent);
| > | cbo.Click += (EventHandler)d;
| > | }
| > | tb.SetBounds(locX, locY, sizeX, sizeY);
| > |
| > | And I just use a simple foreach(Control ct in
| > this.optionsGroupBox.Controls)
| > | to loop through each control.
| > |
| > | Any ideas?
| > |
| > | Thanks
| > |
| > | | > | >
| > | > Hi Dan,
| > | >
| > | > Is your problem resolved?
| > | > If you still have any question, please feel free to tell me.
| > | >
| > | > Best regards,
| > | > Jeffrey Tan
| > | > Microsoft Online Partner Support
| > | > Get Secure! - www.microsoft.com/security
| > | > This posting is provided "as is" with no warranties and confers no
| > rights.
| > | >
| > | > --------------------
| > | > | From: "Dan" <[email protected]>
| > | > | References: <[email protected]>
| > | > <[email protected]>
| > | > <[email protected]>
| > | > <#[email protected]>
| > | > <#U6#[email protected]>
| > | > <[email protected]>
| > | > <eB07oT#[email protected]>
| > | > <[email protected]>
| > | > <#[email protected]>
| > | > <[email protected]>
| > | > | Subject: Re: Help Please
| > | > | Date: Thu, 23 Oct 2003 15:38:05 -0600
| > | > | Lines: 52
| > | > | X-Priority: 3
| > | > | X-MSMail-Priority: Normal
| > | > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | > | Message-ID: <[email protected]>
| > | > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | > | NNTP-Posting-Host: 64.207.45.37
| > | > | Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| > | > | Xref: cpmsftngxa06.phx.gbl
| > | microsoft.public.dotnet.languages.csharp:193654
| > | > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > | > |
| > | > | Thanks for the response. For my problem I do not think I need to
| use
| > | > | CodeDOM. I rather not make it so complicated that it is hard to
| > | maintain.
| > | > | Saving C# code in the table and then using CodeDOM to run the code
| at
| > | > | runtime will make it database driven however isnt there a better
way
| > to
| > | do
| > | > | this? I understand I could create one method which I add to each
| > control
| > | > and
| > | > | then have a case statement which points to the right method.
| > However, I
| > | > was
| > | > | hoping to have methods created at design which would then be
called
| > via
| > | > the
| > | > | controlEvent name. However, I cannot figure this out. I am
looking
| > at
| > | > the
| > | > | Invoke method and create a delegate to point to the right method.
I
| > | could
| > | > | create a hashtable of delegates mapping to each method. Any other
| > | > | suggestions would be appreciated. Thanks again for all your help.
| > | > |
| > | > | Thanks
| > | > |
| > | > |
| > | > | | > | > | > Dan wrote:
| > | > | > > Jeffery,
| > | > | > >
| > | > | > > Your example was very helpful. I do have one more question.
| How
| > | can
| > | > I
| > | > | add
| > | > | > > an event to the control by having the event name in the
| database.
| > I
| > | > | > > understand how to add event to a control however since each
| > control
| > | > | needs a
| > | > | > > different method to handle each event. I decided to create in
| the
| > | > table
| > | > | a
| > | > | > > field to have the event name which I will create the method in
| > | design
| > | > | view
| > | > | > > and I want to assign it at runtime. The code below gives me
an
| > | error
| > | > | > > (control event does not exist) :
| > | > | > >
| > | > | > > cbo.Click += new EventHandler(controlEvent);
| > | > | > >
| > | > | > > With controlEvent being a string of the method name.
| > | > | > >
| > | > | > > Any ideas how to accomplish this?
| > | > | > >
| > | > | > > Thanks again for all your help.
| > | > | > >
| > | > | > </snip>
| > | > | > I dont think you want to do that. You might be better off having
| the
| > | > | > controlEvent be "one" method and within it, you can do the
| different
| > | > | > things based on a table data. You can probably store C# code or
| > | JScript
| > | > | > or some custom language code in the table which you can in turn,
| > using
| > | > | > CodeDOM compiler classes, compile and execute on event..
| > | > | >
| > | > | > --
| > | > | > Girish Bharadwaj
| > | > | >
| > | > |
| > | > |
| > | > |
| > | >
| > |
| > |
| > |
| >
|
|
|