首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 仿 QQ 面板簡單實作 (2) — 完整雙層選單邏輯與子選單動態載入

     編輯:關於C語言

核心資料結構


string[] Module;           // 第一層選單
string[,] ChildModule;     // 第二層選單 [模組索引, 子項索引]
int _ModuleButtonHeight = 50;
int _ChildButtonHeight = 30;

Form_Load:建立第一層按鈕


private void Form1_Load(object sender, EventArgs e) {
    panel1.BorderStyle = BorderStyle.FixedSingle;
    Module = new[] { "模組1", "模組2", "模組3" };
    ChildModule = new string[3, 2];
    ChildModule[0, 0] = "子項1"; ChildModule[0, 1] = "子項2";
    ChildModule[1, 0] = "子項3"; ChildModule[1, 1] = "子項4";
    ChildModule[2, 0] = "子項5"; ChildModule[2, 1] = "";

    for (int i = 0; i < Module.Length; i++) {
        Button btn = new Button {
            FlatStyle = FlatStyle.Flat,
            Width = panel1.Width,
            Height = _ModuleButtonHeight,
            Name = "Button" + i,
            Text = Module[i],
            Top = _ModuleButtonHeight * i
        };
        btn.Click += btn_Click;
        panel1.Controls.Add(btn);
    }
}

btn_Click:展開/收合子選單


private void btn_Click(object sender, EventArgs e) {
    bool found = false;
    // 移除舊子面板
    for (int i = 0; i < panel1.Controls.Count; i++) {
        if (panel1.Controls[i] is Panel) panel1.Controls.RemoveAt(i--);
    }
    // 重新定位第一層按鈕
    for (int i = 0; i < panel1.Controls.Count; i++) {
        if (panel1.Controls[i] is Button btn) {
            btn.Top = found
                ? panel1.Height - _ModuleButtonHeight * (Module.Length - i)
                : _ModuleButtonHeight * i;
            if (btn.Name == ((Button)sender).Name) {
                found = true;
                Panel p = new Panel {
                    BackColor = Color.AliceBlue,
                    Top = _ModuleButtonHeight * (i + 1),
                    Width = panel1.Width,
                    Height = panel1.Height - _ModuleButtonHeight * Module.Length
                };
                panel1.Controls.Add(p);
                // 動態建立子按鈕
                for (int j = 0; j < ChildModule.GetLength(1); j++) {
                    if (!string.IsNullOrEmpty(ChildModule[i, j])) {
                        Button cbtn = new Button {
                            FlatStyle = FlatStyle.Flat,
                            Top = _ChildButtonHeight * j,
                            Width = panel1.Width,
                            Height = _ChildButtonHeight,
                            Name = "ChildButton" + i + "_" + j,
                            Text = ChildModule[i, j]
                        };
                        cbtn.Click += btnChild_Click;
                        p.Controls.Add(cbtn);
                    }
                }
            }
        }
    }
}

子按鈕點擊事件


private void btnChild_Click(object sender, EventArgs e) {
    MessageBox.Show("你點擊了 \"" + ((Button)sender).Name + "\" 按鈕!", 
        "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

完整原始碼下載

https://files.cnblogs.com/KenBlove/QQPanel.7z

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved