找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 261|回复: 5

C# 索引器实战开发学习笔记

[复制链接]
发表于 2009-3-16 17:40:52 | 显示全部楼层 |阅读模式 IP:江苏扬州
 这是“使用 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 }
这个类的代码是非常简单的,我就不多作解释了。
发表于 2010-1-13 17:05:04 | 显示全部楼层 IP:香港
大Shi的话真如“大音希声扫阴翳”,犹如“拨开云雾见青天”,使我等网民看到了希望,看到了未来!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 13:18 , Processed in 0.205599 second(s), 15 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表