M
mycwcgr_new
Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何在一个控件的事件中调用另一个控件的事件?
我在控件listView1中增加了SelectedIndexChanged 事件
我想在控件button1的Click事件中调用listView1的SelectedIndexChanged 事件,请问如何做?
下面的语句虽然能执行,但是总是不对劲,有更好的方法吗?
private void button1_Click(object sender, System.EventArgs e)
{
listView1_SelectedIndexChanged(this,null);
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication4
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListView listView1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem("item1");
System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem("item2");
this.button1 = new System.Windows.Forms.Button();
this.listView1 = new System.Windows.Forms.ListView();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(96, 200);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// listView1
//
this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
listViewItem1,
listViewItem2});
this.listView1.Location = new System.Drawing.Point(48, 24);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(168, 152);
this.listView1.TabIndex = 1;
this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.listView1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (listView1.SelectedItems.Count>=1)
{
MessageBox.Show(listView1.SelectedItems[0].Text);
}
}
private void button1_Click(object sender, System.EventArgs e)
{
listView1_SelectedIndexChanged(this,null);
}
}
}
我在控件listView1中增加了SelectedIndexChanged 事件
我想在控件button1的Click事件中调用listView1的SelectedIndexChanged 事件,请问如何做?
下面的语句虽然能执行,但是总是不对劲,有更好的方法吗?
private void button1_Click(object sender, System.EventArgs e)
{
listView1_SelectedIndexChanged(this,null);
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication4
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListView listView1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem("item1");
System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem("item2");
this.button1 = new System.Windows.Forms.Button();
this.listView1 = new System.Windows.Forms.ListView();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(96, 200);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// listView1
//
this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
listViewItem1,
listViewItem2});
this.listView1.Location = new System.Drawing.Point(48, 24);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(168, 152);
this.listView1.TabIndex = 1;
this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.listView1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (listView1.SelectedItems.Count>=1)
{
MessageBox.Show(listView1.SelectedItems[0].Text);
}
}
private void button1_Click(object sender, System.EventArgs e)
{
listView1_SelectedIndexChanged(this,null);
}
}
}