当前位置导航:炫浪网>>网络学院>>编程开发>>Visual C#教程

C#开发智能手机游戏推箱子软件

  这是“使用 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 ///
7 /// “配置”对话框
8 ///
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 == 0) return;
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 < 0) return;
55 lbxGroup.Items.RemoveAt(idx);
56 if (lbxGroup.Items.Count <= 0) return;
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 < 1) return;
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 - 1) return;
73 lbxGroup.Items.Insert(idx + 2, lbxGroup.SelectedItem);
74 lbxGroup.Items.RemoveAt(idx);
75 lbxGroup.SelectedIndex = idx + 1;
76 }
77 }
78 }
这个类的代码是非常简单的,我就不多作解释了。

相关内容
赞助商链接