Control Object in second form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • liadmz
    New Member
    • Jul 2009
    • 32

    #1

    Control Object in second form

    Hi,

    I am currently learning c#.net. the following command to retrieve data from another component on another form:
    to change it's value and stuff..

    In vb.net I would use
    lblMax.Text = frmMain.txtMax. text - to get the value
    frmMain.txtMax. text = 1234 - to change it's value.

    I am trying to find out how I do this in C#.
    when I writing it using C# (with c# syntax) I'm getting the following errors:
    "An object reference is required for the non-static field, method, or property"
    I have changed to object (textbox) to public and the modifier to public as well.

    Thanks....
  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    #2
    RE: Control Object in second form

    Actually the way of accessing values that you are using is not appreciated...
    you're declaring a static member of your the form.......your form becomes dependent on each other and needs to be changed if included in different projects...

    it cuts out the flexibility of your application to be used in several projects..
    And doesn't really follow the Resuability concept of object oriented programming...

    But i've got a better a better way to do it....

    Using your examples....her e's what i got for you....

    There are 2 forms Form1 and Form2...
    lblMax is in Form1
    txtMax is in Form2
    Form1 acts as the Main UI or main Form
    Form2 acts as a dialog where settings need to be done so that it could reflect in Form1....

    So what you would do here is put up a lauch button you can call it as settings
    to open Form2...and when data is enetered in Form2 and Form2 is closed the data in lblMax in Form1 will get updated to whatever is entered in txtMax in Form2....

    code of Form1
    this code should be written at button click event
    Code:
    private void Button1_Click(object sender,EventArgs E)
    {
    Form2 frm2 =new Form2()'
    frm2.ShowDialog(); // show Form2
    lblMax.Text=frm2.MaxValue; //getting the value of txtMax via a Form2 property after the Form2 gets closed...
    }
    code of Form2
    this should be wriiten at OK button click event
    Code:
    private void btnOK_Click(object sender,EventArgs E)
    {
    _maxVal=txtMax.Text;
    }
    this code should be declared as a class property in Form2
    Code:
    private string _maxVal="";
    public string MaxValue
    {
    get{return _maxVal;}
    }
    Now that's the most correct solution.....yo u'd ever find....and show it to those personnel who recommended you to code like that....

    Why did i explained you these because...acces sing values to memebers like that is not a good practice....and if you go on programming like that ...you'll land up in trouble wh en youre into an industry....
    And if you want some help about good programming issues you can download my ebook which is written by me of course...which is exclusively for students...
    here is the link...
    http://thatthatsite.yolasite.com/
    Reply me if this book was useful....

    Comment

    • liadmz
      New Member
      • Jul 2009
      • 32

      #3
      I really appreciate you help...
      I have downloaded your book and I replay to you soon..

      Thanks again.

      Comment

      • JnrJnr
        New Member
        • Oct 2009
        • 88

        #4
        Is MaxValue a declared variable?

        Hi there ThatThatGuy Ive also got more or less the same problem.
        Im righting in Visual C# and want to carry out commands for controls(button s,labels) from form2 onto form1.
        For instance: I want to to use form1 as my main form...form1 must have a label and form2 must have a button.
        When the button on form2 is clicked I want the label on form1 to be disabled.
        I just want o know how to interact with controls on different forms.



        Your help would be much appreciated.

        Comment

        Working...