天天看点

[转]Some Cool Tips for .NET

  原文地址:http://www.codeproject.com/useritems/tips.asp

Use the <code>GetFolderPath</code> method of the <code>System.Environment</code> class to retrieve this information.

The Application class has a static member <code>ExecutablePath</code> that has this information.

Use System.Environment's <code>OSVersion</code> static (shared) property.

Use <code>System.IO.Path.GetFileName</code> and <code>System.IO.Path.GetFileNameWithoutExtension</code> static methods.

Use <code>System.IO.Path.GetExtension</code> static method.

<a href="http://chinasf.cnblogs.com/admin/tips/Tip23_src.zip">Download source files - 8.65 Kb</a>

<a href="http://chinasf.cnblogs.com/admin/tips/Tip23_demo.zip">Download demo project - 4.60 Kb</a>

Use <code>System.Windows.Forms.SystemInformation</code>. <code>SystemInformation</code> provides static (Shared in Visual Basic) methods and properties that can be used to get information such as Windows display element sizes, operating system settings, network availability, and the capabilities of hardware installed on the system.This class cannot be instantiated. e.g

<a href="http://www.codeproject.com/cs/database/FinalCSVReader.asp">Importing CSV files using .NET application</a>

<a href="http://chinasf.cnblogs.com/admin/tips/Freespace_src.zip">Download source files - 10.0 Kb</a>

<a href="http://chinasf.cnblogs.com/admin/tips/Freespace_demo.zip">Download demo project - 5.48 Kb</a>

and then you can use it as

In VS.NET Editor in Code View you can select and drag code to a Tab (other than the Clipboard ring) in the ToolBox and then you can drag the code from the Tab to the desired location.

In the VS.NET IDE , from Menu select <code>TOOLS &gt; OPTIONS&gt;TEXT EDITOR (in left window) &gt;ALL LANGUAGES &gt; LINE NUMBERS</code>You can show Line Numbers depending on languages by selecting specific language in TEXT EDITOR like C# or Basic and checking Line Numbers option

In the VS.NET IDE , from Menu select <code>TOOLS &gt; OPTIONS &gt; ENVIRONMENT (in left window) &gt; FONTS AND COLORS &gt; DISPLAY ITEMS (on right side) &gt;COLLAPSIBLE TEXT</code>. After selecting this you can set the color.

Set the form's Text and ControlBox properties.

You need to install XP on your machine and the theme should be set to XP.You also need to ensure that the <code>FlatStyle</code> property of the control is changed to <code>System</code> and then add following line of code in your <code>Main()</code> method

Set the form's <code>ShowInTaskbar</code> property to <code>False</code> to prevent it from being displayed in the Windows taskbar.

This can be achieved very easily though it seems complicated in beginning. Use <code>MailTo</code> command for this .I had faced this problem while developing Windows Application. This is very common command and known to many web developers but the great thing about this command is that it works as smoothly with windows application as it works with Web Application.

<b>MailTo:[email protected]</b>

<code></code>Some other features of this commands are discussed below:

Features

Syntax

Address message to multiple recipients

, (comma separating e-mail addresses)

Add entry in the "Subject" field

?subject=Subject Field Text

Add entry in the "Copy To" or "CC" field

&amp;[email protected];

Add entry in the "Blind Copy To" or "BCC" field

&amp;[email protected]

Add entry in the "Body" field

&amp;body=Your message here

Following is the complete syntax:

Use <code>System.Diagnostics.Process</code>

<a href="http://chinasf.cnblogs.com/admin/tips/popupform_src.zip">Download source files - 27.2 Kb</a>

<a href="http://chinasf.cnblogs.com/admin/tips/popupform_demo.zip">Download demo project - 13.7 Kb</a>

I always thought about the ways to create alert windows like messenger pop-ups.This is one such attempt. The logic is simple. Just open the form below taskbar and slowly bring it up using timer so we can have the animated effect. Similarly you can close it. One thing we should be careful about is the 'Resolution' of monitor, since the location of form will change accordingly. So instead of directly locating the form with pre-defined co-ordinates, we will calculate the working area of the screen and place our form accordingly. To calculate the working area of the screen we can use <code>Screen. GetWorkingArea(control)</code>, which gives the working area of the screen without taskbar.

