dotnet WinForm FAQ 16 (next)

Source: Internet
Author: User
Tags exit constructor dotnet
9. How to make a form of MDI

1. Create a new Windows Application project

2. Add two forms Form1, Form2

3. Set the Form1 IsMdiContainer property to True. Make it the MDI main form.

4. Add a RichTextBox control to the Form2 and set the dock to: Fill

5. Drag a MainMenu to the form Form1 in the Tools form, and then create a menu file| windows| Help three menu items, including new, exit menu items in file, and Windows including cascade, horizontal, and so on.

6. Set the MDIList property =true for Windows menu items so that each MDI child window is automatically added below the Windows menus.

7. Double-click the New menu item, and then add the following code:

private void Menunew_click (object sender, System.EventArgs e)

{

Form2 Newmdichild;

Newmdichild = new Form2 ();

Newmdichild.mdiparent = this;

Newmdichild.show ();



}

8. Add the following code to the Windows Cascade menu item:

private void Menuwindowcasca_click (object sender, System.EventArgs e)

{

This. LayoutMDI (Mdilayout.cascade);



}

In addition there are the following commonly used:

This. LayoutMDI (mdilayout.tilehorizontal);

This. LayoutMDI (mdilayout.tilevertical);

9. F5 run.

The final version of Vs.net does not know whether there will be a generic template, but the full manual way to create an MDI window, it is a bit cumbersome, not as simple as the Vs.net IDE way.



10. How to make your form not appear on the task bar.

It does not appear on the taskbar when the form's boundary style is the tools window. In addition, the method described in heading 5 above is not visible to the form, and does not appear on the task bar.

If you are in the dotnet world now, this thing also becomes simple, any WinForm form now has the ShowInTaskbar attribute, so you just have to set this attribute simply. Again, you can choose to change ShowInTaskbar from true to false in the Properties window. Or in the form of code:

