473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

proper form Show()?

I've seen one way of opening up another form in a C#/WinForms app and I had
done it another way and I would like to know what the gurus think is the
correct/better way?

I've seen one way, where the parent class/form instantiates the child form,
and still in the parent form, sets properties on the child form by calling
methods/properties on the child before before running a childForm.Show().

The way I had done it was to have a constructor in the child form that took
any/all properties/variables that I wanted to be set, then all I had to do
was instantiate the child form from the parent form and run a
childForm.Show().

What is the prefered way people do this kind of stuff in the real world?
Thanks.
Nov 17 '05 #1
5 4396
I personally have always left the constructor alone and just used it to pass
in a reference to the parent form, and used properties across (set/get). I
find that it remains consistent throughout implementing many forms in your
application, more readable, and easier to follow. But in the world of OOP
as you know they say that variables that need to be set during the
initialization of the object should be done in the construction phase, but
only if they need to be initialized at that point. In the case of a form,
actually, your controls need to be set before they are "shown" but not
necessarily when the object is initializing. My 2 cents.

Alex
"Flip" <[remove_me]ph******@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I've seen one way of opening up another form in a C#/WinForms app and I
had done it another way and I would like to know what the gurus think is
the correct/better way?

I've seen one way, where the parent class/form instantiates the child
form, and still in the parent form, sets properties on the child form by
calling methods/properties on the child before before running a
childForm.Show().

The way I had done it was to have a constructor in the child form that
took any/all properties/variables that I wanted to be set, then all I had
to do was instantiate the child form from the parent form and run a
childForm.Show().

What is the prefered way people do this kind of stuff in the real world?
Thanks.

Nov 17 '05 #2
Flip,

For components, it's generally a bad idea to overload the constructor.
The reason for this is that the designer depends on a default constructor in
order to work. It also depends on public properties to allow you to set
values in the IDE as opposed to having to code them.

If it weren't for this case, I would say that it doesn't matter, but
having IDE integration is important, so I would stay that you stick with
this guideline.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Flip" <[remove_me]ph******@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I've seen one way of opening up another form in a C#/WinForms app and I
had done it another way and I would like to know what the gurus think is
the correct/better way?

I've seen one way, where the parent class/form instantiates the child
form, and still in the parent form, sets properties on the child form by
calling methods/properties on the child before before running a
childForm.Show().

The way I had done it was to have a constructor in the child form that
took any/all properties/variables that I wanted to be set, then all I had
to do was instantiate the child form from the parent form and run a
childForm.Show().

What is the prefered way people do this kind of stuff in the real world?
Thanks.

Nov 17 '05 #3
Thank you Alex and Nicholas for your response. So it looks like the best
way to do it is via methods after the empty constructor is called.

Thanks again! :>
Nov 17 '05 #4
You can do it via methods but also in C# you can use properties, its very
handy, like so:

public TextBox tbFirstName;

public string FirstName
{
set
{
tbFirstName.Text = value;
}
get
{
return tbFirstName.Text;
}
}

In your main code:

Form1 form1 = new Form1(this);
form1.FirstName = "BlahBlahBlah";
form1.Show();

"Flip" <[remove_me]ph******@hotmail.com> wrote in message
news:OY***************@TK2MSFTNGP15.phx.gbl...
Thank you Alex and Nicholas for your response. So it looks like the best
way to do it is via methods after the empty constructor is called.

Thanks again! :>

Nov 17 '05 #5
> You can do it via methods but also in C# you can use properties, its very
handy, like so:

Thank you Alex. Yup, I was hoping that if I could access the form via
methods, I would get the properties for free, so thanks for confirming that
(I hadn't actually tried it yet). So it looks like that's the way to do it
then.
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug...
1
by: RC | last post by:
I have an Access 2002 database with many tables and forms (but just to keep things simple, let's say the DB has one Table "Table1" and one Form "Form1"). I have managed to cobble together so much...
2
by: David Sworder | last post by:
Hi, A Windows form that I'm developing will have two controls: Label1 and Label2. Both controls are of type Label (go figure). The text of Label1 will not be known until runtime and may change...
1
by: Christopher | last post by:
Looking for a proper way to close or hide a form in this example. I have a form X that calls a new form Y to show on a given event. Both X and Y are different classes. Ive tried instancing X...
7
by: spebola | last post by:
I am using VB.Net 2003 professional edition, and .Net Framework 1.1, and Windows forms. What is the proper way to close a windows form displayed by the Show method? I would like it to be removed...
3
by: ILCSP | last post by:
Hello, I'm fairly new to the concept of running action pass through queries (insert, update, etc.) from Access 2000. I have a SQL Server 2000 database and I'm using a Access 2K database as my...
2
by: Swinky | last post by:
Can someone explain to me the proper syntax for requery and where to input it in my form? I have a main form "Account-Master" with two subforms: 1.) Contacts and 2.)TNotes. The TNotes subform...
2
by: GS | last post by:
How do I properly detect row change before allowing moving to next row? I tried using RowLeave . here is brief descript of the form setup. I have ieStringTmpDataSetwith regex table...
22
by: PW | last post by:
Hi All, I go into debug mode with the code below and varReturnVal is Null, but the code still is run. Any idea why? Dim varReturnVal As Variant Dim intDogID As Integer
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.