How to work with multiple form in c# - c#

i have 2 forms.
one is the main one and the other is the secondary.
the first one contain a datagrid with displays the columns of a sql table.
the second has textbox of the sql table of the first form and a save button.
when a run the first one from the design mode to the execution mode it's display the form with the datagrid in executive mode.
but when a run the second form from the design mode to the executive mode so that i can enter data, it gives me the first form(datagridform).
which c# code can i use so that when i want to enter data from the second it can do it? because when i execute the second form from the design mode to executive mode i end up on the firstform so it is difficult for me to enter data from the second form.
if there is a misunderstanding of the question plz let me know.
i need your ideas guys.
thnx.

You're getting the first form because that's the form in your application start. Look for a line like this:
Application.Run(new Form1());
in a file called Program.cs.
You need to launch the second form from the first one. So, you could add a button to the first form and put this code in its Click event:
var f = new Form2();
f.Show();
where Form2 is the name of the second form.
Also consider this, you may want to use ShowDialog when showing the second form. That's up to you. Try it both ways and see.

Give your Form1 a Button with the Event you see below to open the Form2
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show(); // form2.ShowDialog();
}

Create object of second Form.
Form1 frm = new Form1();
Call ShowDialog() or Show() as you need. If you need to pass any data then use common property as member.

Related

Docking a WinForm inside another WinForm

I'm stuck at a problem and the old answers regarding the same problem are not quite recent enough so i thought it'd be okay to ask again.
My question is: How can i dock one form inside another form? Would it be more appropriate to use a Panel and a Form instead? Is the first option even possible?
Thanks in advance.
Create 2 forms, Form1 and Form2. Set TopLevel property of Form2 to false. In form load for Form1 add code
private void Form1_Load(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
this.Controls.Add(frm2);
}
This will include form2 in form1, you then have to set properties on form2 if you want to remove the title bar to make the form more like a Panel.
If you want to have some reusable panel that may be used on forms I think User Control will be what you are looking for.

f2.show() method does not show the progress bar in form2

I have an application that has 2 forms. First one is where I do all the job and second one is just for displaying a progressbar.
I want to open the second one from the main form. if I use
Form2 newForm = new Form2();
newForm.Show();
Form2 opens and closes when it needs to open and close, but I cannot see the progress bar. I just can see a blank instead of it.
When I use
Form2 newForm = new Form2();
newForm.ShowDialog();
I can see the progressbar but Form2 doesn't close when it needs. It runs forever, what should I do?
I use a static public variable closeForm to close the second form. When I need to close the form I set
closeForm = true;
and in the second form, I have a timer
private void timer1_Tick(object sender, EventArgs e)
{
if (Form1.closeForm)
{
this.Dispose();
this.Close();
return;
}
else
{
progVal++;
progressBar1.Value = (progVal % 100);
}
}
this is where I put the ProgressBar value and close the form.
When I use show method, I only see blanks instead of the controls in form2. not just the progressbar, and I want form1 to close form2
first of all you need to report progress to progressbar
int iProgressPercentage = (int)(dProgressPercentage * 100);
// update the progress bar
progressBar1.ReportProgress(iProgressPercentage);
try doing that first then call this.close();
As I said above in the comment, you need to check Modal dialog from here Form.ShowDialog Method, and I just quote the following form there:
You can use this method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed.
As why you can't see your ProgressBar on Form2 with Show(); you need to provide more information of how you handles it, as if I separate your program into two parts and use two button click to run them (Click button1 to show Form2; and click button2 to close it) I can see your expected result: the progressbar.
Without your further information, my best guess is something running prevents the Form2 to update its GUI.

How can I have multiple forms start at once in C# .NET?

In C#, I'm trying to get two forms (but probably three eventually) to start at the same time... I've tried adding a new "Application.Run" to my Program.cs file, but it only starts the second form after the first one closes.
So how could I create something like that? Similar to a program like Lazarus.
You simply have to show your form before invoking Application.Run().
var form1 = new Form1();
var form2 = new Form2();
form1.Show();
form2.Show();
Application.Run();
Word of warning here, since no form is tied to the Application.Run call, you will need a way to tell the application to exit when all your forms are closed.
To display a Form you have 2 methods:
Show() - display a non modal dialog (is what you want); also you need to add Application.Run for to work.
ShowDialog() - display a modal(some blocking) dialog; a modal dialog capture all the input for the current thread.
If you want a interface like Lazarus, google by the "MDI application".
I think you probably should decide on one form to be your "primary" form, and then have the other form(s) be member variables of your main form. Then just have the Load event of your primary form also show the secondary form(s).
so,
class MainForm : Form {
readonly Form _otherForm = new OtherForm();
override OnLoad(EventArgs args) {
_otherForm.Closed += // add a handler for what happens when otherForm is closed.
_otherForm.Show();
base.OnLoad(args);
}
}
There may be a better way of doing this but that's what I would do as a first shot.

