第六、七章只是略读(标题分别为 Advanced Drawing、Pringint),暂无笔记。
1。不同的 control 处理相同的事件
可能会有这样的情况:用户点一个按钮和一个菜单项完成相同的作用(比如都是打开一个文件),这时如果分别写按钮和菜单项的事件处理代码(两端代码完全一样),就会出现重复,而且改动的时候也不得不改动多处。
解决方法:把事件处理代码写成一个单独的方法,比如 OpenFile(),然后在事件处理方法中调用这个独立的方法。
2。ListBox
如何把自定义的类显示在一个 ListBox 中?
答案:override 类的 ToString() 方法(这个方法在 Object 中就存在),然后 ListBox 类的 Add() 方法会自动调用类的 ToString()~~~~

class
Person
{
string name;
int age;


public Person(string name, int age)
{
this.name = name;
this.age = age;
}


public override string ToString()
{
return string.Format("{0} is {1} years old", Name, Age);
}
}


void
Form1_Load(
object
sender, EventArgs e)
{

Person[] boys =
{ new Person("Tom", 7), new Person("John", 8) };

foreach( Person boy in boys )
{
listBox1.Items.Add(boy);
}
}
3。ListView 和 TreeView
//
-------ListView

void
Form1_Load(
object
sender, EventArgs e)
{

Person[] boys =
{ new Person("Tom", 7), new Person("John", 8) };

foreach( Person boy in boys )
{
// 假设listView1已经存在两列
ListViewItem item = new ListViewItem();
item.Text = boy.Name;
item.SubItems.Add(boy.Age.ToString());
listView1.Items.Add(item);
}
}
//
---------TreeView

void
Form1_Load(
object
sender, EventArgs e)
{
TreeNode parentNode = new TreeNode();
parentNode.Text = "Chris";

// 先加根节点
treeView1.Nodes.Add(parentNode);

TreeNode childNode = new TreeNode();
childNode.Text = "John";

// 加一个子节点
parentNode.Nodes.Add(childNode);
}
4。DataGrid
只要设置一下 DataSource 属性就可以了~~

void
Form1_Load(
object
sender, EventArgs e)
{

Person[] boys =
{ new Person("Tom", 7), new Person("John", 8) };
dataGrid1.DataSource = boys;
}
5。List Item Selection
//
----------ListBox

void
listBox1_SelectedIndexChanged(
object
sender, EventArgs e)
{
//得到选中的
object selection = listBox1.Items[listBox1.SelectedIndex];
MessageBox.Show(selection.ToString());

// 类型转化为原来的类型
Person boy = (Person)selection;
MessageBox.Show(boy.ToString());
}
//
----------TreeView

void
Form1_Load(
object
sender, EventArgs e)
{
TreeNode parentNode = new TreeNode();
parentNode.Text = "Chris";
parentNode.Tag = "000-00-0000"; // 用Tag保存额外信息
treeView1.Nodes.Add(parentNode);
}


void
treeView1_AfterSelect(
object
sender, TreeViewEventArgs e)
{
TreeNode selection = treeView1.SelectedNode;
object tag = selection.Tag; // 从Tag取出俄外信息
MessageBox.Show(tag.ToString());
}
注意:
List controls support either custom types or the Tag property but not both. However, a simple wrapper will allow you to add a tag to a list item of any type:
//
----------让任意类都可以有Tag

class
TaggedItem
{
public object Item;
public object Tag;


public TaggedItem(object item, object tag)
{
this.Item = item;
this.Tag = tag;
}


public override string ToString()
{
return Item.ToString();
}
}
//
----------------

void
Form1_Load(
object
sender, EventArgs e)
{
// Add two tagged strings
comboBox1.Items.Add(new TaggedItem("Tom", "000-00-0000));
comboBox1.Items.Add(new TaggedItem("John", "000-00-0000"));
}
//
-----------------

void
comboBox1_SelectedIndexChanged(
object
sender, EventArgs e)
{
TaggedItem selection =
(TaggedItem)comboBox1.Items[comboBox1.SelectedIndex];
object tag = selection.Tag;
MessageBox.Show(tag.ToString());
}