Winform various properties, methods, controls

Source: Internet
Author: User

A form is a visual interface for a program to interact with a user, a form is an object, a form class defines a template that generates a form, and a form is instantiated by instantiating a form class.

. The form class defined in the System.Windows.Forms namespace of the NET Framework class library is the base class for all form classes.

1 , Common Properties

Properties: Icon, title, location, background, etc. can be set in the form Properties panel, or code can be implemented.

(1) Name property: Used to get or set the name of the form, which can be referenced in the application by the Name property.
(2) WindowState property: Used to get or set the window state of a form. There are three types of values: normal (the form is displayed normally), minimized (form appears minimized), and maximized (the form is displayed in maximized form).
(3) StartPosition property: Used to get or set the start position of the run-time form.
(4) Text property: This property is a string property used to set or return the text displayed in the window's title bar.
(5)AcceptButtonProperties: This property is used to get or set a value that is the name of a button that is equivalent to clicking the button on the form when you press the ENTER key.
(6)CancelButtonProperties: This property is used to get or set a value that is the name of a button that is equivalent to clicking the button on the form when the ESC key is pressed.
(7)ModalProperties: This property is used to set whether the form is a modal display form. True if the form is displayed in a modal mode, otherwise false. When a form is displayed in a modal format, only the objects on the modal form can be entered. You must hide or close a modal form (typically in response to a user action) before you can enter another form. A form with a modal display is typically used as a dialog box in an application.
(8)ActiveControlProperties: Used to get or set the active control in a container control. A form is also a container control.
(9) Activemdichild property: The currently active child window used to obtain a multiple-document interface (MDI).
() AutoScroll property: Used to get or set a value that indicates whether the form implements automatic scrolling. If this property value is set to True, scroll bars are displayed on the form when any control is outside the form's workspace. Also, when auto-scrolling is on, the form's workspace automatically scrolls so that the control with the input focus is visible.
(one) the Enabled property: Used to get or set a value that indicates whether the control can respond to user interaction. True if the control can respond to user interaction, otherwise false. The default value is true.
KeyPreview property: Used to get or set a value that indicates whether the form will receive the event before the key event is passed to the control that has the focus. When the value is true, the form receives a key event and the value is false when the form does not receive the keystroke event.
(13)ShowInTaskbarProperties: Used to get or set a value that indicates whether the form is displayed in the Windows taskbar.
Visible property: Used to get or set a value that indicates whether the form or control is displayed. True when the form or control is displayed, False when not displayed.
Capture property: If the property value is true, the mouse is qualified to respond only to this control, regardless of whether the mouse is within the scope of the control.
2, Common methods

The form itself uses this example this.hide ();

Other Forms

Form2 F2 = new Form2 ();
F2. Show ();

The most common methods for some forms are described below.
(1) Show method: The function of this method is to let the form display, its invocation format is:
The name of the form. Show ();
Where the form name is the form names to display.
(2) Hide method: The function of this method is to hide the form, its invocation format is:
The name of the form. Hide ();
Where the form name is the form you want to hide.
(3) Refresh method: The function of this method is to refresh and redraw the form, whose invocation format is:
The name of the form. Refresh ();
The name of the form in which the form name is to be refreshed.
(4) Activate method: The function of this method is to activate the form and give it focus. Its invocation format is:
The name of the form. Activate ();
Where the form name is the names of the forms to be activated.
(5) Close method: The function of this method is to close the form. Its invocation format is:
The name of the form. Close ();
Where the form name is the form you want to close.
(6) ShowDialog method: The function of this method is to display the form as a modal dialog box. Its invocation format is:
The name of the form. ShowDialog ();

3 . Common Events

Add events for forms, controls, and actions in the Events tab of the Properties panel.