To close C# Forms Application

I have 2 forms ...when i start the application..and use the close "X" from the title bar the entire application closes...now when i select an option from the 1st form in my case it is a button "ADD" as its a phonebook application..it goes to the 2nd form as i have used 1stform.hide() and 2ndform.show()...now when i do "X" from the title bar it doesnt shutdown completely as the 1stform is not closed....how to program it in such a way tht any stage the entire application should close
Your first form is set as the startup form. That means whenever it gets closed, your entire application is closed. And conversely, your application does not close until it gets closed. So when you hide the startup form and show the second form, the user closing the second form does not trigger your application closing because they have only closed a secondary, non-modal dialog.
I recommend changing your design so that the startup form is also the main form of your application. No sense trying to work around built-in functionality that can actually be useful. You want the application to quit when the main form is closed, no matter what other child forms are opened.
But the quick-and-dirty solution in your case is to make a call to Application.Exit. That will close all of the currently open forms and quit your application immediately. As I said just above, I don't so much recommend this approach because having to call Application.Exit from every form's FormClosed event handler is a sign that something has gone seriously wrong in your design.
If the single startup form paradigm doesn't work out for you, you should look into taking matters into your own hands and customizing the Main method in your Program.cs source file. See the answers given to this related question for some ideas on how that might work for you.
What you can do is to use the Form's FormClosing event, and add the following code:
Application.Exit();
This will stop the entire application, and close all windows. However, if a background thread is running, the process itself will survive. In this case you can use:
Environment.Exit();
Add a Application.Exit on every forms's Closing event
like this:
Create an closing event handler first
private void Form_ClosingEventhandler()(object sender, CancelEventArgs e)
{
//Perform any processing if required like saving user settings or cleaning resources
Application.Exit();
}
then bind this event to any form you create.
//Where you create new form and show it.
Form1 frm= new Form1();
//set other properties
frm.Closing += new EventHandler(Form_ClosingEventhandler);
Form2 frm2= new Form2();
//set other properties
frm2.Closing += new EventHandler(Form_ClosingEventhandler);
Surely you don't want to shut down the entire application after the user adds a phone number? You just need to make sure that your main window becomes visible again. Write that like this:
private void AddButton_Click(object sender, EventArgs e) {
var frm = new AddPhoneNumber();
frm.StartPosition = FormStartPosition.Manual;
frm.Location = this.Location;
frm.Size = this.Size; // optional
frm.FormClosing += delegate { this.Show(); };
frm.Show();
this.Hide();
}

How to call a form when there are 3 forms in a project and data Transfer between the forms in C#?

I have 3 forms in my project form1 , form2 , form3 and it was running smoothly now i have added one more in my project form4 . The first three forms are already linked up through ShowDialog().
I don't want to touch Program.cs file.
How can i call form 4 first as start up form ? Earlier form 1 was the first form to appear in my project.
Also i have 2 radio buttons in my form1 rdb1 and rdb2.
In my form2 i have openFileDialog attached to a button Select . Now i want when the user selects rdb1 in form1 then the filter of openFileDialog in form2 should open files with only ".XML" as extension and when rdb2 is selected in Form1 then in Form2 only ".TXT" files can be opened.
I am unable to find the syntax for this in intellisense can you please help out?
Thanks in advance..
Can you explain why you don't want to touch your Program.cs file? This is exactly where you change the start-up form.
Change the:
Application.Run(new Form1());
to:
Application.Run(new Form4());
Secondly, you can set the filters on Open- and SaveFileDialog using the Filter property. Set it to a value like this:
XML Files|*.xml
Or for text:
Text Files|*.txt
Edited to add:
To do this from another form:
class Form1 {
Form2 form2;
void Form1_Load(object sender, EventArgs e) {
form2 = new Form2();
}
void rdb1_CheckedChanged(object sender, EventArgs e) {
if (rdb1.Checked)
form2.openFileDialog1.Filter = "XML Files|*.xml";
else
form2.openFileDialog1.Filter = "Text Files|*.txt";
}
}
Make sure you have set the Modifiers property of the openFileDialog1 on the Form2 designer to "Public" or "Internal" to allow access to it from outside the class itself.
You have to touch program.cs. That's where the initial form is created and run.
Create a new winforms project and open program.cs. You can see where Application.Run is called with an instance of the startup form. That's what you have to do.
Don't be scared. Everything will be alright.
In response to the comment:
The Form1_Load event is fired within Form1 by Form1. You must go back up the call chain and find where Form1 is instantiated and passed to Application.Run. And that's in program.cs.
Do it. And then go get a copy of CLR Via C#. You won't regret it (as long as you skip the first couple chapters!)
How can i call form 4 first as start up form ?
I don't want to touch Program.cs file.
Why don't you want to touch Program.cs? The form instance passed to Application.Run(..) determines the start up form.

Categories