Posto
Posto

Reputation: 7550

.net c sharp windows Form Application : opening multiple instance of same form

I want to open Multiple Instance of a single from , and running simultaneously , independent of each other.
is it possible in .net windows Form application , if yes how can I achieve it.

Basically I want to make a chat Application using "**.net windows Form **" which will have ** one to one ** chat window.

Upvotes: 0

Views: 3524

Answers (4)

Niran
Niran

Reputation: 1126

Yes, it is certainly possible.

Form1 f1 = new Form1();
Form1 f2 = new Form1();
f1.Show();
f2.Show();

Forms in .NET world are objects and you can create them (as many you want) like you create instances of any other type. 

Upvotes: 3

BillW
BillW

Reputation: 3435

First, I do assume you are talking about one application hosting multiple forms, not multiple applications running at the same time each hosting the same type of form : if I got that wrong, please ignore what follows :)

Personally I would use the strategy I suggested here : Manage muliple windows forms in c# app

I have implemented solutions using this approach where every window was an instance of a Form Template, all shared the ability to shut down the entire application, and communication between forms can be handled through a single static class that all Forms have a reference to and which serves as a message dispatcher.

For example : if each of your forms has a TextBox : as you create that Form from your Template or whatever, you add a reference to the Form's TextBox to some kind of data structure in the static class :

(perhaps

List<TextBox> 

or

Dictionary<TemplateForm, TextBox>

)

That's just a very broad suggestion, and a lot of what you will need will depend on exactly what you mean by the word "communicate." You can do things like defining a custom event on your TemplateForm that raises an Event which the Static dispatcher class subscribes to : then, when the "Dispatcher" gets that event it can "broadcast" to every other Form, or some Forms, etc. Again these are broad sketches of what you can do.

From my point of view this approach gives you maximum freedom at the small price of requiring you only to be sure and handle the Form Closing event to make sure the last Form closed shuts down the application.

Whether a design where "every Form is equal" suits your needs for your application; I don't know : you can still use this approach while making one and only "master form," if you wish, and making only that Form capable of shutting down the app (so you don't have to worry about each Form's possible closing leaving the application "running on empty."

I did an application for a friend using this approach where there was one "master form" which essentially had four "views" : start-up screen, configuration dialogue, state overview screen showing the current state of the application and which TemplateForm was "connected" to which other TemplateForm(s), and, finally, a "that's all folks" screen :)

best,

Upvotes: 1

Brij
Brij

Reputation: 6122

Make Sure, you are not showing form as dialog means

Form3 objForm = new Form3();
objForm.ShowDialog();

in this condition, it will wait for closing form then execute the next line.

Upvotes: 0

Ramunas
Ramunas

Reputation: 3883

MyForm f1 = new MyForm();
f1.Show();

MyForm f2 = new MyForm();
f2.Show();

// etc.

Upvotes: 2

Related Questions