Elite Coders Society
Hello Guest!
Join us here in this forum!
Please Log in or Register to access more feature in this site.
Start Learning!

† Copyright ©️ 2011 - EliteCoders.forumotion.com †

Start sharing and learning new skills about programming and design by building connections with other professional and students.
HomeForumPortalCalendarGallerySearchRegisterLog in
Elite Coders Partner Links

EliteCoders™️ News!
Hello Guest!, Welcome in our forum. Be active!

Share | 

 C# Tutorial - How To Open New Forms

View previous topic View next topic Go down 
New Member
New Member

Posts : 1
EC-Points : 3
Thanked : 0
Join date : 14/02/2012
Location : Phil.!

PostSubject: C# Tutorial - How To Open New Forms   Sun Feb 19, 2012 9:17 am

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.


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();

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


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.
Back to top Go down
View user profile

C# Tutorial - How To Open New Forms

View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
Elite Coders Society :: Software Development :: C++ :: C++-

Protected by Copyscape Duplicate Content Finder
Powered By EliteCoders™

Elite Coders Partner Links
GreyPhantomYour Link HereYour Link HereYour Link Here