通过ListView ItemActivate更改标签文本 [英] Change Label Text via ListView ItemActivate
问题描述
我有2种形式:主表单和仅具有列表视图的第二种表单,用户可以在其中进行选择.双击激活listview项后,我希望主窗体上的标签显示已激活的项的文本.这是我的代码(不起作用);为什么这是错误的?谢谢
I have 2 forms: a main form and a second form with only a listview in which users can make a selection. Once the listview item is activated with a double click, I want a label on the main form to display the text of the item that was activated. Here is my code (not working); why is this wrong? Thanks
主要形式:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
/* for populating the process list when the user clicks display process button */
private void DisplayProcessButton_Click(object sender, EventArgs e)
{
Process_List plopen = new Process_List();
plopen.Show();
Process[] process = Process.GetProcesses();
foreach (Process prs in process)
{
plopen.listView1.Items.Add(prs.ProcessName);
}
}
第二种形式:
private void listView1_ItemActivate(object sender, EventArgs e)
{
MainForm mf = new MainForm();
mf.label1.Text = e.ToString();
Close();
}
推荐答案
这就是您应该做的!在第二张表格上,执行此操作
Here's what you should do! On your second form, do this
public MainForm parentForm;
public void SecondForm(MainForm form)
{
InitializeComponent();
parentForm = form;
}
然后...
private void listView1_ItemActivate(object sender, EventArgs e)
{
parentForm.label1.Text = e.ToString();
}
然后以您的主要形式...
Then in your main form...
public SecondForm secondform;
public void MainForm()
{
InitializeComponent();
secondform = new SecondForm(this);
}
当您打开SecondForm时,可以在任何需要的地方使用它!
And when you open your SecondForm use this wherever you want!
secondform.Show();
这样做,您可以将信息表单传递给表单.我一直在使用我的每一种表格.这非常有用!如果您有任何疑问,请告诉我!
By doing this, you can transfer information form form to form back and forth. I use this all the time with every one of my forms. It's very very useful! If you have any questions please let me know!
这篇关于通过ListView ItemActivate更改标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!