WinForm (Multiple forms, menus, and toolbars)

Source: Internet
Author: User

One, multiple forms

1. Which is the main form

Use the From1 button to open the From2

Private void button1_click (object  sender, EventArgs e)        {            new  Form2 ();            F2. Show ();             this. Hide (); // when opening From2, hide From1        }

The problem with light: The main form is hidden, the main form is not displayed/closed after closing other forms, then the program is closed

(1), the value of the constructor, the form is passed to another form

First open the FROM2 code and select event FormClosing

NULL ;          Public Form2 (Form1 F1)        {            InitializeComponent ();             = F1;        }         Private void Form2_formclosing (object  sender, FormClosingEventArgs e)        {            F1. Close ();        }

The Form2 f2 in From1 = new Form2 () is then changed to Form2 F2 = new Form2 (this);

2. The form can only open one

Create a global generic collection in From1 in order to place all open forms

list<form> flist = new list<form> ();
(1), before the form opens, determine whether there is a name consistent form in the collection, if there is a description is open, do not open the

Change the Button1 Click event in From1 to

BOOLfa==false;//whether the FROM2 has been opened onceForm2 F2 =NewForm2 ( This); foreach(Form Finchflist) {            if(f.name==F2. Name) { has=true; }            }            if(Has) {}Else{flist.                ADD (F2); F2.            Show (); }

Issue: When the form is open and closed, it cannot be opened again
Workaround: Clears this form object record from the collection in Form1 when the form is closed
First, add a function to the from 1

 Public void Deletefrom (Form F)        {            flist. Remove (F);        

Then, in the From2 formclosing event, replace the

if (f1!=null)            {                F1.deletefrom (this);            }

Issue: When the form is already open, clicking the Open button again will not re-display the open form and focus into
Way:
Locate the Open object and set the WindowState property to:
Locate the object that has the form open and use the focus method;

Add "F2" to the Button1 click event in From1 in the IF (has) {}. Close (); ", it plays the role of releasing memory

if (f.name==f2. Name) {has = true;} true; after adding f.windowstate = formwindowstate.normal;//minimized or maximized click button to restore normal size
F.focus ();//Focus entry

3. Transfer values and controls between forms

Pass value: Constructor pass value

Change the From2 constructor to

Public Form2 (Form1 f1,string uname)
{
InitializeComponent ();
F1 = F1;

Label1. Text = "Welcome back," + uname;
}

Change to Form2 F2 = new Form2 (this,textbox1.text) in From1;

Control: The first step, find the Form object, second step, change the control value of the form object
Note: To modify the access rights of objects in a form to public:

Add a New button and text box in From2, click the Click event of this button, add if (f1!=null) {F1.button1.text=textbox1.text}
4. The form of the prompt box type

ShowDialog ();

Modify the Place:

New Form2 (this, TextBox1.Text);            F2. ShowDialog ();

Role:

Displays the form as a modal dialog box. After you call the Form.ShowDialog method, the code following this method is not executed until the dialog box is closed

Second, menu and tool bar

1, MenuStrip:
Top menu, right click to insert Standard item
Split Line: 1,-2, right-click Insert Separator

Shortcut keys: Each key right-click property at the bottom Shortcukeys can set the shortcut key, Showshortcukeys whether to display the shortcut key on the item
Shortcut keys work regardless of whether the option is hidden or the menu is hidden

2, ContextMenuStrip:
Right-click menu

On the property behavior Contextmeunstrip Select you want to associate the right-click menu, you can use the right-click menu, each control may be associated with the right-click menu

3, Statusstript:
Bottom Menu

4, ToolStrip
Tool bar

5, ToolStripContainer
Tool Bar Container

WinForm (Multiple forms, menus, and toolbars)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.