可以参看类System.Windows.Forms.ListControl的用法示例

示例1: ListBox

主要是使用到了 ListBox1_SelectedValueChanged 事件

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//引入命名空间
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;

namespace MyListControlSample
{
public class ListBoxSample3 : Form
{
private ListBox ListBox1 = new ListBox();
private Label label1 = new Label();
private TextBox textBox1 = new TextBox();

[STAThread]
static void Main()
{
Application.Run(new ListBoxSample3());
}

public ListBoxSample3()
{
this.ClientSize = new Size(307, 206);
this.Text = "ListBox Sample3";

ListBox1.Location = new Point(54, 16);
ListBox1.Name = "ListBox1";
ListBox1.Size = new Size(240, 130);

label1.Location = new Point(14, 150);
label1.Name = "label1";
label1.Size = new Size(40, 24);
label1.Text = "Value";

textBox1.Location = new Point(54, 150);
textBox1.Name = "textBox1";
textBox1.Size = new Size(240, 24);

this.Controls.AddRange(new Control[] { ListBox1, label1, textBox1 });

// Populate the list box using an array as DataSource.
ArrayList USStates = new ArrayList();
USStates.Add(new USState("Alabama", "AL"));
USStates.Add(new USState("Washington", "WA"));
USStates.Add(new USState("West Virginia", "WV"));
USStates.Add(new USState("Wisconsin", "WI"));
USStates.Add(new USState("Wyoming", "WY"));
ListBox1.DataSource = USStates;

// Set the long name as the property to be displayed and the short
// name as the value to be returned when a row is selected. Here
// these are properties; if we were binding to a database table or
// query these could be column names.
ListBox1.DisplayMember = "LongName";
ListBox1.ValueMember = "ShortName";

// Bind the SelectedValueChanged event to our handler for it.
ListBox1.SelectedValueChanged +=
new EventHandler(ListBox1_SelectedValueChanged);

// Ensure the form opens with no rows selected.
ListBox1.ClearSelected();
}

private void InitializeComponent()
{
}

private void ListBox1_SelectedValueChanged(object sender, EventArgs e)
{
if (ListBox1.SelectedIndex != -1)
{
textBox1.Text = ListBox1.SelectedValue.ToString();
// If we also wanted to get the displayed text we could use
// the SelectedItem item property:
// string s = ((USState)ListBox1.SelectedItem).LongName;
}
}
}

public class USState
{
private string myShortName;
private string myLongName;

public USState(string strLongName, string strShortName)
{

this.myShortName = strShortName;
this.myLongName = strLongName;
}

public string ShortName
{
get
{
return myShortName;
}
}

public string LongName
{

get
{
return myLongName;
}
}
}
}

示例1: ListBoxSample3

ListControl类属于System.Windows.Forms命名空间,在下文中一共展示了ListControl类的1个代码示例

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//引入命名空间
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;

namespace MyListControlSample
{
public class ListBoxSample3 : Form
{
private ListBox ListBox1 = new ListBox();
private Label label1 = new Label();
private TextBox textBox1 = new TextBox();

[STAThread]
static void Main()
{
Application.Run(new ListBoxSample3());
}

public ListBoxSample3()
{
this.ClientSize = new Size(307, 206);
this.Text = "ListBox Sample3";

ListBox1.Location = new Point(54, 16);
ListBox1.Name = "ListBox1";
ListBox1.Size = new Size(240, 130);

label1.Location = new Point(14, 150);
label1.Name = "label1";
label1.Size = new Size(40, 24);
label1.Text = "Value";

textBox1.Location = new Point(54, 150);
textBox1.Name = "textBox1";
textBox1.Size = new Size(240, 24);

this.Controls.AddRange(new Control[] { ListBox1, label1, textBox1 });

// Populate the list box using an array as DataSource.
ArrayList USStates = new ArrayList();
USStates.Add(new USState("Alabama", "AL"));
USStates.Add(new USState("Washington", "WA"));
USStates.Add(new USState("West Virginia", "WV"));
USStates.Add(new USState("Wisconsin", "WI"));
USStates.Add(new USState("Wyoming", "WY"));
ListBox1.DataSource = USStates;

// Set the long name as the property to be displayed and the short
// name as the value to be returned when a row is selected. Here
// these are properties; if we were binding to a database table or
// query these could be column names.
ListBox1.DisplayMember = "LongName";
ListBox1.ValueMember = "ShortName";

// Bind the SelectedValueChanged event to our handler for it.
ListBox1.SelectedValueChanged +=
new EventHandler(ListBox1_SelectedValueChanged);

// Ensure the form opens with no rows selected.
ListBox1.ClearSelected();
}

private void InitializeComponent()
{
}

private void ListBox1_SelectedValueChanged(object sender, EventArgs e)
{
if (ListBox1.SelectedIndex != -1)
{
textBox1.Text = ListBox1.SelectedValue.ToString();
// If we also wanted to get the displayed text we could use
// the SelectedItem item property:
// string s = ((USState)ListBox1.SelectedItem).LongName;
}
}
}

public class USState
{
private string myShortName;
private string myLongName;

public USState(string strLongName, string strShortName)
{

this.myShortName = strShortName;
this.myLongName = strLongName;
}

public string ShortName
{
get
{
return myShortName;
}
}

public string LongName
{

get
{
return myLongName;
}
}
}
}

相关链接(侵删)

  1. C# ListControl.SelectedValueChanged事件代码示例
  2. C# ListControl类代码示例

=================我是分割线=================

欢迎到公众号来唠嗑: