views:

51

answers:

2

Hi, I am a beginner for Visual Studio C# 2008. Currently, I am creating the program which requires me to have user input in one User Control item and this data is needed to be passed on to another User Control for arithmetic manipulation.

My first User Control is called Structure_Data. I will be getting input values from the textboxes named LengthB_txt, WidthB_txt and HeightB_txt.

These value inputs in the textboxes above will be accessed in a new UserControl called CollectionArea.

I do not know how to connect the User controls as well as to access the data. In my User Control: Collection Area, to make my final result appear in the Ad_txt textbox, I did the following codes. However, I got error :

'WindowsFormsApplication1.Structure_Data.LengthB_txt' is inaccessible due to its protection level

Please help me. I am stuck! =( Thanks...


private void Ad_txt_TextChanged(object sender, EventArgs e)
    {
      //  const double PI = 3.14159265;
        double Lb;
        double Wb;
        double Hb;

        // Get the input value for Dimensions: Length
        Lb = Convert.ToDouble(StructDataPass.LengthB_txt.Text);
        Wb = Convert.ToDouble(StructDataPass.WidthB_txt.Text);
        Hb = Convert.ToDouble(StructDataPass.HeightB_txt.Text);

        double Ad_temp=0;
        double result_temp1=0;
        result_temp1 = Math.Pow(3 * Hb, 2);
        Ad_temp = Lb*Wb*6*Hb*(Lb+Wb)+(Math.PI)*result_temp1;
        Ad_txt.Text = Convert.ToString(Ad_temp);

    }
A: 

You could make LengthB_txt public in your first user control but it's best to expose public properties instead. For instance if LengthB_txt represents a double, add the following to the user control:

public double LengthB {get {return double.Parse(LengthB_txt.Text); set {LengthB_txt.Text = value.ToString();}}

do this for all the properties you want to expose and then you can use them from outside code the following way:

StructDataPass.LengthB = 45;
double Ad_temp = StructDataPass.LengthB;
vc 74
Hi vc 74... Thank you so much for your answer! I did the following instructions but got this error "An object reference is required for the non-static field, method, or property WindowsFormsApplication1.Structure_Data.LengthB.get' " Does this mean that I need to set a value for Structure_Data.LengthB ? Structure_Data is the name of my user control 1.
Tiffiny
Hey!! I think it's solved cos I did this: Structure_Data StructData = new Structure_Data(); and used StructData.LengthB to access the data.
Tiffiny
Glad to see you got this working
vc 74
+1  A: 

How about the following:

  1. Create a class with a property for each control
  2. Each usercontrol gets a reference to the class instance which would be initialised in the main form.
  3. When a value changes in a user control raise an event which is caught my the main form
  4. The other user control would be subscribed to that event and refresh its textbox
Raj
Hey Raj Thanks for your answer! =)
Tiffiny