Accessing control on one form from another

M

markliam

I have a form with a listview and an edit button named Form1. When
the edit button is clicked, a dialog box containing some textboxes
should appear and the textboxes should be filled with data from the
currently selected listview item. The dialog form is called FormEdit.

How do I access the listview object on my main form the dialog form?

// The main form, in a file called Form1.cs... contains the listview
and the edit button

namespace WindowsApplication17
{
public partial class Form1 : Form
{

public Form1()
{
ListViewItem lvItem = new ListViewItem(textBoxLastName.Text, 0);
listView1.Items.Add(lvItem);
}


private void btnEditRecord_Click(object sender, EventArgs e)
{
FormEdit formEdit = new FormEdit();
formEdit.ShowDialog();
}
}

}

// The edit form, in a filed called Form2.cs

namespace WindowsApplication17
{
public partial class FormEdit : Form
{
public FormEdit()
{
InitializeComponent();
}

private void FormEdit_Load(object sender, EventArgs e)
{
// here is where I want to pull the data from the listview
on Form1 and populate the textboxes
// on this dialog with information from the main form
}
}
}

I can access the Form1 object by just typing it "Form1", but there is
no access to the listview (ie. Form1.listView)
 
P

Peter Duniho

I have a form with a listview and an edit button named Form1. When
the edit button is clicked, a dialog box containing some textboxes
should appear and the textboxes should be filled with data from the
currently selected listview item. The dialog form is called FormEdit.

How do I access the listview object on my main form the dialog form?

Here's the short version:
http://tinyurl.com/2lguxp

In case the long version gets broken by line-wrapping:
http://groups.google.com/groups/sea...ic.dotnet.languages.csharp&qt_s=Search+Groups
 
M

markliam

Here's the short version:http://tinyurl.com/2lguxp

In case the long version gets broken by line-wrapping:http://groups.google.com/groups/search?q=access+control+form+another+...

I'm probably doing it wrong, but here is what I tried:

On the main form where I call the dialog box, I changed:
FormEdit formEdit = new FormEdit();
to
FormEdit formEdit = new FormEdit(this);

And in the constructor of the dialog box I changed:
public FormEdit()
{
InitializeComponent();
}

to

public FormEdit(Form parent)
{
InitializeComponent()
}

but I still can't seem to access parent.listView1, even after making
it public on the main form

What am I doing wrong here?
 
M

markliam

I'm probably doing it wrong, but here is what I tried:

On the main form where I call the dialog box, I changed:
FormEdit formEdit = new FormEdit();
to
FormEdit formEdit = new FormEdit(this);

And in the constructor of the dialog box I changed:
public FormEdit()
{
InitializeComponent();

}

to

public FormEdit(Form parent)
{
InitializeComponent()

}

but I still can't seem to access parent.listView1, even after making
it public on the main form

What am I doing wrong here?

Doh, I was passing a new Form rather than my own Form1 object. I've
got it now, thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top