国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

.Net WinForm學習筆記

2024-07-10 12:57:19
字體:
來源:轉載
供稿:網友
1,mdi窗體
設有兩個窗體frmmain,frmchild,則:
frmmain: 設ismdicontainer屬性為true
打開子窗口:
在相關事件中寫如下代碼:
frmchild child=new frmchild();
child.mdiparent=this;//this表示本窗體為其父窗體
child.show();
在打開子窗體時,如果只允許有一個子窗體,可以加入如下判斷:
if (this.activemdichild!=null)
{
this.activemdichild.close(); //關閉已經打開的子窗體
//....
}
更改mdi主窗體背景
先聲明一個窗體對象
private system.windows.forms.mdiclient m_mdiclient;
在form_load等事件中,添加如下代碼:
int icnt=this.controls.count;
for(int i=0;i<icnt;i++)
{
if(this.controls[i].gettype().tostring()=="system.windows.forms.mdiclient")
{
this.m_mdiclient=(system.windows.forms.mdiclient)this.controls[i];
break;
}
}
this.m_mdiclient.backcolor=system.drawing.color.silver;
具體可參見:http://cnblogs.com/daview/archive/2004/05/06/8381.aspx

2,創建系統托盤菜單
2.1,創建一個contextmenu(cmnmain)菜單
2.2,添加一個notifyicon組件,設置contextmenu屬性為cmnmain
2.3,相應窗體改變事件(最小化等)
private void frmmain_sizechanged(object sender,eventargs e)
{
if (this.windowstate==formwindowstate.minimized)
{
this.hide();
noimain.visible=true;
}
}

2.4,相應用戶單擊系統托盤上contextmenu菜單事件
private void mniopen(object sender,eventargs e)
{
noimain.visible=false;
this.show();
this.focus();
}

2.5,響應用戶雙擊系統托盤圖標事件
private void noimain_doubleclick(object s,eventargs e)
{
minopen.performclick(); //相當與mniopen按鈕的單擊事件
}
**注意添加相應的事件句柄**

3,創建不規則窗體
3.1,在窗體上創建不規則圖象,可以用gdi+繪制,或在圖象控件上使用圖象填充
3.2,設置窗體的backcolor為colora,然后設置transparencykey為colora
3.3,設置formborderstyle為none;

4,創建頂部窗體
this.topmost=true;//把窗體的topmost設置為true


5,調用外部程序

using system.diagnostics

process proc=new process();
[email protected]"notepad.exe"; //注意路徑
proc.startinfo.arguments="";
proc.start();

//獲得當前目錄directory.getcurrentdirectory() (using system.io)

6,toolbar的使用
toolbar控件通常需要imagelist控件結合使用(需要用到其中圖標)
響應toolbar單擊事件處理程序代碼:
switch(toolbarname.buttons.indexof(e.button))
{
case 0: //第一個按鈕
//code ...
break;
case 1: //第二個按鈕
//code ...
break;
//other case code
default: //默認處理,但以上所有項都不符合時
//code ...
break;
}

7,彈出對話框獲得相關返回值
在窗體的closing事件中運行如下代碼,可以在用戶關閉窗體時詢問
dialogresult result=messagebox.show(this,"真的要關閉該窗口嗎?","關閉提示",messageboxbuttons.okcancel,messageboxicon.question);
if (result==dialogresult.ok)
{
//關閉窗口
e.cancel=false;
}
else
{
//取消關閉
e.cancel=true;
}

8,打印控件
最少需要兩個控件
printdocument
printpreviewdialog:預覽對話框,需要printdocument配合使用,即設置document屬性為
對應的printdocument
printdocument的printpage事件(打印或預覽事件處理程序)代碼,必須.

float fltheight=0;
float fltlineperpage=0;
long lngtopmargin=e.marginbounds.top;
int intcount=0;
string strline;

//計算每頁可容納的行數,以決定何時換頁
fltlineperpage=e.marginbounds.height/txtprinttext.font.getheight(e.graphics);


while(((strline=streamtoprint.readline()) != null) && (intcount<fltlineperpage))
{
intcount+=1;
fltheight=lngtopmargin+(intcount*txtprinttext.font.getheight(e.graphics));
e.graphics.drawstring(strline,txtprinttext.font,brushes.green,e.marginbounds.left,fltheight,new stringformat());
}

//決定是否要換頁
if (strline!=null)
{
e.hasmorepages=true;
}
else
{
e.hasmorepages=false;
}
以上代碼的streamtoprint需要聲明為窗體級變量:
private system.io.stringreader streamtoprint;

打開預覽對話框代碼(不要寫在printpage事件中)
streamtoprint=new system.io.stringreader(txtprinttext.text);
printpreviewdialogname.showdialog();

9,string對象本質與stringbuilder類,字符串使用
string對象是不可改變的類型,當我們對一個string對象修改后將會產生一個新的string對
象,因此在需要經常更改的字符對象時,建議使用stringbuilder類:
[范例代碼]構造一個查詢字符串
stringbuilder sb=new stringbuilder("");
sb.append("select * from employees where ");
sb.append("id={0} and ");
sb.append("title='{1}'");
string cmd=sb.tostring();

