Results 1 to 3 of 3

Thread: Use a control from a class function

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2004
    Location
    Belgium
    Posts
    77

    Use a control from a class function

    Can someone give me a simple but working exemple of the method to use a control from a external class function ?

    I mean by exemple, I have a textbox on my form and a button, bwhen I click the button it call a function from another class and that function change de textbox.text.

    Thanks in advance.

  2. #2
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Use a control from a class function

    You can a property of type TextBox that returns the instance of your TextBox or make this public and pass a reference of the instance of your Form or TextBox (anyway you want). Something like this.
    VB Code:
    1. private void Form1_Load(object sender, System.EventArgs e)
    2.         {
    3.             new Class1(this).ChangeTextBoxTextTo("Some text");
    4.         }
    5.  
    6.         public TextBox MyTextBox
    7.         {
    8.             get{ return textBox1;}
    9.             set{ textBox1=value;}
    10.         }
    and Class1 is something like this
    VB Code:
    1. using System;
    2. using System.Windows.Forms;
    3. namespace test
    4. {
    5.     /// <summary>
    6.     /// Summary description for Class1.
    7.     /// </summary>
    8.     public class Class1
    9.     {
    10.         public Class1()
    11.         {
    12.             //
    13.             // TODO: Add constructor logic here
    14.             //
    15.         }
    16.         Form1 caller;
    17.         public Class1(Form1 caller)
    18.         {
    19.             this.caller=caller;
    20.         }
    21.         public void ChangeTextBoxTextTo(string text)
    22.         {
    23.             caller.MyTextBox.Text=text;
    24.         }
    25.     }
    26. }
    Or there is an alternative making your TextBox a static which is not a member of the instance of your Form1 but as said (not a member of the instance of the Form), you can see it on the designer mode of your IDE. If you're interested, it's like this
    VB Code:
    1. public static TextBox txt=new TextBox();
    2.         private void Form1_Load(object sender, System.EventArgs e)
    3.         {
    4.             this.Controls.Add(txt);
    5.             new Class1().ChangeTextBoxTextTo("Some text");
    6.         }
    and the Class1 is something like
    VB Code:
    1. using System;
    2. using System.Windows.Forms;
    3. namespace test
    4. {
    5.     /// <summary>
    6.     /// Summary description for Class1.
    7.     /// </summary>
    8.     public class Class1
    9.     {
    10.         public Class1()
    11.         {
    12.             //
    13.             // TODO: Add constructor logic here
    14.             //
    15.         }
    16.         public void ChangeTextBoxTextTo(string text)
    17.         {
    18.             Form1.txt.Text=text;
    19.         }
    20.     }
    21. }
    Is this what you want, or I am missing something?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2004
    Location
    Belgium
    Posts
    77

    Re: Use a control from a class function

    Thanks you, it's working

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width