Not understanding simple concept

  • Thread starter Thread starter Bryan
  • Start date Start date
B

Bryan

Lets say I have a form From1, and I add a text box to it, TextBox1, in
the VS2005 form designer. I set the 'Text' property of TextBox1 to
"Original" using the properties dialog box in the form designer.
Consider the following code that runs on the loading of the form

dim txt as new TextBox
txt.Text = "changed"
Me.TextBox1 = txt

I was thinking that when I open the form the text in the textbox would
read "changed", but it still reads "original". Why does this not work?

I wan't to be able to create an object at runtime, then set a control
that I created at designtime equal to the runtime version.
 
What you have done is created a new instance of textbox but not added it to
the controls collection of the parent form. So there are now two instances,
the original and your new instance.

To get your "changed" instance onto the form, you should set it's position
and then add it to the controls collection of the parent form. You also
need to "AddHandler" for any events you want to handle on the text box.
 
Thanks for the response. Wouldn't that leave me with two textboxes on
my form though? I only want one. I basically want to replace all the
old one's properties with the new one's properties i guess. I thought
this would be done with the equals operator '='.
 
Bryan said:
Thanks for the response. Wouldn't that leave me with two textboxes on
my form though? I only want one. I basically want to replace all the
old one's properties with the new one's properties i guess. I thought
this would be done with the equals operator '='.

Well you are basically wanting to use the Prototype pattern. If you want to
implement it like this, then you can always make the prototype invisible
(visible = false). You might also consider deriving a new class from
TextBox and implementing the IClonable interface, to make cloning (creating
a new instance from the prototype) easier. However not knowing what
application you have in mind it's quite difficult to suggest a better way
;).
 
Did you add the control to the control collection?

Myform.controls.add(MyTextBox)

This is done in the designer class of your form in vb2005. But it is hidden.

You will also have to set the Location, Size, and Color.


--
Thiele Enterprises - The Power Is In Your Hands Now!

--
Lets say I have a form From1, and I add a text box to it, TextBox1, in
the VS2005 form designer. I set the 'Text' property of TextBox1 to
"Original" using the properties dialog box in the form designer.
Consider the following code that runs on the loading of the form

dim txt as new TextBox
txt.Text = "changed"
Me.TextBox1 = txt

I was thinking that when I open the form the text in the textbox would
read "changed", but it still reads "original". Why does this not work?

I wan't to be able to create an object at runtime, then set a control
that I created at designtime equal to the runtime version.
 
Bryan said:
Thanks for the response. Wouldn't that leave me with two textboxes on
my form though? I only want one. I basically want to replace all the
old one's properties with the new one's properties i guess. I thought
this would be done with the equals operator '='.

Is there a particular reason you need to create a *new* textbox? Why
not just alter the parameters of the old one:

TextBox1.Text = "Changed"

Chris
 
Chris said:
Is there a particular reason you need to create a *new* textbox? Why
not just alter the parameters of the old one:

TextBox1.Text = "Changed"

Chris

Because in reality I actually built a class that builds a Developer
Express XtraGrid (fancy datagrid) based on its properties that are set
at runtime (TextBox was just an example). This class holds the
finished XtraGrid control in a member. Sometimes I want to create a
form and add this control to the form at runtime, like some of the
above suggestions. But other times, I already have an XtraGrid on a
form that was placed in design mode and has some code behind its events
already. In this case I simply want the design time XtraGrid to
inherit all the properties of the runtime control, but keep the same
events.
 
Back
Top