+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: C# Tutorial - How To Open New Forms

  1. #1
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    C# Tutorial - How To Open New Forms

    OK, this question seems to have popped up quite a lot, and it's really quite simple, so here's some details about how to open secondary forms from a main form.

    Example

    We have a main form. The user clicks "New Order", where the order form pops up. But how does it pop up?

    Instance and Static

    This is how C# differs to VB. In VB, you have one copy of each form. You can hide and show this form any time you like, but there is one form. You can toggle visibility by using Show() or Hide().

    C#, on the other hand, is very different. Here we can only use instance members. What this means is that there is no single copy of the form - if you want to open a form, you must create a new "version" of the form, and display this to the user.

    Classes and Objects

    In VB, a form name in the code would refer to the actual form object. By typing a period (.), we could access the properties of the form. In C#, the form name is a class. Therefore, we cannot access any properties directly from it. We must create a new object variable, and use the form class to fashion it to the shape of the form we want to show.

    The Code

    To open the order form, we would do something like this:

    [HIGHLIGHT=csharp]
    frmOrder form = new frmOrder();
    form.ShowDialog();
    [/HIGHLIGHT]

    Notice how the frmOrder form (the name of our order form) is being used as a class - the actual form object is called "form". Therefore, to access its properties we refer to this, and not the form class.

    Suppose we wanted to know the result of the order form:


    [HIGHLIGHT=csharp]
    frmOrder form = new frmOrder();
    if (form.ShowDialog() == DialogResult.OK)
    {
    MessageBox.Show("You selected the " + form.txtOrder.Text + " item!");
    }
    else
    {
    MessageBox.Show("Oh well, maybe next time!");
    }
    [/HIGHLIGHT]

    First, we check the DialogResult enumeration that ShowDialog() returns. If it is DialogResult.OK (under a couple of namespaces, of course) then we grab the contents of the txtOrder textbox on the form. If not, we display a cancellation note (you don't have to do this, of course).

    Conclusion

    I hope this will be helpful - especially for those migrating from VB to C#. Bear this in mind, and creating new forms will be a doddle.

    Xav

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: C# Tutorial - How To Open New Forms

    Nice tutorial
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: C# Tutorial - How To Open New Forms

    Thanks. Can I have my 20 contest points then?

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  5. #4
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: C# Tutorial - How To Open New Forms

    Ask Jordan for them. I'm not the one that hands them out.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: C# Tutorial - How To Open New Forms

    He's offline ATM.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  7. #6
    Join Date
    Sep 2008
    Location
    Kosovo
    Posts
    4,032
    Rep Power
    44

    Re: C# Tutorial - How To Open New Forms

    Nice Tutorial .. Xav +rep when it lets me to ..

  8. #7
    Jordan Guest

    Re: C# Tutorial - How To Open New Forms

    Nice read. I've seen this question crop up before and I've heard people ask it in person. I prefer instantiating the class/form rather than Visual Basic's ... method? I believe me or someone on here also wrote a tutorial on how to do this using Visual C++ forms. A tricky thing in Visual C++ is accessing the parent form from the child form. Not creating a new parent class but actually accessing the current instance of the parent form. Perhaps this could be your next C# tutorial?

    Anyway, I awarded you your 20 points before I closed the contest. Congrats!

  9. #8
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: C# Tutorial - How To Open New Forms

    Accessing the parent form from the child form? Ouch.

    You could always expose a few of the parent form's methods if need be.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  10. #9
    doomsaber is offline Newbie
    Join Date
    Nov 2009
    Posts
    5
    Rep Power
    0

    Re: C# Tutorial - How To Open New Forms

    Luv the tutorial

  11. #10
    Umbrageous's Avatar
    Umbrageous is offline Newbie
    Join Date
    Oct 2008
    Location
    Undernet
    Posts
    17
    Rep Power
    0

    Re: C# Tutorial - How To Open New Forms

    This is great, but how would you use variables from the other form in the new form, I ask this because i was trying to make my own notepad and wanted to add a find button so you could search through text and what not, and i knew how to open a new form but i could not for the life of me figure out how to use variables and objects from the parent form,

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 6 users browsing this thread. (0 members and 6 guests)

Similar Threads

  1. PHP Open Database Connectivity (ODBC) Tutorial
    By kailas in forum PHP Tutorials
    Replies: 0
    Last Post: 03-08-2010, 08:17 AM
  2. Where can i find a free Open GL C++ tutorial?
    By chinog9 in forum C and C++
    Replies: 7
    Last Post: 10-07-2009, 07:26 AM
  3. Replies: 6
    Last Post: 02-03-2009, 10:39 AM

Tags for this Thread

Bookmarks

Posting Permissions

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