C# windows form application - c#

i am trying to go from one form application to the next by clicking a button, how do i do it?

I assume by "go from one form application to the next" you mean you want to invoke another executable (another windows forms app, for example). If that assumption is correct, then you can wire up the OnClick method of the button to do something like this:
System.Diagnostics.Process.Start(#"C:\your_other_app.exe");
You can refer to this msdn link for more info about Process.Start and its different variations.

Try this:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show() ;
}
and make sure that button1's click event is handled:
this.button1.Click += new System.EventHandler(this.button1_Click);
or
button1.Click += this.button1_Click;
either in the designer code, or in your Form.load handler, or your form constructor.

You have to make in buttonClick SecondForm's object and then object.ShowDialog() or Show, if you want your second form to be active and not to have right to move to first Form, then use ShowDialog method, other way use Show(). Like this :
private void Button_Click(object sender, EvantArgs e)
{
SecondForm form = new SecondForm();
form.ShowDialog(); //form.Show();
}

Related

WinForms; detecting form closed from another form

Is it possible to detect a form closing from another form.
For example.
If I had a mainForm that opens subForm, can I detect within the mainForm that the subForm has closed and execute code?
I understand I could create an event handler within the subForm, but this is not really what I'm after because what I'm about to do after the subForm closes, is within the mainForm (changes to mainForm).
The FormClosed event is public, so you can create a handler from the main form.
//Inside main Form. Click button to open new form
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.FormClosed += F2_FormClosed;
f2.Show();
}
private void F2_FormClosed(object sender, FormClosedEventArgs e)
{
MessageBox.Show("Form was closed");
}
Take a look at the public FormClosedEvent. Since the modifier is public, you're able to do something like the following example:
SubForm subForm = new SubForm();
subForm.FormClosed += delegate
{
MessageBox.Show("subForm has closed");
};
subForm.ShowDialog();
The above example creates a new form (of type SubForm), adds a new event handler to display a message box telling the user that the form has closed, and finally uses the ShowDialog() method which will prevent the user accessing the main form until the sub form has been closed.
The usual case for this is a "Modal Dialog" (like Message Box and its Family).
Every form can be opened as Modal Dialog, by using ShowDialog() isntead of Show().
Otherwise the event way is the only way.

LinkLabel to open a new Form

I am making a Windows Forms application. I have a LinkLabel in Form1.
How can I open another form (Form2) when someone clicks on my LinkLabel?
A LinkLabel is for opening an url. You probably want to create a "normal" Label and than handle to click event (double click the Label in the WinForms designer and it will generate one for you).
By the way, if you really need to use StackOverflow for this I suggest you watch some beginners video's first. Better try to understand thing first.
https://msdn.microsoft.com/en-us/library/dd492132.aspx
In Visual studio select every control you want to use it's event, here select LinkLabel and from properties window click on Events tab , you will see list of events of selected control. Here you want to use click event .so you can double click on click event .visual studio will create below method for you
public void YourControlName_click ( object sender , EventArgs e )
{
// Add code that you want execute when you click control
}
For display the form on screen you must use Show or ShowDialog method of Form class
Form1 f = new Form1();
f.ShowDialog();
So you must add above code to your method
public void YourControlName_click ( object sender , EventArgs e )
{
Form1 f = new Form1();
f.ShowDialog();
}
Use Linklable_LinkClicked event to open another form. Don't use Clicked event. Here is my example below:
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
frmSecondForm secondForm = new frmSecondForm();
secondForm.Show();
this.Hide();
}

Open a new winform by doubleclick and close the caller window

