Hi Eric,
Do you have the same problem as jason?
Sorry, you have the same email address as jason, I almost can not
distinguish you from jason.
As jason said, after creating out the controls in groupbox, he wanted to
loop through these controls and check the tag property which tells us if
this control should be removed. The problem is when referring to the tag
property, null reference exception will generate.(only for textbox etc..)
But as I pasted in last post, I have wrote a sample for this, but can not
reproduce his problem.(I also dynamic create the textbox, and loop through
the groupbox, then refer to the tag property). This sample works well on my
machine, I think you also can try this sample.
Thanks
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: "Eric" <sp**@hotmail.com>
| References: <uK**************@TK2MSFTNGP11.phx.gbl>
<TA**************@cpmsftngxa06.phx.gbl>
<01****************************@phx.gbl>
<#G**************@tk2msftngp13.phx.gbl>
<#U**************@TK2MSFTNGP12.phx.gbl>
<B7**************@cpmsftngxa06.phx.gbl>
<eB**************@tk2msftngp13.phx.gbl>
<ns**************@cpmsftngxa06.phx.gbl>
<#0**************@TK2MSFTNGP10.phx.gbl>
<eH**************@TK2MSFTNGP09.phx.gbl>
<u4**************@TK2MSFTNGP12.phx.gbl>
<AO**************@cpmsftngxa06.phx.gbl>
<uv**************@TK2MSFTNGP09.phx.gbl>
<vw**************@cpmsftngxa06.phx.gbl>
<uD**************@TK2MSFTNGP10.phx.gbl>
<i$*************@cpmsftngxa06.phx.gbl>
| Subject: Re: Help Please
| Date: Thu, 6 Nov 2003 14:48:12 -0700
| Lines: 367
| 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: <uK**************@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 64.207.45.37
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:197312
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Jason,
|
| Thanks for your help. The problem was when I was trying to loop through
| each control in the groupbox and removing them at the same time if the tag
| value was not correct. Was not using a counter to remove each item I was
| using the foreach loop.
|
| Thanks again
|
| ""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
| news:i$*************@cpmsftngxa06.phx.gbl...
| >
| > 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" <sp**@hotmail.com>
| > | References: <uK**************@TK2MSFTNGP11.phx.gbl>
| > <TA**************@cpmsftngxa06.phx.gbl>
| > <01****************************@phx.gbl>
| > <#G**************@tk2msftngp13.phx.gbl>
| > <#U**************@TK2MSFTNGP12.phx.gbl>
| > <B7**************@cpmsftngxa06.phx.gbl>
| > <eB**************@tk2msftngp13.phx.gbl>
| > <ns**************@cpmsftngxa06.phx.gbl>
| > <#0**************@TK2MSFTNGP10.phx.gbl>
| > <eH**************@TK2MSFTNGP09.phx.gbl>
| > <u4**************@TK2MSFTNGP12.phx.gbl>
| > <AO**************@cpmsftngxa06.phx.gbl>
| > <uv**************@TK2MSFTNGP09.phx.gbl>
| > <vw**************@cpmsftngxa06.phx.gbl>
| > | 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: <uD**************@TK2MSFTNGP10.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: 64.207.45.37
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.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
| > |
| > | ""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
| > | news:vw**************@cpmsftngxa06.phx.gbl...
| > | >
| > | > 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" <sp**@hotmail.com>
| > | > | References: <uK**************@TK2MSFTNGP11.phx.gbl>
| > | > <TA**************@cpmsftngxa06.phx.gbl>
| > | > <01****************************@phx.gbl>
| > | > <#G**************@tk2msftngp13.phx.gbl>
| > | > <#U**************@TK2MSFTNGP12.phx.gbl>
| > | > <B7**************@cpmsftngxa06.phx.gbl>
| > | > <eB**************@tk2msftngp13.phx.gbl>
| > | > <ns**************@cpmsftngxa06.phx.gbl>
| > | > <#0**************@TK2MSFTNGP10.phx.gbl>
| > | > <eH**************@TK2MSFTNGP09.phx.gbl>
| > | > <u4**************@TK2MSFTNGP12.phx.gbl>
| > | > <AO**************@cpmsftngxa06.phx.gbl>
| > | > | 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: <uv**************@TK2MSFTNGP09.phx.gbl>
| > | > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | > | NNTP-Posting-Host: 64.207.45.37
| > | > | Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP09.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
| > | > |
| > | > | ""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in
| message
| > | > | news:AO**************@cpmsftngxa06.phx.gbl...
| > | > | >
| > | > | > 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" <sp**@hotmail.com>
| > | > | > | References: <uK**************@TK2MSFTNGP11.phx.gbl>
| > | > | > <TA**************@cpmsftngxa06.phx.gbl>
| > | > | > <01****************************@phx.gbl>
| > | > | > <#G**************@tk2msftngp13.phx.gbl>
| > | > | > <#U**************@TK2MSFTNGP12.phx.gbl>
| > | > | > <B7**************@cpmsftngxa06.phx.gbl>
| > | > | > <eB**************@tk2msftngp13.phx.gbl>
| > | > | > <ns**************@cpmsftngxa06.phx.gbl>
| > | > | > <#0**************@TK2MSFTNGP10.phx.gbl>
| > | > | > <eH**************@TK2MSFTNGP09.phx.gbl>
| > | > | > | 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: <u4**************@TK2MSFTNGP12.phx.gbl>
| > | > | > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | > | > | NNTP-Posting-Host: 64.207.45.37
| > | > | > | Path:
| > cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.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
| > | > | > |
| > | > | > |
| > | > | > | "Girish Bharadwaj" <girishb@nowhere> wrote in message
| > | > | > | news:eH**************@TK2MSFTNGP09.phx.gbl...
| > | > | > | > 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
| > | > | > | >
| > | > | > |
| > | > | > |
| > | > | > |
| > | > | >
| > | > |
| > | > |
| > | > |
| > | >
| > |
| > |
| > |
| >
|
|
|