(1) Load event: This event occurs when a form is loaded into memory, that is, before the form is first displayed.
(2) Activated event: This event occurs when the form is activated.
(3) Deactivate event: This event occurs when the form loses focus and becomes inactive.
(4) Resize event: This event occurs when the form size is changed.
(5) Paint Event: This event occurs when the form is redrawn.
(6) Click event: This event occurs when the user clicks the form.
(7) DoubleClick Event: This event occurs when the user double-clicks the form.
(8) Closed event: This event occurs when the form is closed.

4. Add an event

Take the form Load event as an example:

The Load event occurs when the form is loaded and is set up as follows.
(1) Open VS2008, create a new Windows Forms application, and name it formeventtest.
(2) Open the Properties window for its Form1 and switch to the Events tab (a lightning bolt flag)
(3) Locate the Load event entry, and then double-click into the event code to edit the file Form1.cs to write code for it.

5. Inheriting forms

Inheriting a form is creating a new form that is identical to the structure of an existing form, a process that inherits from an existing form, called visual inheritance.

Two ways to create an inherited form

Programming Method: From2:from1

Inheritance selector:

6. Controls

Forms are made up of controls, which are divided into common controls and advanced controls, which are mainly text-class controls, select Class controls, and grouping controls.

The base class for C # controls is the control class under the System.Windows.Forms namespace

Text class controls: Label, Button, TextBox, RichTextBox

Select a class control: CheckBox, ComboBox, ListBox, RadioButton

Grouped controls: GroupBox, Panel, FlowLayoutPanel, SplitContainer, TabControl, TableLayoutPanel

7. Special implementations

( 1 ) Start the Welcome screen

Set the interface to the Welcome screen, add a Timer control, and start the main interface after a few seconds countdown

( 2 ) Multi-form application setup startup form

In Program.cs, change the parameters of the Run method

public static void Run (from mainfrom);

For example: Application.Run (New Form1 ());

Where Form1 is the name of the form

( 3 ) Program Exit

Form-related events

This.hide (); Hide

This.show (); Show

This.  Close (); Close, note that the entire application is closed

Application.exit (); Close, note that the entire application is closed

( 4 ) " Accept " buttons, " Cancel " Button

AcceptButton CancelButton

