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

首頁(yè) > 開發(fā) > 綜合 > 正文

C#,深入淺出全接觸(四)

2024-07-21 02:27:11
字體:
供稿:網(wǎng)友
 

2、用visual c# 創(chuàng)建windows應(yīng)用程序
在visual c#創(chuàng)建一個(gè)windows (gui) 應(yīng)用程序要以前版本的vc++ 容易得多。下面將介紹用visual c#工程文件向?qū)?chuàng)建windows應(yīng)用程序的過程。
創(chuàng)建應(yīng)用程序框架
在vs .net ide中選擇“新建->工程文件->visual c# 工程文件->windows 應(yīng)用程序”:



然后點(diǎn)擊 ok,出現(xiàn)一個(gè)表單設(shè)計(jì)視圖(這與vb或delphi相同)。在右側(cè)我們看到了一個(gè)解決方案導(dǎo)航器( solution explorer)。向?qū)樾卤韱卧黾恿艘粋€(gè)form1.cs 文件,其中包括了這個(gè)表單及其所有子窗口的的代碼:


雙擊 form1.cs就能看到這個(gè)代碼:
namespace mcwinformsapp
{
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.winforms;
using system.data;
/// <summary>
/// summary description for form1.
/// </summary>
public class form1 : system.winforms.form
{
/// <summary>
/// required designer variable.
/// </summary>
private system.componentmodel.container components;
public form1()
{
//
// required for windows form designer support
//
initializecomponent();
//
// todo: add any constructor code after initializecomponent call
//
}
/// <summary>
/// clean up any resources being used.
/// </summary>
public override void dispose()
{
base.dispose();
components.dispose();
}
/// <summary>
/// required method for designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void initializecomponent()
{
this.components = new system.componentmodel.container ();
//@this.trayheight = 0;
//@this.traylargeicon = false;
//@this.trayautoarrange = true;
this.text = "form1";
this.autoscalebasesize = new system.drawing.size (5, 13);
this.click += new system.eventhandler (this.form1_click);
}
protected void form1_click (object sender, system.eventargs e)
{
}
/// <summary>
/// the main entry point for the application.
/// </summary>
public static void main(string[] args)
{
application.run(new form1());
}
}
}
從以上代碼中,我們看到:向?qū)г黾恿艘粋€(gè)默認(rèn)的名稱空間以及對(duì)winforms 所要求的不同名稱空間的引用;form1 類是從system.winforms.form中派生出來的;initializecomponent方法負(fù)責(zé)初始化(創(chuàng)建)表單及其控件(當(dāng)在表單中托放下一些控件時(shí),可以看到它的更多細(xì)節(jié));dispose方法負(fù)責(zé)清除所有不再使用的資源。
添加控件
要向一個(gè)表單中添加控件或者子窗口,需要打開 工具箱toolbox。這個(gè)工具箱的概念來自vb。點(diǎn)擊菜單“視圖->工具箱”,激活工具箱功能:


toolbox(工具箱)窗口的樣子如下圖所示。現(xiàn)在就可以添加控件了,添加方法與visual studio的以前版本一樣,拖放或者雙擊控件都可以。


首先在表單上托放下一個(gè)按鈕和一個(gè)編輯框,然后讓我們看看系統(tǒng)向初始組件(initializecomponent)中增加了什么東西。


接著在屬性窗口中設(shè)置控件的屬性,這與vb中的操作方式一樣。在控件上點(diǎn)擊右鍵,并點(diǎn)中“屬性”菜單條就可以調(diào)出屬性窗口。



現(xiàn)在看看initializecomponent方法,就會(huì)發(fā)現(xiàn)這些代碼已經(jīng)增加到其中了。接著手工修改一下這些代碼:
this.components = new system.componentmodel.container ();
this.button1 = new system.winforms.button ();
this.textbox1 = new system.winforms.textbox ();
//@this.trayheight = 0;
//@this.traylargeicon = false;
//@this.trayautoarrange = true;
button1.location = new system.drawing.point (16, 24);
button1.size = new system.drawing.size (88, 32);
button1.tabindex = 0;
button1.text = "browse";
button1.click += new system.eventhandler (this.button1_click);
textbox1.location = new system.drawing.point (128, 32);
textbox1.text = "textbox1";
textbox1.tabindex = 1;
textbox1.size = new system.drawing.size (144, 20);
this.text = "form1";
this.autoscalebasesize = new system.drawing.size (5, 13);
this.click += new system.eventhandler (this.form1_click);
this.controls.add (this.textbox1);
this.controls.add (this.button1);
添加事件處理器
最后,要為按鈕增加一個(gè)事件處理器,實(shí)現(xiàn)瀏覽文件的目的。在按鈕上雙擊,打開button1_click事件處理器。同理,使用同樣的方法可以為任何控件編寫事件處理器。
protected void button1_click (object sender, system.eventargs e)
{
openfiledialog fdlg = new openfiledialog();
fdlg.title = "c# corner open file dialog" ;
fdlg.initialdirectory = @"c:" ;
fdlg.filter = "all files (*.*)|*.*|all files (*.*)|*.*" ;
fdlg.filterindex = 2 ;
fdlg.restoredirectory = true ;
if(fdlg.showdialog() == dialogresult.ok)
{
textbox1.text = fdlg.filename ;
}
}
到此就完成了所有步驟,剩下的就是運(yùn)行這個(gè)程序。它實(shí)現(xiàn)了瀏覽一個(gè)文件,然后將選擇的文件名裝進(jìn)文本框的功能。請(qǐng)下載相關(guān)代碼:winformapp.zip 。

商業(yè)源碼熱門下載www.html.org.cn

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 衡山县| 台湾省| 明水县| 凤翔县| 浮山县| 温州市| 中山市| 徐州市| 比如县| 芦溪县| 增城市| 晋宁县| 卓尼县| 吴桥县| 皋兰县| 揭东县| 邵阳县| 新蔡县| 兴国县| 和田县| 新干县| 泸水县| 沧州市| 英山县| 方正县| 白玉县| 新安县| 兰州市| 武山县| 崇仁县| 天柱县| 攀枝花市| 鄱阳县| 海门市| 潮安县| 东宁县| 巴塘县| 镇康县| 库尔勒市| 桑植县| 桑植县|