sb=null; //在不再需要時清空它

cmd=string.format(cmd,txtid.text,txttile.text); //用實際的值填充格式項

判斷字符串是否為空:
檢查一個字符串是否為空或不是一個基本的編程需要,一個有效的方法是使用string類的length屬性來取代使用null或與""比較。

比較字符串:使用string.equals方法來比較兩個字符串
string str1="yourtext";
if (str1.equals("teststing") )
{
  // do something
}

10,判斷某個字符串是否在另一個字符串(數組)中
需要用到的幾個方法
string.split(char);//按照char進行拆分,返回字符串數組
array.indexof(array,string):返回指定string在array中的第一個匹配項的下標
array.lastindexof(array,string):返回指定string在array中的最后一個匹配項的下標
如果沒有匹配項,則返回-1
[示例代碼]:
string strnum="001,003,005,008";
string[] strarray=strnum.split(',');//按逗號拆分,拆分字符為char或char數組

console.writeline(array.indexof(strarray,"004").tostring());

11,datagrid與表和列的映射
從數據庫讀取數據綁定到datagrid后,datagrid的列標頭通常跟數據庫的字段名相同,如果
不希望這樣,那么可以使用表和列的映射技術:
using system.data.common;

string strsql="select * from department";
oledbdataadapter adapter=new oledbdataadapter(strsql,conn);

datatablemapping dtmdep=adapter.tablemappings.add("department","部門表");

dtmdep.columnmappings.add("dep_id","部門編號");
dtmdep.columnmappings.add("dep_name","部門名稱");

dataset ds=new dataset();

adapter.fill(ds,"department"); //此處不能用"部門表"

響應單擊事件(datagrid的currentcellchanged事件)
datagridname.currentcell.columnnumber;//所單擊列的下標,從0開始,下同
datagridname.currentcell.rownumber;//所單擊行的下標
datagridname[datagridname.currentcell];//所單擊行和列的值

datagridname[datagridname.currentrowindex,n].tostring();//獲得單擊行第n+1列的值

12,動態添加菜單并為其添加響應事件
添加頂級菜單:
mainmenuname.menuitems.add("頂級菜單一");//每添加一個將自動排在后面

添加次級菜單:
menuitem mniitemn=new menuitem("menuitemtext")
menuitem mniitemn=new menuitem("menuitemtext",new eventhandler(eventdealname))
mainmenuname.menuitems[n].menuitems.add(mniitemn);//n為要添加到的頂級菜單下標,從0開始

創建好菜單后添加事件:
mniitemn.click+=new eventhandler(eventdealname);

也可以在添加菜單的同時添加事件:
menuitem mniitemn=new menuitem("menuitemtext",new eventhandler(eventdealname));
mainmenuname.menuitems[n].menuitems.add(mniitemn);

13,正則表達式簡單應用(匹配,替換,拆分)
using system.text.regularexpressions;

//匹配的例子
string strregextext="你的號碼是:020-32234102";
string [email protected]"/d{3}-/d*";

regex regex=new regex(filter);
match match=regex.match(strregextext);

if (match.success) //判斷是否有匹配項
{
console.writeline("匹配項的長度:"+match.length.tostring());
console.writeline("匹配項的字符串:"+match.tostring());
console.writeline("匹配項在原字符串中的第一個字符下標:"+match.index.tostring());
}

//替換的例子
string replacedtext=regex.replace(strregextext,"020-88888888");
console.writeline(replacedtext);//輸出"你的號碼是:020-88888888"

//拆分的例子
string strsplittext="甲020-32654已020-35648丙020-365984";
foreach(string s in regex.split(strsplittext))
{
console.writeline(s); //依次輸出"甲乙丙"
}

13,多線程簡單編程
using system.threading;

thread threadtest=new thread(new threadstart(threadcompute));
threadtest.start();//使用另一個線程運行方法threadcompute

threadcompute方法原型:
private void threadcompute()
{}

14,操作注冊表
using system.diagnostics;
using microsoft.win32;
//操作注冊表
registrykey regkey=registry.localmachine.opensubkey("software",true);

//添加一個子鍵并給他添加鍵值對
registrykey newkey=regkey.createsubkey("regnewkey");
newkey.setvalue("keyname1","keyvalue1");
newkey.setvalue("keyname2","keyvalue2");

//獲取新添加的值
messagebox.show(newkey.getvalue("keyname1").tostring());

//刪除一個鍵值(對)
newkey.deletevalue("keyname1");

//刪除整個子鍵
regkey.deletesubkey("regnewkey");
  • 本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。
  • 發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 宁城县| 东阿县| 黄山市| 吴旗县| 府谷县| 建宁县| 湘潭县| 刚察县| 搜索| 黎平县| 莱州市| 桑日县| 清苑县| 本溪| 海南省| 大冶市| 社会| 辽宁省| 桃园市| 灵璧县| 盐山县| 青州市| 临湘市| 浦北县| 凤庆县| 新源县| 定州市| 临猗县| 开封县| 巍山| 黄骅市| 平山县| 连南| 连平县| 松阳县| 罗甸县| 若尔盖县| 阿拉善右旗| 文化| 页游| 文化|