找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 2853|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

用C語言的風(fēng)格寫一個(gè)C#控件使用例子

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:32490 發(fā)表于 2019-3-15 23:28 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式

有同事看我打開了VS,說能不能幫忙寫個(gè)例子,他也要學(xué),入門大家都知道是簡單的,語言都是相通的嘛。

好了,下面簡單說下:
using System;
using System.IO;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;//引用WINDOWS FRAMWORK 庫文件
這些都是系統(tǒng)自帶的庫,好比C語言自帶的庫。
也可以u(píng)sing其他外來的庫,好比C語言的.lib或.a之類。

然后C#中的枚舉:
    //枚舉實(shí)列,
    public enum BtnNameType
    {
        按鈕A,//
        按鈕B,
        按鈕C,
    }
這個(gè)和C語言的也很類似,今天寫的例子有一個(gè)點(diǎn)就是程序可擴(kuò)展性,就是以這個(gè)枚舉來擴(kuò)展舉例
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
  }
這個(gè)是窗體的類,繼承于FORM基類,是剛才System.Windows.Forms中的類。
InitializeComponent();是窗體的初始化,有興趣也可以去研究下,比較簡單。

private void checkBox1_CheckedChanged(object sender, EventArgs e)

還有

private void button2_Click(object sender, EventArgs e)

等等都是事件處理,好比C語言的事件或消息或郵箱或者變量標(biāo)志

        private void button3_Click(object sender, EventArgs e)
        {
            int cCurptr = 0;
            for (cCurptr = 0; cCurptr < this.comboBox1.Items.Count; cCurptr++)
            {
                TreeNode cTreeNode = new TreeNode(this.comboBox1.Items[cCurptr].ToString());
                this.treeView1.Nodes.Add(cTreeNode);
            }
            this.treeView1.Enabled = true;
            this.button1.Enabled = true;
        }
如上,C#里面也是有FOR循環(huán),基本上和C一樣呢
        private void BtnNameList_BtnClickEvent(object sender, System.EventArgs e)
        {
            int cIndex=(e as BtnList.BtnClickEventArgs).GetIndex;
            switch ((BtnNameType)cIndex)
            {
                case BtnNameType.按鈕A:
                    {
                        MessageBox.Show("我是按鈕A");
                    }break;
                case BtnNameType.按鈕B:
                    {
                        MessageBox.Show("我是按鈕B");
                    } break;
                case BtnNameType.按鈕C:
                    {
                        MessageBox.Show("我是按鈕C");
                    } break;
            }
        }
這個(gè)就是按鈕點(diǎn)擊事件的集中處理了,相當(dāng)于有了一個(gè)框架,如果要新增一個(gè)按鈕以及這個(gè)按鈕的點(diǎn)擊處理,那么
就只要兩個(gè)地方更改,一個(gè)是剛才的枚舉多加一個(gè),另外就這個(gè)地方加個(gè)CASE處理
    public enum BtnNameType
    {
        按鈕A,//

        按鈕B,

        按鈕C,

        按鈕D,
    }

                case BtnNameType.按鈕C:
                    {
                        MessageBox.Show("我是按鈕C");
                    } break;
                case BtnNameType.按鈕D:
                    {
                        MessageBox.Show("我是按鈕D");
                    } break;


所以說,框架和編程的思想也是通用的,一個(gè)程序要能夠很容易的擴(kuò)展,降低維護(hù)成本
然后整體上就是一些常用控件的簡單使用,為了同事更好理解,特地按照顯示的箭頭操作,前一步操作后才能后一個(gè)使能操作
,這樣會(huì)比較好理解些。
具體的要看代碼,先貼上全部代碼來:
using System;
using System.IO;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;//引用WINDOWS FRAMWORK 庫文件
//引用其他外來庫
//本工程只演示最常用幾個(gè)系統(tǒng)組件的用法,深入了解每個(gè)組件的用法請(qǐng)閱讀官網(wǎng)MDSN類庫說明
namespace WindowsFormsApplication1
{
    //枚舉實(shí)列,
    public enum BtnNameType
    {
        按鈕A,//
        按鈕B,
        按鈕C,
    }
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            this.button5.Enabled = this.checkBox1.Checked;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            int cCurptr = 0;
            this.comboBox1.Enabled = true;
            char[] cText = this.textBox1.Text.ToArray();
            for (cCurptr = 0; cCurptr < cText.Length; cCurptr++)
                this.comboBox1.Items.Add(cText[cCurptr]);
            this.comboBox1.SelectedIndex = 0;
            this.button3.Enabled = true;
        }
        private void button3_Click(object sender, EventArgs e)
        {
            int cCurptr = 0;
            for (cCurptr = 0; cCurptr < this.comboBox1.Items.Count; cCurptr++)
            {
                TreeNode cTreeNode = new TreeNode(this.comboBox1.Items[cCurptr].ToString());
                this.treeView1.Nodes.Add(cTreeNode);
            }
            this.treeView1.Enabled = true;
            this.button1.Enabled = true;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.buttonList1.BtnClickEvent += new BtnList.ButtonList.BtnClickEventHandler(BtnNameList_BtnClickEvent);
            this.buttonList1.GcBtnTxt = Enum.GetNames(typeof(BtnNameType));
            this.buttonList1.BackColor = Color.BlueViolet;
        }
        private void BtnNameList_BtnClickEvent(object sender, System.EventArgs e)
        {
            int cIndex=(e as BtnList.BtnClickEventArgs).GetIndex;
            switch ((BtnNameType)cIndex)
            {
                case BtnNameType.按鈕A:
                    {
                        MessageBox.Show("我是按鈕A");
                    }break;
                case BtnNameType.按鈕B:
                    {
                        MessageBox.Show("我是按鈕B");
                    } break;
                case BtnNameType.按鈕C:
                    {
                        MessageBox.Show("我是按鈕C");
                    } break;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int cCurptr = 0;
            for (cCurptr = 0; cCurptr < this.treeView1.Nodes.Count; cCurptr++)
            {
                this.textBox2.Text += this.treeView1.Nodes[cCurptr].Text;
            }
            this.textBox2.Enabled = true;
            this.button4.Enabled = true;
        }
        private void button4_Click(object sender, EventArgs e)
        {
            if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fileName = this.saveFileDialog1.FileName;
                Stream stream = File.OpenWrite(fileName);
                StreamWriter writer = new StreamWriter(stream);
                writer.Write(this.textBox2.Text);
                this.button1.Enabled = false;
                this.button2.Enabled = false;
                this.button3.Enabled = false;
                this.button4.Enabled = false;
                this.textBox1.Enabled = false;
                this.textBox2.Enabled = false;
                this.comboBox1.Enabled = false;
                this.treeView1.Enabled = false;
            }
        }
        private void button5_Click(object sender, EventArgs e)
        {
            this.textBox1.Enabled = true;
            this.openFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
            this.openFileDialog1.Filter = "文本文件 | *.txt";
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fileName = this.openFileDialog1.FileName;
                if (fileName!="")
                {
                    string cText;
                    try
                    {
                        StreamReader sr = new StreamReader(fileName);
                        while ((cText = sr.ReadLine()) != null)
                        {
                            this.textBox1.Text += cText;
                        }
                    }
                    catch { }
                    this.button2.Enabled = true;
                }
            }
        }
    }
}





WindowsFormsApplication1.7z

51.11 KB, 下載次數(shù): 10, 下載積分: 黑幣 -5

評(píng)分

參與人數(shù) 1黑幣 +100 收起 理由
admin + 100 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表