Several C # programming tips

xiaoxiao2021-03-06  90

First, minimize windows

Click "X" or "Alt F4", minimize windows,

Such as:

Protected Override Void WndProc (Ref Message M)

{

Const int WM_SYSCOMMAND = 0x0112;

Const int sc_close = 0xf060;

IF (M.MSG == WM_SYSCOMMAND & & (INT) m.wparam == sc_close)

{

// User Clicked Close Button

this.windowState = formwindowstate.minimized;

Return;

}

Base.WndProc (Ref M);

}

Second, how to make the foreach loop faster

Foreach is a ready-made statement that makes a simple enumeration and processing of elements in the collection, as shown in the following example:

Using system;

Using system.collections;

Namespace looptest

{

Class class1

{

Static void main (string [] args)

{

// Create an ArrayList of Strings

Arraylist array = new arraylist ();

Array.Add ("Marty");

Array.Add ("Bill");

Array.Add ("George");

// Print The Value of Every Item

FOREACH (String Item in Array)

{

Console.writeLine (item);

}

}

}

You can use the foreach statement in each of the set of Ienumerable interfaces. If you want to know more foreach usage, you can view the C # Language Specification in the .NET Framework SDK document.

When compiling, the C # editor converts each Foreach area. IEnumerator Enumerator = array.getenumerator ();

Try

{

String item;

While (enumerator.movenext ())

{

Item = (string) enumerator.current;

Console.writeLine (item);

}

}

Finally

{

Idisposable d = enumerator as idisposable;

IF (d! = null) d.dispose ();

}

This shows that in the background, Foreach's management will bring you some additional code that increases system overhead.

Third, save pictures to an XML file

In WinForm's resource file, non-text content such as porturebox's image property is converted into text, which is implemented by serialization.

example://

Using system.Runtime.Serialization.formatters.soap;

Stream Stream = New FileStream ("E: //Image.xml", FileMode.create, FileAccess.write, Fileshare.none;

SOAPFORMATTER F = New soapFormatter ();

Image img = image.fromfile ("e: //image.bmp");

F.Serialize (Stream, IMG);

stream.close ();

Fourth, shield Ctrl-V

The TextBox control in WinForm does not block the Ctrl-V clipboard paste action, if you need an input box, you don't want the user to paste the contents of the clipboard, you can use the RichTextBox control, and shield the Ctrl-V key in KeyDown ,example:

Private void RichtextBox1_keydown (Object Sender, System.Windows E)

{

IF (E.Control && E.Keycode == Keys.v)

e.handled = true;

}

First, determine if the file or folder exists

Use system.io.file to check if there is a file that exists very simple:

BOOL EXIST = System.IO.File.exists (filename);

If you need to judge whether you exist (folder), you can use system.io.directory:

BOOL EXIST = System.io.directory.exists (FolderName);

Second, design custom events with Delegate type

In C # Programming, in addition to Method and Property, any Class can have its own events (Event). The steps to define and use custom events are as follows:

(1) Define a Delegate type outside of the class to determine the interface of the event program.

(2) Inside the Class, a public event variable is declared, the type defined by the previous step is the Delegate type defined.

(3) Trigger events in some Method or Property

(4) Use the = operator in the Client program to specify event handler

Example: // Define the Delegate type, constraint event program parameters

Public Delegate Void MyEventHandler (Object Sender) and Long Linenumber;

Public Class DataImports

{

/ / Define new events newlineRead

Public evenet myeventhandler newlineread;

Public void importdata ()

{

Long i = 0; // Event parameters

While ()

{

i ;

// trigger event

IF (newLineRead! = null) NewLineRead (this, i);

// ...

}

// ...

}

// ...

}

// The following is a Client code

Private void callmethod ()

{

// Declare a Class variable, no needsss

Private dataImports_da = null;

/ / Specify an event handler

_da.newlineRead = new myeventhandler (this.da_enternewline);

// Call the Class method, trigger an event on the way

_da.importdata ();

}

// Event handler

Private void da_enternewline (Object Sender, long lineenumber)

{

// ...

}

Third, IP and host name analysis

Use System.net to implement an IP parsing function similar to the ping command line, such as resolving the hostname to IP or in turn: private string gethostnamebyip (string ipaddress) {

IPHOSTENTRY HOSTINFO = DNS.GETOSTBYADDRESS (IPADDRESS);

Return Hostinfo.hostname;

}

Private string getipbyhostname (String hostname)

{

System.net.iphostentry Hostinfo = DNS.GETHOSTBYNAME (Hostname);

Return Hostinfo.addresslist [0] .tostring ();

}

转载请注明原文地址:https://www.9cbs.com/read-107622.html

New Post(0)