I have a C# winform called Form1, and this winform has a list and a button.
I added a click() event to the button, and a doubleclick() event to the list.
Both events call to the same method: (in form1.designer.cs)
this.myList.DoubleClick += new System.EventHandler(this.myMethod);
this.myButton.Click += new System.EventHandler(this.myMethod);
In myMethod, I want to do the following operations:
open a new winform of kind Form2, and make it the active winform.
close the caller winform (of kind Form1), there is no need for this form anymore.
I did it like this: (in form1.cs)
private void myMethod(object sender, EventArgs e)
{
Form2 frm = new Form2();
this.dispose();
}
when myMethod is being called by list doubleclick event, when myMethod ends, there is a null pointer exception.
When it's being called by the button click event, it works properly.
I tried this.close() as well, and got the same behavior.
my questions:
How should I write myMethod properly so it will make the wanted operations for the button click event and also for the list doubleclick event?
What is the difference between the button and the list? why does it work properly for the button, but crashes for the list?
Thanks
You can hide the Form1 and show Form2. This will raise some issue like closing Form2
won't close Form1.
this.Hide();
var form2 = new Form2();
form2.ShowDialog();

Properties Window in C#

I'm currently trying to create a Properties Window which is opened after a Button on the Outlook Toolbar is pressed, i now have:
1) the Button on the Toolbar (currently if pressed nothing occurs)
2) i know how to create the method which would hold the action after the Button is Pressed
-but, I am a beginner and i don't know how to create a window which would open after the button is pressed, the Window should be fairly big, and for now have nothing but a checkbox(which i later would like to apply some method to.
if you ever created a window which opens after a button is pressed, i would be really pleased to get your help.
All help is appreciated, thank you
Here's the recommended way of opening a dialog window when the user clicks a button:
Add a new form to your project (e.g. MyForm) and then you can use the following code in your button's click event handler:
private void OnMyButtonClicked(object sender, EventArgs e)
{
MyForm myForm = new MyForm();
if (myForm.ShowDialog() == DialogResult.OK)
{
// The code that should be executed when the dialog was closed
// with an OK dialog result
}
}
In case you do not want the new window to be modal (i.e. you want to allow the user use other parts of the application while the window is opened), the code gets even more simple:
private void OnMyButtonClicked(object sender, EventArgs e)
{
MyForm myForm = new MyForm();
myForm.Show();
}
You can also create your form on the fly without adding one to your project, which is a bit more complicated, but advanced developers prefer this approach instead of messing with the designer ;)
private void OnMyButtonClicked(object sender, EventArgs e)
{
Form myForm = new Form();
myForm.Text = "My Form Title";
// Add a checkbox
CheckBox checkBox = new CheckBox();
checkBox.Text = "Check me";
checkBox.Location = new Point(10, 10);
myForm.Controls.Add(checkBox);
// Show the form
myForm.Show();
}
Here is a small tutorial for you to follow..
http://msdn.microsoft.com/en-us/library/ws1btzy8%28v=vs.90%29.aspx
EDIT: I would also recommend you remember the msdn website because it will prove invaluable for other programming issues you come across..
you have to add a new form to your project. Then you call the constructor where you want to pop up the window.
like this
Form2 form2 = new Form2();
form2.showDialog();
Edit:
where form2 is not the "main" Form of you program.
This'll set your main window to the background as long as the newly popped up window is closed.

Should I use form1_Load event to have text box information displayed when form is opened?

I have a winform with a button that the user clicks when they want to generate a certain report. When this button is clicked, another winform opens up. The second winform has a text box and a button to take you back to the first winform.
When the second from opens up, I want the text box to already have the report displayed. Therefore, all the user has to do is is look at it and go back to the previous form when finished.
To do this, would I assign the text box to the appropriate method and put it in a Form1_Load event method?
I've never used the Form1_Load event method so I'm a little unsure if this is the proper way of doing it.
Yes, of course, in the Form_Load event you have access to all of your controls already Initialized by the form constructor via InitializeComponent().
Then you can call
private void Form_Load(object s, EventArgs e)
{
textBox1.Text = "your_report_title";
}
Yes, this would be the correct use of Form1_Load().
private void Form1_Load(object sender, EventArgs e)
{
textbox1.Text = "Whatever is supposed to go here"
}
Alternatively you can use the constructor of the form which should already be there.
public Form2(string text)
{
InitializeComponent();
textBox1.Text = text;
}
Then just open the form using
Form2 form2 = new Form2("text that should be displayed");
form2.Show();

Categories