0

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.

Tom
  • 23
  • 6
  • I have edited my main post. – Tom Jan 20 '22 at 09:34
  • please put the code in the correct order. At which point in time do you call `SE.Show()` and at which time do you initialize the textbox ? – Mong Zhu Jan 20 '22 at 09:35
  • The textbox is initalized at frmStatusEdit. I call the SE.Show() in another method, because launching the window itself happens on doubleclicking a listview item. Then, in a selectedindexchanged method, I attempt to pass over the text from that listview item. – Tom Jan 20 '22 at 09:40
  • Does this answer your question? [Communicate between two windows forms in C#](https://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp) – JohnG Jan 20 '22 at 09:40
  • It sounds like you need to pass that info to the `SE` form. Something like… `string listItem = lvEmployees1.SelectedItems[0].Text;` … then … `frmStatusEdit SE = new frmStatusEdit(listItem);`. Obviously you will need to create a constructor that accepts the passed in `listItem`. – JohnG Jan 20 '22 at 09:43
  • @JohnG actually it should work like Tom did it. Even after the new form is already displayed then it even updates the textbox after initialization. At least it does that on my machine. – Mong Zhu Jan 20 '22 at 09:45
  • did you check in the debugger that `listItem` contains a value at all? please tell us more about your listview? how is it populated ? only with strings? does it have only 1 column ? – Mong Zhu Jan 20 '22 at 09:46
  • "because launching the window itself happens on doubleclicking a listview item." ok so on double click you call only `SE.Show()` ? or do you also make a new instance? please post the entire click handler of this double click method? Where exactly is this line of code : `frmStatusEdit SE = new frmStatusEdit();` situated? I have a suspition that you create a new instance but the desired text ends up in the old instance, since the `selectedindexchanged` event will be triggered already after the first click. – Mong Zhu Jan 20 '22 at 09:50
  • @Mong Zhu … It is true that `SE.tbxAbbrevEmployee.Text = listItem;` will work if the text box is “publicly” exposed, however it just seems backwards to me. Why would the calling form be responsible for initializing some other forms controls? And it needlessly exposes a text box on the second form… why? Please forgive me as I am no expert, but it just sounds much easier to simply pass the `listItem` to the other form. – JohnG Jan 20 '22 at 09:53
  • @JohnG "hy would the calling form be responsible for initializing some other forms controls?" it shouldn't be and I would not do it this way personally, but technically it should work, that was all that I wanted to say. My guess is that the order of instantiation is messed up. If it is like this than passing the listItem wouldn't help either I think – Mong Zhu Jan 20 '22 at 09:56
  • The double click handler is nothing more than this: `public void lvEmployees1_DoubleClick(object sender, EventArgs e) { frmStatusEdit SE = new frmStatusEdit(); SE.Show(); MessageBox.Show(lvEmployees1.SelectedItems[0].Text); }` – Tom Jan 20 '22 at 09:59
  • The messagebox was/is for the test I mentioned earlier. In another event I try to pass listview item to textbox: `public void lvEmployees1_SelectedIndexChanged(object sender, EventArgs e) { if (lvEmployees1.SelectedItems.Count > 0) { string listItem = lvEmployees1.SelectedItems[0].Text; SE.tbxAbbrevEmployee.Text = listItem; } }` – Tom Jan 20 '22 at 10:02
  • 1
    @Mong Zhu … your last comment asked… _"Why would the calling form be responsible for initializing some other forms controls?"_ … And I agree, I do not know “why” you would want to do this, however the code… `SE.tbxAbbrevEmployee.Text = listItem;` … is doing just that. I am just saying that… that line of code is a red flag saying you are doing it wrong and directly conflicts with basic OOP concepts. We both know what the OP is trying to do and it WILL work, I was simply trying to point out another approach. Thank you for your useful comments. – JohnG Jan 20 '22 at 10:10
  • "The double click handler is nothing more than..." this is exactly you problem. you create a new local instance but in `lvEmployees1_SelectedIndexChanged` you access a class variable which is an entire different instance – Mong Zhu Jan 20 '22 at 10:11

1 Answers1

0

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();
    }
}
Shay Vaturi
  • 89
  • 1
  • 6