银河

SKYIV STUDIO

  博客园 :: 首页 :: 博问 :: 闪存 :: :: :: 订阅 订阅 :: 管理 ::
  268 随笔 :: 2 文章 :: 2616 评论 :: 140万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
    这是“使用 C# 开发智能手机软件:推箱子” 系列文章的第十六篇。在这篇文章中,介绍 Window/ConfigDlg.cs 源程序文件。这个源程序文件包含 ConfigDlg 类,该类继承自 System.Windows.Forms.Form 类,表示推箱子的“配置”对话框。如下图所示:



    下面是 Window/ConfigDlg.Designer.cs 的源程序的部分代码:

namespace Skyiv.Ben.PushBox.Window
{
  partial 
class ConfigDlg
  {
    
private void InitializeComponent()
    {
        
// 注意:省略了一些代码
      
      
this.btnSave.DialogResult = System.Windows.Forms.DialogResult.OK;
      
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
      
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
      
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
      
this.btnUp.Click += new System.EventHandler(this.btnUp_Click);
      
this.btnDown.Click += new System.EventHandler(this.btnDown_Click);
    }

    
private System.Windows.Forms.ListBox lbxGroup;
    
private System.Windows.Forms.TextBox tbxGroup;
    
private System.Windows.Forms.Button btnSave;
    
private System.Windows.Forms.Button btnCancel;
    
private System.Windows.Forms.Button btnAdd;
    
private System.Windows.Forms.Button btnDelete;
    
private System.Windows.Forms.Button btnUp;
    
private System.Windows.Forms.Button btnDown;
  }
}

下面是 ConfigDlg.cs 的源程序代码:

 1 using System;
 2 using System.Windows.Forms;
 3 
 4 namespace Skyiv.Ben.PushBox.Window
 5 {
 6   /// <summary>
 7   /// “配置”对话框
 8   /// </summary>
 9   public partial class ConfigDlg : Form
10   {
11     public ConfigDlg(bool isTopMost)
12     {
13       InitializeComponent();
14       TopMost = isTopMost;
15     }
16 
17     public string[] Groups
18     {
19       get
20       {
21         string[] groups = new string[lbxGroup.Items.Count];
22         for (int i = 0; i < lbxGroup.Items.Count; i++) groups[i] = lbxGroup.Items[i].ToString();
23         return groups;
24       }
25       set
26       {
27         if (value != null)
28         {
29           lbxGroup.BeginUpdate();
30           foreach (string group in value) lbxGroup.Items.Add(group);
31           lbxGroup.EndUpdate();
32           if (lbxGroup.Items.Count > 0) lbxGroup.SelectedIndex = 0;
33         }
34       }
35     }
36 
37     private void btnAdd_Click(object sender, EventArgs e)
38     {
39       string s = tbxGroup.Text.Trim();
40       if (s.Length == 0return;
41       int idx = lbxGroup.SelectedIndex;
42       if (idx < 0)
43       {
44         lbxGroup.Items.Add(s);
45         idx = lbxGroup.Items.Count - 1;
46       }
47       else lbxGroup.Items.Insert(idx, s);
48       lbxGroup.SelectedIndex = idx;
49     }
50 
51     private void btnDelete_Click(object sender, EventArgs e)
52     {
53       int idx = lbxGroup.SelectedIndex;
54       if (idx < 0return;
55       lbxGroup.Items.RemoveAt(idx);
56       if (lbxGroup.Items.Count <= 0return;
57       lbxGroup.SelectedIndex = (idx < lbxGroup.Items.Count) ? idx : (idx - 1);
58     }
59 
60     private void btnUp_Click(object sender, EventArgs e)
61     {
62       int idx = lbxGroup.SelectedIndex;
63       if (idx < 1return;
64       lbxGroup.Items.Insert(idx - 1, lbxGroup.SelectedItem);
65       lbxGroup.Items.RemoveAt(idx + 1);
66       lbxGroup.SelectedIndex = idx - 1;
67     }
68 
69     private void btnDown_Click(object sender, EventArgs e)
70     {
71       int idx = lbxGroup.SelectedIndex;
72       if (idx < 0 || idx >= lbxGroup.Items.Count - 1return;
73       lbxGroup.Items.Insert(idx + 2, lbxGroup.SelectedItem);
74       lbxGroup.Items.RemoveAt(idx);
75       lbxGroup.SelectedIndex = idx + 1;
76     }
77   }
78 }

     这个类的代码是非常简单的,我就不多作解释了。她只是一个用户界面,实际工作在 Skyiv.Ben.PushBox.Common.ConfigFile 类中完成,请参见:“使用 C# 开发智能手机软件:推箱子(九)”。

上一篇:使用 C# 开发智能手机软件:推箱子(十五)
下一篇:使用 C# 开发智能手机软件:推箱子(十七)
返回目录
posted on   银河  阅读(2816)  评论(14编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2005-10-07 ACTIVE OBJECT 模式 C# 版
点击右上角即可分享
微信分享提示