Mytaskbarfrm.showintaskbar = false; (C #)



11. How to make a form with a splash screen.

Need you to prepare two WinForm form, one call it: SplashScreen, make it a beautiful form. Then you need a main form to call it: Form1 bar, and then add the following code to the form.

(C #)

protected override void OnLoad (System.EventArgs e)

{

Make load take a long

Thread.Sleep (2000);



Base. OnLoad (e);



}

Then add the code to main:

[STAThread]

static void Main ()

{

SplashScreen splashform = new SplashScreen ();

Splashform.show ();



Form1 mainform = new Form1 ();

Mainform.load + = new EventHandler (splashform.mainscreen_load);

Application.Run (MainForm);



}

Don't forget to add a reference to Threading: using System.Threading;



12. How to make your form trayicon.

To implement this feature you can use the NotifyIcon control to reach, drag NotifyIcon from tools windows to your form and then add the following code to the event below, F5.



'//vb.net

Private Micona as Icon = New icon ("Icon1.ico")

Private miconb as Icon = New icon ("Icon2.ico")

Private micondisplayed as Boolean



Public Sub New ()

MyBase.New



Form1 = Me



' This is required by the Win Form Designer.

InitializeComponent



' Todo:add any initialization on the InitializeComponent () call



' This form isn ' t used directly so hide it immediately

Me.hide ()



' Setup the tray icon

Initializenotifyicon ()

End Sub



Private Sub Initializenotifyicon ()

' Setup the default icon

NotifyIcon = New System.Windows.Forms.NotifyIcon ()

Notifyicon.icon = Micona

Notifyicon.text = ' Right click ' for the ' menu '

Notifyicon.visible = True

micondisplayed = True



' Insert all MenuItem objects into a array and add them to

' The context menu simultaneously

Dim Mnuitms (3) as MenuItem

MNUITMS (0) = new MenuItem ("Show Form ...", New EventHandler (AddressOf me.showformselect))

MNUITMS (0). Defaultitem = True

MNUITMS (1) = new MenuItem ("Toggle Image", New EventHandler (AddressOf me.toggleimageselect))

MNUITMS (2) = New MenuItem ("-")

MNUITMS (3) = new MenuItem ("Exit", New EventHandler (AddressOf me.exitselect))

Dim notifyiconmnu as ContextMenu = New ContextMenu (MNUITMS)

NotifyIcon. ContextMenu = Notifyiconmnu

End Sub



Public Sub Showformselect (ByVal sender as Object, ByVal e as System.EventArgs)

' Display the Settings dialog

Dim Settingsform as New settingsform ()

Settingsform.showdialog ()



End Sub



Public Sub Toggleimageselect (ByVal sender as Object, ByVal e as System.EventArgs)

' Called when the ' user selects ' Toggle Image ' context menu



' Determine which icon is currently visible and switch it

If micondisplayed Then

' Called when the ' user selects ' Show Form ' context menu

Notifyicon.icon = miconb

Notifyicon.text = "Sad"

micondisplayed = False

Else

Notifyicon.icon = Micona

Notifyicon.text = "Happy"

micondisplayed = True

End If



End Sub



Public Sub Exitselect (ByVal sender as Object, ByVal e as System.EventArgs)

' Called when the ' user selects ' Exit ' context menu



' Hide the tray icon

Notifyicon.visible = False



' Close Up

Me.close ()

End Sub



' Form overrides dispose to clean up the component list.

Public Overloads Overrides Sub Dispose ()

Mybase.dispose ()

Components. Dispose ()

End Sub

Icon file you have prepared yourself, if successful you can see the various functions of notifyicond.



13. How to modify the control of the size and length of the form size.

It mainly modifies the size, Width, and Height properties of the WinForm. Also, they can be modified and set up at design and run time.

Form1.size = New System.Drawing.Size (vb.net)

Form1.width + + (vb.net)

Form1.height = (vb.net)



14. How to create a Windows Explorer-style form.

1. Create a new Windows application

2. Drag a TreeView control, a Splitterk control, and a ListView control from the Toolbox window to set the TreeView's Dock property in the Properties window as:: Left ; Set the Dock property of the ListView control to: Fill

3:f5 Run



15. How to set the initial startup form

In either C # or Visual Basic WinForm projects, you can right-click your project in the Solution Explorer window and select Properties to select the form you started or the main () method from your project's property page.

The difference is that in the current vs.net Beta2 the C # Project automatically generates the main () method, and you must add the main () code yourself in the Visual Basic.NET project, in C # you can change the Form1 to any form name you can launch:

(C #)

static void Main ()

{

Application.Run (New Form1 ());

}



16. How to create a form with a background image

All the forms in the WinForm now have a backgroundimage attribute, and you can only assign values to it. The normal form can also be completed in run mode in run mode. For example, add code such as InitializeComponent () or the constructor of a form:

This. BackgroundImage = new Bitmap ("C:\\dotnetapp\\winform\\tile.bmp");

For the main form of the MDI, in the Vs.net IDE form, when you have finished setting the IsMdiContainer property to True, you need to check to see if the InitializeComponent () has such code (C #):

This.mdiClient1.Dock = System.Windows.Forms.DockStyle.Fill;

This.mdiClient1.Name = "MdiClient1";

Or see mdiClient1 in the Window's Properties window combo box System.Windows.Forms.mdiClient. This is the main MDI window, but I don't find any description of System.Windows.Forms.mdiClient in Dotnet's documentation. You can then include this code in the InitializeComponent () or the constructor of the form (C #):

This.mdiClient1.BackgroundImage = new Bitmap ("C:\\dotnetapp\\winform\\tile.bmp");

There is a imageview example that shows a way to add a line of logo text to the background on the MDI main form, making your MDI form look commercial, and you can do this:

1. First, see if there is such a statement (C #) in the InitializeComponent of Vs.net automatically generating code:

This. Controls.AddRange (new system.windows.forms.control[] {this.mdiclient1});

Again this mdiclient (haha)

2. Create the following two functions to display this logo character:

(C #)

protected void Mdi_onpaint (Object s, System.Windows.Forms.PaintEventArgs e)

{

Control C = (control) s;





Rectangle r1 = C.clientrectangle;

R1. Width = 4;

R1. Height = 4;



Rectangle r2 = R1;

R2. Width = 1;

R2. Height = 1;



Font f = new Font ("Tahoma", 8);



String str = "Mywinform.net 2001 mywinform application V1.0";



StringFormat SF = new StringFormat ();

sf. Alignment = Stringalignment.far;

sf. LineAlignment = Stringalignment.far;



E.graphics.drawstring (str, F, new SolidBrush (Systemcolors.controldarkdark), R1, SF);

E.graphics.drawstring (str, F, new SolidBrush (systemcolors.controllight), r2, SF);



}



protected void Mdi_onresize (Object s, System.EventArgs e)

{



Control C = (control) s;

C.invalidate ();

}

3. Add this code to the InitializeComponent () or the constructor of the form:

(C #)

This. Controls[0]. Paint + = new Painteventhandler (mdi_onpaint);

This. Controls[0]. Resize + = new EventHandler (mdi_onresize);

Note that it is appended to the InitializeComponent () or after the function is this.Controls.AddRange in the InitializeComponent function.



--------------------------------------------------------------------------------


Overall, the whole WinForm part of the Hejlsberg design vj++ always appear in the WFC library breath, now there is no clue to prove that dotnet is copied WFC library, but I still believe that Delphi and vj++ Users will be more likely to feel the Hejlsberg design style, such as the event commissioned a few years ago was seen as leading and seriously violate the "pure Java" principle and the developers into embarrassment, now we can naturally enjoy this feature, But on the other hand dotnet and Java or Delphi seem to be closer, you can hardly like the MFC era to go from the source code to find secrets and insider.


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.