Exposing UserControl Constituents

G

Guest

MAIN FORM: In my main form I have a treeview control docked on the left and a
panel control filling the rest of the form.
USER CONTROLS: I am constructing separate user controls to be displayed
within the content area of the form when a tree node is clicked. Each user
control has a panel control containing varius controls, ie textboxes, labels,
datagrid.

QUESTION: How can I expose the constituent controls on the user control so
that I can reference them from within my main form code?

Thanks.
 
I

Imran Koradia

One way would be to change the access modifier for the controls to public (I
believe the detault is Friend (Internal) in VB and Private in C#). However,
that way, anyone using your user controls will be able to access all
properties of the constituent controls which may or may not be desirable.
You could only expose the properties that you would want to access (and
allow others to access) from the application using your user control.

for example, this one just returns the text of a textbox in your user
control.

ReadOnly Property TextBoxText() As String
Get
Return Me.TextBox1.Text
End Get
End Property


hope that helps..
Imran.
 
G

Guest

I don't want to make each of the private controls public. Also, I think I
tried this and it didn't work. I found some posted code on another site, but
it's for VB, and haven't got it to work for C# yet:

Public Property Get Controls() as Object
Set Controls = UserControl.Controls
End Property

I tried to port it to C#, but don't think I've got the syntax correct:
public Object controls
{
set
{
controls = this.customPanel1.Controls;
}
get
{
return(controls);
}
}

Thanks for your assistance! -Jokra
 
I

Imran Koradia

Jokra said:
I don't want to make each of the private controls public. Also, I think I
tried this and it didn't work. I found some posted code on another site, but
it's for VB, and haven't got it to work for C# yet:

Public Property Get Controls() as Object
Set Controls = UserControl.Controls
End Property

I tried to port it to C#, but don't think I've got the syntax correct:
public Object controls
{
set
{
controls = this.customPanel1.Controls;
}
get
{
return(controls);
}
}

I assume you only want to be able to get the controls collection rather than
set it, right? In that case, here's the syntax:

public Object Controls
{
get
{
return(this.customPanel1.Controls);
}
}


hope that helps..
Imran.
 
G

Guest

Imran-

Thank you so much. I ended up doing exactly what you first suggested,
setting the constituent controls modifier property to Public, then set the
usercontrol modifier property to Public within the main form.

Thanks for your help!

-Jokra
 

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