Reputation: 9
I am trying to open a new form by clicking on a row in a ListView and pass the NoteId that is listed in the specific Row to the new Form, can anyone help please?
Sorry if this is a silly question, but I have only been programming since last month and my research proved fruitless :(
private void populatingMainList()
{
List<Note> getAllNotes = GetAllNotes();
lstMain.Items.Clear();
for (int i = 0; i < getAllNotes.Count; i++)
{
lstMain.FullRowSelect = true;
string _note;
ListViewItem lvi = new ListViewItem(_note = getAllNotes[i].NoteComplete.ToString());
if (_note == "True")
{
lvi.Text = "";
lvi.Checked = true;
}
else
{
lvi.Text = "";
lvi.Checked = false;
}
lvi.SubItems.Add(getAllNotes[i].NoteTitle);
lvi.SubItems.Add(getAllNotes[i].NoteDot.ToString("dd-MM-yyyy"));
lvi.SubItems.Add(getAllNotes[i].NoteNote);
lvi.SubItems.Add(getAllNotes[i].NoteId.ToString());
lstMain.Items.Add(lvi);
}
}
private void lstMain_SelectedIndexChanged_1(object sender, EventArgs e)
{
//I believe that some sort of code that retrieve NoteId from the specific Row must be added here.
if (_list == true)
{
frmSticky StickyForm = new frmSticky(_currentUser, _noteid);
}
}
Upvotes: 1
Views: 354
Reputation: 721
You can use a contextmenustrip for your listview and then add a button on it with function to open the form you are trying to.
1.Find ContextMenuStrip and add it to your application from toolbox.
2.Add it to your listview as shown in the image below.
3.Select the contextmenu you added and create a new button by clicking on "Type here".
4.Double click that button on your contextmenu and write the code you want to execute when you click on that button on contextmenu from listview.
Upvotes: 0
Reputation: 26
private void lstMain_SelectedIndexChanged_1(object sender, EventArgs e)
{
var lst = sender as ListView;
_noteid = lst.SelectedItems[0].SubItems[3];
if (_list == true)
{
frmSticky StickyForm = new frmSticky(_currentUser, _noteid);
}
}
Upvotes: 1