Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    [C#] how to create a function within a class

    Hi guys,

    I need to store a function in a class, which can be accessed all the time. For instance: a user types his name in a messagebox, clicks the OK-button, and a new form will show. There is a new button on the second form, which needs to trigger the class function where a popup-box is generated which will show a welcome-message with the user's name. How do I need to program this? And how do I close the first form after showing the second?

    Thanks in advance!

    Edit: getting a bit further. Got the function inside the class working, now I need to create a variable which saves the info from the messagebox in the form. This is my code, and this is as far as I can get:

    Form1.cs
    Code:
            public void button1_Click(object sender, EventArgs e)
            {
                Class1.test(); 
            }
    Class1.cs
    Code:
            public static void test()
            {
                string test123 = txtTest.Text;
                Form testform1 = new Form1();
                Form testform2 = new Form2();
                testform2.Show();
                testform.close(); // How to close the first form??
            }
    Form2.cs
    Code:
            private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show(test123);
            }
    Anyone knows how to solve this?
    Last edited by Scientist_nl; 10-26-2007 at 01:34 AM.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    In the code for Form2, you can have a property. Set it after you create an instance of your form

    From2.cs:
    add:
    Code:
    // declare field
    public string msgtext
    
    //declare property
    public string msgtext
    {
      get
      {
        return msgtext;
      }
      set
      {
        msgtext = value;
      }
    }
    change:
    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show(msgtext);
            }
    class1.cs:
    change
    Code:
            public static void test()
            {
                string test123 = txtTest.Text;
                Form testform1 = new Form1();
                Form testform2 = new Form2();
                testform2.msgtext = txtTest.Text;
                testform2.Show();
                testform.close(); // How to close the first form??
            }
    I am the luckiest man in the world

  • #3
    New to the CF scene
    Join Date
    Sep 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    didn't do the trick For some reason he can't find the object txtTest, which is created in Form1.cs, in Class1.cs


  •  

    Posting Permissions

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