We can position the form initially using following method:

Now we will open the form as below:

Similarly we'll close the form

<code>Default</code> Button of a form means that button on form whose click

event fires when <code>Enter</code> key is pressed. To make a button on form as

default set the form's <code>AcceptButton</code> property. You can do this

either through the designer, or through code such as

<code>Cancel</code> Button of a form means that button on form whose click

event fires when <code>ESC</code> key is pressed. To make a button on form as

Cancel set the form's <code>CancelButton</code> property. You can do this either

through the designer, or through code such as

In VB 6.0 it was possible to call CommandButton click event from anywhere

like any other method or function (Sub). But in .NET it is not possible in same

way. But .NET provides a very simple way to do this. Just use the button's

public method <code>PerformClick</code>.

You

can trigger a button (Web and Win) by calling <code>Buttonclick</code> with null

parameters

You can use it this way, if you're planning on doing

something with the sender object, you have a reference to it.

To prevent the default context menu of a TextBox from showing up, assign a

empty context menu as shown below:

Sometimes it is needed to show data on different lines. The first idea that

comes is to set <code>MULTILINE</code> Property to true and use <code>'\n'</code>escape sequence for this. But this escape sequence is not supported in

.NET textbox. Still it very easy to overcome this problem. To assign multiline

text at design time, in the designer window use the <code>LINES</code> property

of TextBox control. For achieving this at runtime, create an array of string and

assign it to <code>LINES</code> property of Textbox as shown below.

Use following code in some Button Click event:

The checkbox in checkbox column of datagrid shows indeterminate status by

default. To remove this behaviour set <code>AllowNull</code> property of the

CheckBox column to <code>false</code> as below:

<a href="http://chinasf.cnblogs.com/admin/tips/GridGrouping_src.zip">Download source files - 9.57 Kb</a>

<a href="http://chinasf.cnblogs.com/admin/tips/GridGrouping_demo.zip">Download demo project - 5.26 Kb</a>

Hi friends, you may be knowing better ways of doing it, but I managed

to find this solution in the time limit I had been given. The logic is that,

while looping through datatable we save the values of current column and

previous column and we compare it. If Current Value is same as Previous Value,

we don't show it in grid and if it is not same then we show it.

You can make a panel or label transparent by specifying the <code>alpha value</code> for the <code>Background color</code>.

This is very common problem, as a newcomer I had spent great deal of time to

solve this. Suppose you have two controls viz, Combobox and Listbox on same form

and Datasource property of both these controls is same. Then the selection in

one control selects same item in other control. This problem occurs because of

<code>BindingContext</code> property of controls. By default the BindingContext

member of each of the two controls is set to the Form's BindingContext. Thus,

the default behavior is for the ComboBox and Listbox to share the same

BindingContext, and that's why the selection in the ComboBox is synchronized

with the current item of Listbox. If you do not want this behavior, create a new

BindingContext member for at least one of the controls.

The following block makes the status bar of Crystal Report invisible.

Following block of code generates PDF version of Crystal Report

programmatically.

You can accomlish this using PrintDialog control and get the computer name

using <code>printDialog1.PrinterSettings.PrinterName</code>

<a href="http://www.codeproject.com/cs/database/DBInstaller.asp">Solution 1</a>

<a href="http://www.codeproject.com/cs/database/DBInstaller1.asp">Solution 2</a>

<a href="http://www.codeproject.com/cs/database/msdeinstaller.asp">Installing MSDE with .NET Application</a>

<a href="http://www.codeproject.com/dotnet/shortcut.asp">Creating Conditional shortcuts</a>

Usually the underline appears only after you press the Alt Key, but you can

enable it by changing the Operating System Settings. On Windows XP, Right Click

Desktop to bring up the Display Properties Dialog and then choose Appearance tab

and then the Effects Button and uncheck the checkbox <code>"Hide Underlined letters for keyboard navigation until I press the ALT Key"</code>.

Though this in not related to .NET directly but it is useful while working

with ADO.NET

)&gt;

继续阅读