Select in listview, to pass the index through to another form
public static int iDeliverySelected = -1;
public static ArrayList myDeliveries = new ArrayList();
I specify these two values at the top of the page.
The listview is populated as follows:
lstDeliveryDetails.Items.Clear();
foreach (Delivery d in mainForm.myDeliveries)
{
ListViewItem item = lstDeliveryDetails.Items.Add(d.DeliveryName);
item.SubItems.Add(d.DeliveryA开发者_JAVA百科ddress);
item.SubItems.Add(d.DeliveryDay);
item.SubItems.Add(d.DeliveryTime);
item.SubItems.Add(d.DeliveryMeal);
item.SubItems.Add(d.DeliveryInstructions);
item.SubItems.Add(d.DeliveryStatus);
}
And then when I want to select one of these, I am getting stuck. My idea is that I want to select a value and then click an "Edit" button which will take me through to the saveForm and allow me to edit all the values that correspond to the selected index.
I have tried the following:
iDeliverySelected = lstDeliveryDetails.SelectedIndicies;
iDeliverySelected = lstDeliveryDetails.SelectedIndex;
They both throw up the following error:
'System.Windows.Forms.ListView' does not contain a definition for 'SelectedIndicies' and no extension method 'SelectedIndicies' accepting a first argument of type 'System.Windows.Forms.ListView' could be found (are you missing a using directive or an assembly reference?)
I am literally stuck and have no idea what to do next! I would really appreciate it if someone could help!
One way is to pass thru the second form's custom constructor.
public Form2()
{
}
int index;
public Form2(int index)
{
this.index = index;
}
// Code in Main Form
Form2 f2 = new Form2(this.listView1.SelectedIndex);
f2.ShowDialog();
In your code in the question after the line
ListViewItem item = lstDeliveryDetails.Items.Add(d.DeliveryName);
Add the line
item.tag = d;
Then make your button handler be something like:
private void btnEdit_Click(System.Object sender, System.EventArgs e)
{
if (ListView1.SelectedItems.Count > 0)
{
saveForm frm = new saveForm((Delivery)ListView1.SelectedItems(0).Tag);
frm.Show();
}
}
Make the saveForm contain something like:
public class saveForm
{
private Delivery m_delivery;
public saveForm(Delivery d)
{
InitializeComponent();
m_delivery = d;
}
}
精彩评论