Sadly I cannot include images since I am new...So please work with me here.
This is the code I am trying to use, to add an item from another form's 'textbox.Text' into my main form's listview.
public partial class AddingClient : Form //(Form 2)
{
private Form1 UseForm1; // The Object to access Form1's listview
public AddingClient()
{
UseForm1 = new Form1();
InitializeComponent();
}
public void Accept_Click(object sender, EventArgs e) // When all textbox's are filled press accept to add to listview.
{
try
{
// Check to see if there are any blank texts, we can't be having those.
if (name.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Name");
if (phoneN.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Phone Number");
if (address.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Address");
if (email.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Email");
// Use the info given via textbox's and add them to items/subitems
ListViewItem lvi = new ListViewItem(name.Text);
lvi.SubItems.Add(phoneN.Text);
lvi.SubItems.Add(address.Text);
lvi.SubItems.Add(email.Text);
// Add the items to the list view.
UseForm1.listView1.Items.Add(lvi); // This is the code I believe to be the problem but found no solutions.
// If no error, success.
MessageBox.Show("Successfully Added Client", "Success");
Close();
}
catch(Exception ex)
{
//If error show the error
MessageBox.Show(ex.Message,"Error");
}
}
Here is the problem : There are no errors, and no exceptions are thrown, However nothing is added into the list view. When debugging the code, it shows that the information was stored but not passed into the listview... Why? (Thanks in advance)
This occurs because you are creating a new instance of Form1 in the constructor of AddingClient.
Assuming you had Form1 initially loaded as the main form and you clicked a button to open up AddingClient. Now in AddingClient you are creating a new instance of Form1. You now have two instances of Form1 and you are inserting into the second instance of Form1 which is not visible on screen.
What you need to do is either, pass the Form1 instance to AddingClient through a function, which can be achieved by passing this or you retrieve the values from AddingClient in your Form1 main instance, once you close the AddingClient form.
public partial class AddingClient : Form //(Form 2)
{
private Form1 UseForm1; // The Object to access Form1's listview
public AddingClient()
{
InitializeComponent();
}
public void setForm1(Form1 form)
{
UseForm1 = form;
}
public void Accept_Click(object sender, EventArgs e) // When all textbox's are filled press accept to add to listview.
{
try
{
// Check to see if there are any blank texts, we can't be having those.
if (name.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Name");
if (phoneN.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Phone Number");
if (address.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Address");
if (email.Text == string.Empty)
throw new System.ArgumentException("You must fill all blanks.", "Email");
// Use the info given via textbox's and add them to items/subitems
ListViewItem lvi = new ListViewItem(name.Text);
lvi.SubItems.Add(phoneN.Text);
lvi.SubItems.Add(address.Text);
lvi.SubItems.Add(email.Text);
// Add the items to the list view.
UseForm1.listView1.Items.Add(lvi); // This is the code I believe to be the problem but found no solutions.
// If no error, success.
MessageBox.Show("Successfully Added Client", "Success");
Close();
}
catch(Exception ex)
{
//If error show the error
MessageBox.Show(ex.Message,"Error");
}
}
}
And in your main form, where you are calling the AddingClient form write this
AddingClient addingClientForm = new AddingClient();
addingClientForm.setForm1(this);
addingClientForm.ShowDialog(this);