( 5 start the browser to open the Web page

System.Diagnostics.Process.Start (E.linktext); E.linktext Switch to specific links

(6) Determine if a string is a digital type

int A;
if (Int32.TryParse (TextBox1.Text, out a))
{
MessageBox.Show ("number");
}
Else
{
MessageBox.Show ("Non-digital");
}

8. text box textbox

Password text box: Set the PasswordChar property of the text box, or the Usesystempasswordchar property

Multiline text Box: Multiline property set to True

Highlighting: Setting Selecttionstart, SelectionLength properties

9. RichTextBox

A rich text control that is used to explicitly, enter, and manipulate formatted text, such as implementing explicit fonts, colors, links, loading from files, and embedded graphics, undoing duplicate edits, and finding string functions.

Drop - down combo box combobox

(1) DropDownStyle: Set style

Simple: List parts are always visible

DropDown: The default value, editable text box

DropDownList: Cannot edit text box

(2) SelectAll () method Select all text of an editable box

One check box CheckBox

CheckState property: Checked, value is checked, unchecked

radio button

Checked property: Whether position is selected true,

Numeric selection controls

(1) for displaying and entering values, provide up and down arrows, the user can enter directly, you can also use the arrows to modify the value

Maximum: Maximum Value

Mininmun: Minimum value

Value: Values

(2) explicit format

DecimalPlaces: Number of decimal places, default 0

Thousandsseparater: Thousand characters, default false

Hexadecimal: hexadecimal explicit

List control ListView

(1) Add Delete item: ListBox control the Add method of the Items property, the Remove method

(2) Currently selected item: SelectedItem property of the ListBox

(3) Multi-select: SelectionMode property

MultiExtended (SHIFT, CTRL key available)

MultiSimple Multi-choice

One: Radio

None: Cannot Select

(4) Number of items selected

ListBox1.SelectedItems.Count.ToString (); Multiple Choice Cases

Style, column header, width setting

Listview1.view = View.Details; View style,
LISTVIEW1.COLUMNS.ADD ("filename"); Add header
LISTVIEW1.COLUMNS.ADD ("path");
LISTVIEW1.COLUMNS.ADD ("size");
LISTVIEW1.COLUMNS.ADD ("Creation Time");
Listview1.columns[0]. Width = 100; Set width
LISTVIEW1.COLUMNS[1]. Width = 200; Set width
LISTVIEW1.COLUMNS[2]. Width = 100; Set width
LISTVIEW1.COLUMNS[3]. Width = 100; Set width

Add Item

ListView1.Items.Clear (); Empty

LISTVIEW1.ITEMS.ADD (Processes[i]. ProcessName);
LISTVIEW1.ITEMS[LISTVIEW1.ITEMS.COUNT-1]. SubItems.Add (Processes[i]. Id.tostring ());

LISTVIEW1.ITEMS[LISTVIEW1.ITEMS.COUNT-1]. SubItems.Add (Processes[i]. Id.tostring ());

LISTVIEW1.ITEMS[LISTVIEW1.ITEMS.COUNT-1]. SubItems.Add (Processes[i]. Id.tostring ());

. Grouping Controls

Panel:

GroupBox: Group Box

TabControl: Tab

(1) Set icon for tab, Change tab title

Create a Windows application, add a ImageList control to the form, and then add a graphical list like the ImageList control;

Add a TabControl control, set its ImageList property to the ImageList1 control, and set the ImageIndex property of the TabPage tab to the index of the corresponding image in the list.

(2) Display the tab as a button

Set the Appearence property of the TabControl control to buttons or flatbuttons, and you can display the tab as a button style. (Three-dimensional button, flat button)

Tabcontrol1.appearance = tabappearance.buttons;

(3) Add a new control to the tab

Initializes the class with new and calls the add

(4) Add and Remove tabs

Add: TabPages property of the Add method

string title = "Add Tab" + (tabcontrol1.tabcount+1);
TabPage mytabpage = new TabPage (title);
TABCONTROL1.TABPAGES.ADD (Mytabpage);

Remove: TabPages property of the Remove method

TabControl1.TabPages.Remove (Tabcontrol1.selectedtab);

Remove all Tabs: TabPages Property's clear

(5) DataGridView

Column width Setting not valid?

The AutoSizeMode property of the column needs to be set to none and the width setting will not take effect.

Center column headings

DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = Datagridviewcontentalignment.middlecenter; Center Title

Column Center Alignment

Datagridview1.columns[0]. Defaultcellstyle.alignment = Datagridviewcontentalignment.middlecenter;

How do I fill all the columns with the entire control?

In the top right corner of the DataGridView, click Edit Column, select the last column, and the property box to the right has a property of "layout", "AutoSizeMode", set it to fill and OK.

DataGridView Select a positive row instead of a cell when selected

Datagridview1.selectionmode = Datagridviewselectionmode.fullrowselect; Select a positive row, not a cell

Assignment of the leftmost column

DataGridView. Rows[i]. Headercell.value

Left-most column display and change width

Datagridview1.rowheadersvisible = false; Left-most column hidden
Datagridview1.rowheaderswidth = 60; Set width

Anti-delete error

int indexid = DataGridView1.CurrentRow.Index; When moving forward
MessageBox.Show (dataGridView1.Rows.Count.ToString () + IndexID. ToString ());
if (IndexID < 0 | | IndexID >= datagridview1.rows.count-1)
{ }
Else
{
if (MessageBox.Show ("OK to delete?") "," hint ", Messageboxbuttons.yesno, messageboxicon.warning) = = Dialogresult.yes)
{
DataGridView1.Rows.Remove (Datagridview1.currentrow);
}
}

Winform various properties, methods, controls

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.