六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

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

[摘要]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中選擇“新建->工程文...

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)的名稱空間以及對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方法,就會發(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)文本框的功能。請下載相關(guān)代碼:winFormApp.zip 。




相關(guān)文章

主站蜘蛛池模板: 探花 在线 | 手机看片久久国产免费不卡 | 色网站网址 | 三级国产4国语三级在线 | 日本高清视频一区二区三区 | 青青青国产精品视频 | 永久视频免费 | 中文字幕波多野不卡一区 | 青娱乐免费视频在线观看 | 日韩欧美一及在线播放 | 啪啪午夜| 伊人久久网站 | 天堂美女 | 深夜福利成人 | 欧美一级淫片免费观看 | 欧美一级片免费观看 | 欧洲一区麻豆文化传媒 | 在线bt天堂网www在线下载 | 日韩欧美h | 五月天婷婷在线视频 | 青娱乐欧美视频 | 亚洲xxxxx | 色橹橹高清视频在线播放 | 亚洲大片 | 四虎影院新网址 | 最近韩国高清免费 hd | 亚洲成a人片 | 天天干网站 | 欧美在线a级高清 | 日本一区高清 | 亚洲第一影视 | 午夜a级片 | 日本不卡免费新一区二区三区 | 日本成人免费在线观看 | 色综网| 视频在线亚洲 | 日本特黄特色免费大片 | 奇米网久久 | 野外三级国产在线观看 | 色噜噜噜噜亚洲第一 | 天堂网在线播放 |