一、窗體設計
Winform是基於Windows平台下的應用程序開發框架,是一種基於.NET的GUI開發技術。在Winform開發中,窗體是最基礎的UI界面,是整個應用程序的視覺前端。因此,窗體設計是整個應用的起點。以下是Windows Forms中窗體設計的一些基本概念。
1、窗體設計器
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
2、窗體控制項
//創建窗體控制項,如Label、TextBox等 public Label label1 = new Label(); //將控制項添加到窗體里 this.Controls.Add(label1);
3、窗體事件
//按鈕事件處理函數
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "Hello, world!";
}
二、控制項應用
控制項是Winform應用的重要組成部分,可以使應用程序的功能更加完善。以下是Winform中常用控制項的功能及使用方法。
1、Label控制項
//創建Label控制項 Label label1 = new Label(); //設置Label的屬性 label1.Text = "Please enter your name:"; label1.Location = new Point(10, 10); label1.Size = new Size(150, 20); //將Label添加到窗體中 this.Controls.Add(label1);
2、TextBox控制項
//創建TextBox控制項 TextBox textBox1 = new TextBox(); //設置TextBox的屬性 textBox1.Location = new Point(170, 10); textBox1.Size = new Size(100, 20); //將TextBox添加到窗體中 this.Controls.Add(textBox1);
3、Button控制項
//創建Button控制項 Button button1 = new Button(); //設置Button的屬性 button1.Text = "OK"; button1.Location = new Point(10, 40); button1.Size = new Size(70, 30); //將Button添加到窗體中 this.Controls.Add(button1); //為Button添加事件處理函數 button1.Click += new EventHandler(button1_Click);
三、界面實現
Winform中的界面實現是指通過控制項和代碼的結合來實現應用程序的各種功能。以下是Winform界面實現的一些例子。
1、計算器
//創建Button控制項,代表「+」、「-」、「*」、「/」四個運算符
Button addButton = new Button();
addButton.Text = "+";
addButton.Location = new Point(250, 50);
addButton.Size = new Size(40, 30);
this.Controls.Add(addButton);
addButton.Click += new EventHandler(addButton_Click);
//創建TextBox控制項,用於輸入數字
TextBox numBox1 = new TextBox();
numBox1.Location = new Point(50, 50);
numBox1.Size = new Size(150, 30);
this.Controls.Add(numBox1);
TextBox numBox2 = new TextBox();
numBox2.Location = new Point(50, 100);
numBox2.Size = new Size(150, 30);
this.Controls.Add(numBox2);
//創建Label控制項,用於顯示計算結果
Label resultLabel = new Label();
resultLabel.Location = new Point(50, 150);
resultLabel.Size = new Size(150, 30);
this.Controls.Add(resultLabel);
//定義事件處理函數,實現計算功能
private void addButton_Click(object sender, EventArgs e)
{
double a = double.Parse(numBox1.Text);
double b = double.Parse(numBox2.Text);
resultLabel.Text = (a + b).ToString();
}
2、文件瀏覽器
//創建TreeView控制項,用於顯示目錄結構
TreeView treeView1 = new TreeView();
treeView1.Location = new Point(10, 10);
treeView1.Size = new Size(250, 400);
this.Controls.Add(treeView1);
//創建ListView控制項,用於顯示文件和文件夾
ListView listView1 = new ListView();
listView1.Location = new Point(270, 10);
listView1.Size = new Size(500, 400);
this.Controls.Add(listView1);
//定義事件處理函數,實現文件瀏覽功能
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
listView1.Clear();
string path = e.Node.FullPath;
string[] dirs = Directory.GetDirectories(path);
foreach (string dir in dirs)
{
DirectoryInfo info = new DirectoryInfo(dir);
ListViewItem item = new ListViewItem(info.Name);
item.SubItems.Add("文件夾");
listView1.Items.Add(item);
}
string[] files = Directory.GetFiles(path);
foreach (string file in files)
{
FileInfo info = new FileInfo(file);
ListViewItem item = new ListViewItem(info.Name);
item.SubItems.Add(info.Length + "B");
listView1.Items.Add(item);
}
}
以上是Winform應用開發教程中的一些基本知識點,通過這些知識點的學習,可以讓我們更好地掌握Winform應用開發技術。在實踐中,我們可以根據自己的需求進行更加靈活的應用程序開發。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/195730.html
微信掃一掃
支付寶掃一掃