asp.net一些很酷很實(shí)用的.Net技巧
更新時(shí)間:2008年08月04日 09:21:05 作者:
方便使用asp.net編程的朋友,都是一些非常有用的東西
二
. OOPs
1. 什么是復(fù)制構(gòu)造函數(shù)
我們知道構(gòu)造函數(shù)是用來初始化我們要?jiǎng)?chuàng)建實(shí)例的特殊的方法。通常我們要將一個(gè)實(shí)例賦值給另外一個(gè)變量c#只是將引用賦值給了新的變量實(shí)質(zhì)上是對同一個(gè)變量的引用,那么我們怎樣才可以賦值的同時(shí)創(chuàng)建一個(gè)全新的變量而不只是對實(shí)例引用的賦值呢?我們可以使用復(fù)制構(gòu)造函數(shù)。
我們可以為類創(chuàng)造一個(gè)只用一個(gè)類型為該類型的參數(shù)的構(gòu)造函數(shù),如:
public Student(Student student)
{
this.name = student.name;
}
使用上面的構(gòu)造函數(shù)我們就可以復(fù)制一份新的實(shí)例值,而非賦值同一引用的實(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)構(gòu)造函數(shù)中賦值。
class Numbers
{
public readonly int m;
public static readonly int n;
public Numbers (int x)
{
m=x;
}
static Numbers ()
{
n=100;
}
} //其中n就是一個(gè)只讀的常量,對于該類的所有實(shí)例他只有一種值,而m則根據(jù)實(shí)例不同而不同
三.VS.Net IDE
1. 2請看原作
3.如何改變r(jià)egion的顏色
通過工具 à 選項(xiàng) à 環(huán)境 à 字體和顏色 à 可折疊文本設(shè)置
四.WinForm
1.如何使winForm不顯示標(biāo)題欄?
通過設(shè)置form的Text屬性為空字符串,設(shè)置ControlBox屬性為false
form1.Text = string. Empty;
form1.ControlBox = false;
2.如何使winform的窗體使用XP的風(fēng)格
見原作
3.如何禁止form在工具欄顯示
設(shè)置form的ShowInTaskbar屬性為false即可
4.如何使程序打開默認(rèn)的郵件程序并帶有一些參數(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)屬性獲得屏幕的大小,然后使用一個(gè)timer根據(jù)時(shí)間改變窗口的位置
五.Button控件
1.如何設(shè)置form的默認(rèn)button(即在form上按下回車時(shí)觸發(fā)的button)
可以設(shè)置form的AcceptButton屬性:form1.AcceptButton = button1;
2. 如何設(shè)置form的取消button(即在用戶按下Esc鍵時(shí)觸發(fā)的button)
可以設(shè)置form的CancelButton屬性:form1.CancelButton = buttonC;
3. 如何通過程序觸發(fā)一個(gè)button的Click事件
Button1.PerformClick
六.Combo Box
1.如何使用可選字體填充Combo Box
comboBox1.Items.AddRange (FontFamily.Families);
七.TextBox
1.如何禁用TextBox的默認(rèn)上下文菜單(右鍵菜單)
textBox1.ContextMenu = new ContextMenu();
2,3 見原作
4.如何在TextBox獲得焦點(diǎn)的時(shí)候,將焦點(diǎn)放在textBox文字的最后
textBox1.SelectionStart = textBox1.Text.Length;
相關(guān)文章
ASP.NET WebService中使用ASP.NET_SessionId的問題說明
proxy.CookieContainer存儲了客戶端的 ASP.NET_SessionId。這樣以后每次通過webservice 方法調(diào)用時(shí),都會(huì)將ASP.NET_SessionId傳遞到服務(wù)器端。2011-09-09
ASP.net處理XML數(shù)據(jù)實(shí)例淺析
這篇文章主要介紹了ASP.net處理XML數(shù)據(jù)實(shí)例淺析,分析了XML的原理與用法,并以實(shí)例形式講述了asp.net處理XML數(shù)據(jù)的方法,需要的朋友可以參考下2014-10-10
.Net?ORM?訪問?Firebird?數(shù)據(jù)庫的方法
這篇文章簡單介紹了在?.net6.0?環(huán)境中使用?FreeSql?對?Firebird?數(shù)據(jù)庫的訪問,目前?FreeSql?還支持.net?framework?4.0?和?xamarin?平臺上使用,對.Net?ORM?訪問?Firebird?數(shù)據(jù)庫相關(guān)知識感興趣的朋友一起看看吧2022-07-07
ASP.NET MVC 2右鍵菜單和簡單分頁實(shí)例講解
在這里我們將討論的是通過一個(gè)插件實(shí)現(xiàn)ASP.NET MVC 2中的右鍵菜單和一個(gè)相當(dāng)簡單的分頁,希望對大家有所幫助。2015-09-09

