LinkLabel to open a new Form - c#

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

Related

I minimize one Form and minimize several

I have a form and through this I press a button and a new form opens. If I click minimize this new form, the form I call this form from is minimized as well. How could I make sure the form from which I call the new form is not minimized?
Maybe I have to enable some property in the form or something.
Of course, I tried with the following code and with the form propety singleFixed but the two forms are minimized:
private void bminimize_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
}
Maybe have I to create this new form as subform or something like this?
EDIT: How I call this new form:
private void Button_Click(object sender, EventArgs e)
{
DateTime rnow = DateTime.Now;
Chronometer chrono = new chronometer();
var resultchrono = chrono.ShowDialog();
if (resultchrono == DialogResult.OK)
{
...
}
You're using ShowDialog(), which is documented thus, emphasis mine:
This version of the ShowDialog method does not specify a form or control as its owner. When this version is called, the currently active window is made the owner of the dialog box. If you want to specify a specific owner, use the other version of this method.
If you need the dialog result, you'll need to use the ShowDialog(owner) form of the call with say, the desktop window handle.
Its because of: chrono.ShowDialog();. Show the dialog with chrono.Show(); . But you will have to handle returned values differently.
If you need the DialogResult in the same method in which you open the window, then use AKX´s answer.

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

C# windows form application

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

opening forms in c#

I had a button in first form .if i click the button the second form is opening ,if i click again same button in first form another second form is opening.i need to open only one second form only in c# .
Well you could do a :
Form.ShowDialog()
This will prevent the user clicking the button on the first form, as the second form will keep focus until it is closed.
Or you could do
Form2 form2 = null;
void button_click(object sender, EventArgs e)
{
if(form2 == null)
{
form2 = new Form2();
form2.Disposed += new EventHandler(f_Disposed);
form2.Show();
}
}
void f_Disposed(object sender, EventArgs e)
{
form2 = null;
}
Check to see if the form is already shown by storing a reference to it and using something like:
if(form2Instance.Visible==true)
....
If you provide some sample code, we'll have a more specific answer, but it sounds like you are instantiating a new form in your button click event. The first time you click the button, your second form (will/may) not exist, so create it and keep a reference to it local to your first form. Then the next time the button is clicked, show the form instead of recreating it.
I'd declare a private variable in the class with the button that contains a reference to the opened form.
If the button is clicked:
Check whether it is null,
If yes, create and show a new form, if no, don't do anything.
If it's about having exactly one form open, you might also check out form.ShowDialog(), which blocks the caller form until the new form is closed.

Categories