-1

I'm working on a football related project in C#, winForms. I'm trying to make an update button which opens another window. this is my class

public class Match
{
    public string team1 { get; set; }
    public string team2 { get; set; }
    public string pariu { get; set; }
    public float cota { get; set; }
    public DateTime data { get; set; }
}

This is in Form1

List<Match> m = new List<Match>();
//UPDATE
    private void btEdit_Click(object sender, EventArgs e)
    {
        if (listView1.SelectedItems.Count != 0)
        {
            Match p = m.ElementAt(listView1.SelectedIndices[0]);

            Form2 edit = new Form2(p);
            DialogResult dialogResult = edit.ShowDialog();//this is everytime = cancel

            if (dialogResult == DialogResult.OK)
            {
   
                PopulateListView();//so this never happens
            }
        }
    }

this is form2

 private void button2_Click(object sender, EventArgs e)
    {
        this.Close();//cancel
    }

    private void button1_Click(object sender, EventArgs e)
    {
        match.team1 = textBox1.Text;
        match.team2 = textBox2.Text;
        float.TryParse(textBox3.Text, out float value);
        match.cota = value;
        this.Close();//save edit
    }

Why do I get everytime when I update something in my listView, showDialog = 'Cancel'?

  • Are you working on Instat? – Kamiky Apr 29 '21 at 10:18
  • @Kamiky Nope, this is just a project for college, Im trying to make a betting platform for soccer games. – Foreastbtch Apr 29 '21 at 10:20
  • You can set the `DialogResult` to a Button directly, using its `DialogResult` property. When the Button is clicked, it also defines the result of the Dialog. -- Remove `Close()` when you use a DialogResult, since setting this also closes the Dialog. – Jimi Apr 29 '21 at 15:19

1 Answers1

2

Replace in method button1_Click()

this.Close()

to

Close();
DialogResult = DialogResult.OK;
Kamiky
  • 601
  • 2
  • 8
  • 25
  • 2
    If you set `DialogResult`, it will automatically close the dialog when the current method returns, so you shouldn't need to call `.Close()` at all. – Matthew Watson Apr 29 '21 at 10:46
  • @Fildor: _"I'd actually set the result before calling Close()"_ -- from a readability point of view, that's probably not a bad idea. But in practice it doesn't matter. The only code that will care what the value of the `DialogResult` property is, won't be able to run until that method has returned, so the order is irrelevant. Of course, it's better to just set the `AcceptButton` and/or the button's own `DialogResult` property so that handling the `Click` event isn't even necessary. And of course, even if handling the event, there's not even a need to call `Close()`...just set `DialogResult`. – Peter Duniho May 16 '21 at 00:58