Find control on inherited form from base class

T

TS

I have a base class that has a form that inherits from it. On this form is a
control I need to get a handle to. How do I get this handle from the base
class? I don't have a FindControl that i've been able to get intellisense on
like I do in a web form.

thanks for any help!
 
H

Herfried K. Wagner [MVP]

* "TS said:
I have a base class that has a form that inherits from it. On this form is a
control I need to get a handle to. How do I get this handle from the base
class? I don't have a FindControl that i've been able to get intellisense on
like I do in a web form.

\\\
Private Function FindControl( _
ByVal ControlName As String, _
ByVal CurrentControl As Control _
) As Control
Dim ctr As Control
For Each ctr In CurrentControl.Controls
If ctr.Name = ControlName Then
Return ctr
Else
ctr = FindControl(ControlName, ctr)
If Not ctr Is Nothing Then
Return ctr
End If
End If
Next ctr
End Function
///

Usage:

\\\
DirectCast(FindControl("Button1", Me), Button).Enabled = False
///

Notice that the procedure listed above is "slow", if you have to access a
lot of controls by name very often, you should store references to them in a
'Hashtable' object. You can use the name of the control as key:

\\\
Private m_Controls As New Hashtable()
///

Adding a control:

\\\
Dim DynamicPictureBox As New PictureBox()
DynamicPictureBox.Name = "PictureBox1"
m_Controls.Add(DynamicPictureBox.Name, DynamicPictureBox)
///

Looking for a control:

\\\
Dim p As PictureBox = DirectCast(m_Controls.Item("PictureBox1"), PictureBox)
///

Removing a control:

\\\
m_Controls.Remove("PictureBox1")
///

Sometimes it's even better to add the control to an array. This will allow
fast and easy index-based access to the control references:

\\\
Dim MyLabels() As Label = {Label1, Label2, ..., Label10}
///

Access by 'MyLabels(0)' to 'MyLabels(9)'.
 
T

TS

So this will find the control if we are on the form that actually has this
control. If we are not on this form, then the control will still be in the
hash table, but it won't be used on the form, so it is kind of useless,
right? (this is using the assumption that I am getting this control from the
hashtable in the code of the base class). Does it throw an error?

So using this hashtable methodology, does the controls inserted need to be
dynamically created as in your example?

I'm trying to understand this concept: It seems like this control lives in
the hash table, and its not a reference to a control that lives on the form.
So when you set a property of this control, it seems like it would set the
property to the control in the hashtable, and not to the actual control that
lives on the form, or do the two sync up, or are there 2 objects at all?

thanks a bunch!
 
J

Jeffrey Tan[MSFT]

Hi TS,

Thanks for your feedback.

The control references stored in the hashtable are ONLY reference, they are
not the actual control. When you did not create a control instance and
point this reference to the control instance, the reference is just null
reference in the hashtable. You can use "object !=null" statement to
determine every reference in the hashtable is a null reference.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

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.
 
T

TS

When you did not create a control instance and
point this reference to the control instance, the reference is just null
reference in the hashtable
You can use "object !=null" statement to
determine every reference in the hashtable is a null reference.

By null reference you mean "the reference references a null object"? If so,
I don't understand. If the reference in the hashtable points to this
control, say a button, on the form, then the reference is not null, it
points to the button on the form (assume the button was created at design
time). I did a test and added a button on my form to a hashtable, and that
item is not null.

I don't understand why you said "the reference is just null reference in the
hashtable"


"Jeffrey Tan[MSFT]" said:
Hi TS,

Thanks for your feedback.

The control references stored in the hashtable are ONLY reference, they are
not the actual control. When you did not create a control instance and
point this reference to the control instance, the reference is just null
reference in the hashtable. You can use "object !=null" statement to
determine every reference in the hashtable is a null reference.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

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.
 
T

TS

I think I understand this point you made "The control references stored in
the hashtable are ONLY reference". Tell me if i'm wrong, but say i have a
button on my form. this button is simply a reference to the instance of the
button class, so when I add this control to the hashtable, all i'm adding is
this same reference. I was thinking that the button I created at design time
was the object, but all objects are reference types, so when you create an
instance of a class and assign it to a variable, the variable only holds a
reference to the object in memory.

Please correct anything I said incorrectly.

Note: I still want an answer to the previous post I submitted

thanks
 
J

Jeffrey Tan[MSFT]

Hi TS,

Thanks for your feedback.

Yes, you statement is basically right. Actually, this is a concept issue,
has not much relation to with Windows Forms.

In .Net, all classes are referece type, only a few primitive types are
value type(such as int, double, and some structures). Reference type is
somewhat like a pointer in C language, it will point to some object(class
instance), if it did not refer any object, it will point to null. These
objects are allocated in heap. In .Net, there is no need for you to explict
free these objects, because there is GC in .Net to collect these memory in
heap.

Let's back to our issue, at runtime, if you created a button, you actually
created a button class instance, and there is a reference point to this
instance. When the reference count of this object drop to 0, it will be
suitable for GC to collect this object. When you add this button instance
to the Form.Controls property, there will be another reference point to the
button object, also, if you add the button object into HashTable, there
will be a third reference point to it.

To store the reference in Hashtable, you just got a convinient way to get
the button OBJECT's reference. This has nothing to do with the button
object self.

If your button is created at design-time, VS.net IDE will added a Form wide
reference for this Button instance as a private variable, like this:
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;

Also, in the "Windows Form Designer generated code"'s InitializeComponent()
method, VS.net IDE will add the following code for the button:
this.button1 = new System.Windows.Forms.Button();
this.button1.Location = new System.Drawing.Point(104, 224);
this.button1.Location = new System.Drawing.Point(104, 224);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(56, 40);
this.button1.TabIndex = 0;
this.button1.Text = "button1";

So the button instance is already created InitializeComponent() method,
which is called by Form1's constructor, button1 reference is already point
to this created Button instance.

Hope this makes sense to you.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

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.
 
J

Jeffrey Tan[MSFT]

Hi TS,

Thanks very much for your feedback. I am also glad my reply makes sense to
you. If you need further help, please feel free to post. 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top