asp.net一些很酷很實用的.Net技巧第2/2頁
更新時間:2008年08月04日 09:21:05 作者:
方便使用asp.net編程的朋友,都是一些非常有用的東西
二
. OOPs
1. 什么是復制構造函數(shù)
我們知道構造函數(shù)是用來初始化我們要創(chuàng)建實例的特殊的方法。通常我們要將一個實例賦值給另外一個變量c#只是將引用賦值給了新的變量實質上是對同一個變量的引用,那么我們怎樣才可以賦值的同時創(chuàng)建一個全新的變量而不只是對實例引用的賦值呢?我們可以使用復制構造函數(shù)。
我們可以為類創(chuàng)造一個只用一個類型為該類型的參數(shù)的構造函數(shù),如:
public Student(Student student)
{
this.name = student.name;
}
使用上面的構造函數(shù)我們就可以復制一份新的實例值,而非賦值同一引用的實例了。
class Student
{
private string name;
public Student(string name)
{
this.name = name;
}
public Student(Student student)
{
this.name = student.name;
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
}
class Final
{
static void Main()
{
Student student = new Student ("A");
Student NewStudent = new Student (student);
student.Name = "B";
System.Console.WriteLine("The new student's name is {0}", NewStudent.Name);
}
}
The new student's name is A.
2.什么是只讀常量
就是靜態(tài)的只讀變量,它通常在靜態(tài)構造函數(shù)中賦值。
class Numbers
{
public readonly int m;
public static readonly int n;
public Numbers (int x)
{
m=x;
}
static Numbers ()
{
n=100;
}
} //其中n就是一個只讀的常量,對于該類的所有實例他只有一種值,而m則根據(jù)實例不同而不同
三.VS.Net IDE
1. 2請看原作
3.如何改變region的顏色
通過工具 à 選項 à 環(huán)境 à 字體和顏色 à 可折疊文本設置
四.WinForm
1.如何使winForm不顯示標題欄?
通過設置form的Text屬性為空字符串,設置ControlBox屬性為false
form1.Text = string. Empty;
form1.ControlBox = false;
2.如何使winform的窗體使用XP的風格
見原作
3.如何禁止form在工具欄顯示
設置form的ShowInTaskbar屬性為false即可
4.如何使程序打開默認的郵件程序并帶有一些參數(shù)讓用戶開始寫郵件
1)如果是web程序:
<a href=”mailto:email@address1.com,email@address2.com?cc=email@address3.com&Subject=Hello&body=Happy New Year”>some text</a>
2) 對于windows程序,需要使用System.Diagnostics.Process類
Process process = new Process();
process.StartInfo.FileName = "mailto:email@address1.com,email@address2.com?subject=Hello&cc=email@address3.com
&bcc=email@address4.com&body=Happy New Year" ;
process.Start();
5.如何創(chuàng)建類似msn提示窗口
需要獲得通過Screen.GetWorkingArea(this).Width(Height)屬性獲得屏幕的大小,然后使用一個timer根據(jù)時間改變窗口的位置
五.Button控件
1.如何設置form的默認button(即在form上按下回車時觸發(fā)的button)
可以設置form的AcceptButton屬性:form1.AcceptButton = button1;
2. 如何設置form的取消button(即在用戶按下Esc鍵時觸發(fā)的button)
可以設置form的CancelButton屬性:form1.CancelButton = buttonC;
3. 如何通過程序觸發(fā)一個button的Click事件
Button1.PerformClick
六.Combo Box
1.如何使用可選字體填充Combo Box
comboBox1.Items.AddRange (FontFamily.Families);
七.TextBox
1.如何禁用TextBox的默認上下文菜單(右鍵菜單)
textBox1.ContextMenu = new ContextMenu();
2,3 見原作
4.如何在TextBox獲得焦點的時候,將焦點放在textBox文字的最后
textBox1.SelectionStart = textBox1.Text.Length;
相關文章
ASP.NET WebService中使用ASP.NET_SessionId的問題說明
proxy.CookieContainer存儲了客戶端的 ASP.NET_SessionId。這樣以后每次通過webservice 方法調用時,都會將ASP.NET_SessionId傳遞到服務器端。2011-09-09
.Net?ORM?訪問?Firebird?數(shù)據(jù)庫的方法
這篇文章簡單介紹了在?.net6.0?環(huán)境中使用?FreeSql?對?Firebird?數(shù)據(jù)庫的訪問,目前?FreeSql?還支持.net?framework?4.0?和?xamarin?平臺上使用,對.Net?ORM?訪問?Firebird?數(shù)據(jù)庫相關知識感興趣的朋友一起看看吧2022-07-07

