通过ListView ItemActivate更改标签文本

时间:2016-12-06 23:54:59

标签: c# winforms listview label

我有两种形式:主表单和第二种表单,只有一个列表视图,用户可以在其中进行选择。通过双击激活listview项目后,我希望主窗体上的标签显示已激活项目的文本。这是我的代码(不工作);为什么这是错的?感谢

主要表格:

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();
}

1 个答案:

答案 0 :(得分:0)

这是你应该做的!在第二个表单上,执行此操作

public class Butter : Product { }
public class Egg : Product { }
public class Product { }

public T AddGeneric<T>(T item) where T : Product
{
    //Do something
    return item;
}

public Product Add(Product item)
{
    //Do something

    return item;
}

//Even though we know it's going to return an Egg, we still need to cast here
//If we want to treat t as an Egg, rather than as a Product
Product t = Add(new Egg());

//No cast needed
Egg tt = AddGeneric(new Egg());

和...

public MainForm parentForm;
public void SecondForm(MainForm form)
{
    InitializeComponent();
    parentForm = form;
}

然后在你的主要形式......

private void listView1_ItemActivate(object sender, EventArgs e)
{
    parentForm.label1.Text = e.ToString();
}

当你打开你的SecondForm时,你可以随意使用它!

public SecondForm secondform;
public void MainForm()
{
    InitializeComponent();
    secondform = new SecondForm(this);
}

通过这样做,您可以来回传输信息表单。我一直用我的每一个表格。这非常有用!如果您有任何疑问,请告诉我们!