1

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

Main Form:

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

Second Form:

private void listView1_ItemActivate(object sender, EventArgs e)
{
    MainForm mf = new MainForm();
    mf.label1.Text = e.ToString();
    Close();
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
Sean Kelly
  • 183
  • 3
  • 14
  • Did you notice you made a *new* MainForm? That's not the one you have on the screen now. – LarsTech Dec 06 '16 at 23:58
  • How can I reference my main form? When i try, i am unable to. Please see my new edit – Sean Kelly Dec 07 '16 at 00:05
  • Probably make an event on the second form that your main form listens to, and in that lisView1_ItemActivate method you have, you raise your custom event, pass the value. – LarsTech Dec 07 '16 at 00:12
  • Check this http://stackoverflow.com/questions/20505445/accessing-main-form-objects-in-other-cs-files – Seminda Dec 07 '16 at 00:19
  • Thanks for the reply seminda, but I really don't understand what I'm reading. I guess I am more novice than I thought! – Sean Kelly Dec 07 '16 at 00:45

1 Answers1

0

Here's what you should do! On your second form, do this

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

And...

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

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!

  • This worked! I'm not 100% sure why this works, as I am coming from Python and Java, however I feel like I am beginning to grasp it. C-land is a crazy place. Thanks Landon! You're the real MVP. – Sean Kelly Dec 07 '16 at 03:19
  • No problem! This is just my usual way of doing things! And I thought I would pass it on to more people like you who are having trouble with it! I never create a form without doing this unless there is a specific reason! – Landon Conway Dec 07 '16 at 03:50