I'm currently working on an application that shows whether colleagues are present at work. To make it a bit 'easy' to use for the one who's going to manage it, I want the user to be able to double click a listview item of a user, that prompts a window where the status of that person can be changed.
It was fairly easy to prompt a messagebox with the abbreviation of the selected person (which acts as a primary key for the database used). However, I want this abbreviation to be passed over to the textbox in that other window.
This is what I use to launch the window to change the employee status:
frmStatusEdit SE = new frmStatusEdit();
SE.Show();
Keep in mind tho: this code works fine; the window launches without any issue. However, upon doube clicking a user, the window launches without the textbox containing its abbreviation.
For some reason I can't get this to work, despite using this line, which worked succesfully with the messagebox:
MessageBox.Show(lvEmployees1.SelectedItems[0].Text);
When I use this (with SE referring to the other window), nothing happens:
if (lvEmployees1.SelectedItems.Count > 0)
{
string listItem = lvEmployees1.SelectedItems[0].Text;
SE.tbxAbbrevEmployee.Text = listItem;
}
I've been searching for a solution and came across several, but none seem to fix the issue. No error message is shown either, making it even more difficult to find out what I do wrong.
Anyone who has an idea what I might be doing wrong? I'm not very experienced with coding, so it's easy to forget things in my case.
CodePudding user response:
Try this, take the code from the button click to the list view double click:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var form2 = new Form2
{
Message = "Hello World"
};
form2.ShowDialog();
}
}
public partial class Form2 : Form
{
public string Message { get; set; } = "";
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = Message;
}
}
Another option for Form2:
public partial class Form2 : Form
{
private string _message = "";
public string Message
{
get { return _message; }
set
{
_message = value ?? "";
textBox1.Text = _message;
}
}
public Form2()
{
InitializeComponent();
}
}