Properties Window in C# - 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.

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.

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

C# - How to close current window and get new window

I'm implementing a window form in C#. I want to close current window and open a new window. (Just like File->New function in applications)
This is my code
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
Image_Editor IE = new Image_Editor(); //Image_Editor is the name of window
IE.Show();
this.Close();
}
Images of what I want to achieve:
(source: infosight.com)
I want to implement the "new" function as shown in the above link. When executing given code, new window just appeared and both windows close at the same time.
What should I do to solve this?
if you mean that you want to close this windows and open a windows like your current windows use this
Myform frm; //thats name of the class of your form like Form1
frm = new Myform();
frm.Show(); // show the secend one
this.Close(); // and close the first one

Control.Focus() bug

I have some problems with Form control focusing.
On form1 I click a button and run the code below:
private void btnTest_Click(object sender, System.EventArgs e)
{
form2 = new Form2();
Application.Idle += new EventHandler(Application_Idle);
form2.Show();
form2.Activate();
form2.textBox1.Focus();
Form3 form3 = new Form3();
form3.ShowDialog();
}
Then, after this CLR I run the event Application_Idle on which I add a method that must focus on the textBox2 control..
private void Application_Idle(object sender, EventArgs e)
{
form2.textBox2.Focus();
form2.textBox2.Select();
form2.textBox2.Focus();
Application.Idle -= new EventHandler(Application_Idle);
}
But when I click the button on form1, I see Form2 showing, Form3 showing and then Application_Idle method raise, but form2.textBox2 control doesn't get focused...
If I comment out the form3.ShowDialog(); line it's works fine, but how do I focus a form element with another form activation?(form3.ShowDialog()) ?
Remark added:
Problem in also is I have a strict architecture and all I can change is Application_Idle method.
The issue you are having is with modality:
Forms and dialog boxes are either modal or modeless. A modal form or dialog box must be closed or hidden before you can continue working with the rest of the application.
Dialog boxes that display important messages should always be modal. The About dialog box in Visual Studio is an example of a modal dialog box. MessageBox is a modal form you can use.
Modeless forms let you shift the focus between the form and another form without having to close the initial form. The user can continue to work elsewhere in any application while the form is displayed.
When you use ShowDialog, the form that is shown prevents the caller from returning control until the dialog box is closed. If this is not the desired effect, you can use the Show method.
You could focus the textfeld, when the form itself got the focus:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.GotFocus += (s, e) =>
{
this.textBox2.Focus();
};
}
}
As John Koerner stated, you cannot set focus to Form 2 while Form 3 is open because of modality.
Since you stated that a user input in Form 3 is necessary to proceed, you should change your approach. You can place a listener watch for Form 3's closing. Only then can you set the focus somewhere else
form3.FormClosed += Application_Idle

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