This is the first article in the series "use C # To develop the smartphone software: Push box. This article introduces the Windows/ConfigDlg. cs source program file. This source file contains the ConfigDlg class, which inherits from the System. Windows. Forms. Form class and indicates the "configuration" dialog box for pushing the box. As shown in:
The following is part of the source code of Window/ConfigDlg. Designer. cs:
Namespace Skyiv. Ben. PushBox. Window
{
Partial class ConfigDlg
{
Private void InitializeComponent ()
{
// Note: some code is omitted.
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;
}
}
The following is the source code of ConfigDlg. cs:
1 using System;
2 using System. Windows. Forms;
3
4 namespace Skyiv. Ben. PushBox. Window
5 {
6 ///
7 // "configuration" dialog box
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}