Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C# Tutorial - How To Open New Forms


  • Please log in to reply
12 replies to this topic

#1 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 20 November 2008 - 12:39 PM

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:


frmOrder form = new frmOrder();
form.ShowDialog();


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:



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!");
}


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
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 20 November 2008 - 12:55 PM

Nice tutorial :)
  • 0

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

My MineCraft server site: http://banishedwings.enjin.com/


#3 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 20 November 2008 - 12:55 PM

Thanks. Can I have my 20 contest points then?
  • 1
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 20 November 2008 - 01:01 PM

Ask Jordan for them. I'm not the one that hands them out.
  • 0

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

My MineCraft server site: http://banishedwings.enjin.com/


#5 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 20 November 2008 - 01:02 PM

He's offline ATM.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#6 Egz0N

Egz0N

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1155 posts

Posted 20 November 2008 - 01:30 PM

Nice Tutorial .. Xav ;) +rep when it lets me to .. :D
  • 0

#7 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 21 November 2008 - 04:31 AM

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!
  • 0

#8 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 21 November 2008 - 11:01 AM

Accessing the parent form from the child form? Ouch.

You could always expose a few of the parent form's methods if need be.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#9 doomsaber

doomsaber

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 22 November 2009 - 08:54 AM

Luv the tutorial
  • 0

#10 Umbrageous

Umbrageous

    CC Newcomer

  • Just Joined
  • PipPip
  • 17 posts

Posted 29 November 2009 - 01:03 PM

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,
  • 0

#11 QuackWare

QuackWare

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 88 posts

Posted 03 January 2010 - 06:17 PM

Cool tutorial, thanks for the info.
  • 0

#12 gokuajmes

gokuajmes

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 483 posts

Posted 22 January 2010 - 01:16 AM

really good i never used this technique before
let me try this then
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download