Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

User Controls, Accessing SubControls from a Windows Form

Status
Not open for further replies.

JasonSummers

Programmer
Sep 11, 2002
26
US

Using Microsoft Visual C#.net Express Edition Version 8.050727.42
Using .NET Framework 2.0.50727



I have been studying C#.net for only a few months now. Please forgive me if I am not using correct vocabulary to describe the problem I am having.


Completed Tasks - Design a User Control (ucDataFieldProperties) that I can drag onto a Windows Form open in the design window. The user control will be part of a Windows Form whos purpose is to generate the SQL string for creating a SQL table at runtime.

User Control Details - User Control ucDataFieldProperties contains an assortment of textboxes, comboboxes, and checkboxes. To keep things simple, I will address only three of the form controls that I have added to my User Control ucDataFieldProperties. (Is it correct to call this a Composite Control?)

Laid out in a row, I have Checkbox ckIsKey, TextBox tbFieldName, and ComboBox cboDataType. The text property value of tbFieldName and cboDataType and the CheckState property of the ckYesKey will (after being processed by some formatting logic) create the bulk of a 'CREATE TABLE' SQL String to be executed at runtime.

HERE IS MY MAIN PROBLEM. While using the User Control on a Windows Form I have trouble accessing values of Objects that are on the User Control. In my mind, I thought I would be able to grab the text value of a control on my User Control by trying something like this

whatever = ucDataFieldProperties.tbFieldName.Text.ToString(); Oops...that does not work.

What I tried next was to add a public variable to act like a property of my User Control. I did this for the text value and color value for each control on my User Control ucDataFieldProperties. Below is an example of how I handled this for tbFieldName. (I have removed all of the other control code to simplify)

public partial class ucDateFieldProperties : UserControl
{
// Handles the Color of the TextBox tbFieldName
private Color tbFieldNameColor

public Color FieldName_TextBoxColor

{
get
{
return tbFieldNameColor;
}

set
{
tbFieldNameColor = value;
tbFieldName.BackColor = tbFColor;
}
}

// Handles the Text of the TextBox tbFieldName

public string tbFieldName_Text
{
get
{
return this.tbFieldName.Text;
}
set
{
this.tbFieldName.Text = value;
}
}

Now when I use User Control ucDataFieldProperties, I have access to the tbFieldName text via the FieldName_TextBoxColor property of ucDataFieldProperties. Knowing that I may have 10 or more of these controls on a Windows form, I now want to iterate through the Text values of the textboxes and comboboxes on my form to facilitate construction of a CREATE TABLE SQL statement. Unfortunately, if I create an array of my User Controls (so I can iterate through them and grab values) I can't access their subcontrols text properties like I could if I working with an instance of the control.

for example - this works fine.

private void TestButton_Click(object sender, EventArgs e)
{
MessageBox.Show(ucMakeTableFieldEntry1.tbFieldName_Text.ToString());
}


but his does not

Multiple attempts at iterating through the controls using for or foreach. I was unable to ever access the tbFieldName_Text and extract it from each item UserControl item in the array.

Well, I decided to quit typing here, this post is getting a little long. I think that there is probably an easier way to do this. Any ideas people?
 
That's strange...

You got the Properties section right on your usercontrol but I don't see why you can't iterate through them.

Bascially, your usercontrol should have the properties you want to retrieve:

public string FieldName
{
get
{
return txtFieldName.Text;
}
}

Then you should add these controls to a panel or something on your main form.

panel1.Controls.Add(new MyUserControl());

Then you could loop through all the usercontrols in that panel's control collection to get all the values you need.

foreach (MyUserControl control in panel1.Controls)
{
Console.WriteLine(control.FieldName);
}

And that should output all the field name values for each instance of your usercontrol contained in panel1.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top