简体   繁体   中英

C# Pass variables from a dynamically created event

My problem is this: I have a form which is created at runtime within it's own function which takes 2 parameters to retrieve a text value from a dataGridView and display it in a textBox. The issue is that when I call this function on it's own it works fine however when called from another runtime-created form control event, no text value is returned but the form is still displayed.

This is the code which creates and shows the form:

private void commentsForm(int x, int y)
        {
            using (Form frmDisplayText = new Form())
            {
                TextBox txt = new TextBox();//                  <- Decleration
                Button btnAccept = new Button();
                Button btnRevert = new Button();
                Button btnClose = new Button();
                frmDisplayText.Size = new Size(550, 350);//     <- Size
                txt.Size = new Size(510, 250);
                btnAccept.Size = new Size(216, 30);
                btnRevert.Size = new Size(216, 30);
                btnClose.Size = new Size(66, 30);
                txt.Location = new Point(12, 12);//             <- Location
                btnAccept.Location = new Point(12, 269);
                btnRevert.Location = new Point(234, 269);
                btnClose.Location = new Point(456, 269);
                string dataToOutput = dataGridView1.Rows[x].Cells[y].Value.ToString();//  <- Text
                txt.Text = dataToOutput;
                frmDisplayText.Text = "Comments";
                btnAccept.Text = "Accept";
                btnRevert.Text = "Revert";
                btnClose.Text = "Cancel";
                txt.Multiline = true;//                         <- Other
                btnAccept.Font = new Font(FontFamily.GenericSansSerif, 12.0F, FontStyle.Regular);
                btnRevert.Font = new Font(FontFamily.GenericSansSerif, 12.0F, FontStyle.Regular);
                btnClose.Font = new Font(FontFamily.GenericSansSerif, 12.0F, FontStyle.Regular);

                btnAccept.Click += (_, args) =>//               <- Event Handlers
                {
                    string txtText = txt.Text;
                    using (var conn = new SqlConnection(connection))
                    {
                        string SQL = "UPDATE Comments SET [" + dataGridView1.Rows[x].Cells[y].OwningColumn.HeaderText.ToString() + "] = @val2 WHERE ID = @val3";
                        using (var cmd = new SqlCommand(SQL, conn))
                        {
                            cmd.Parameters.AddWithValue("@val2", txt.Text);
                            cmd.Parameters.AddWithValue("@val3", dataGridView1.Rows[x].Cells[0].Value.ToString());
                            try
                            {
                                conn.Open();
                                cmd.ExecuteNonQuery();
                                dataGridView1.Rows[x].Cells[y].Value = txt.Text;
                                MessageBox.Show("Success");
                            }
                            catch (SqlException ex)
                            {
                                MessageBox.Show(ex.ToString());
                            }
                        }
                    }
                };
                btnRevert.Click += (_, args) =>
                {
                    txt.Text = dataToOutput;
                };
                btnClose.Click += (_, args) =>
                {
                    frmDisplayText.Close();
                };
                frmDisplayText.Controls.Add(txt);//             <- Add Controls
                frmDisplayText.Controls.Add(btnAccept);
                frmDisplayText.Controls.Add(btnRevert);
                frmDisplayText.Controls.Add(btnClose);
                frmDisplayText.ShowDialog();
            }
        }


And here is when it is called:

                btnAddComment.Click += (_, args) =>
                {
                    commentsForm(x, y);
                };

Any ideas as to why the text property is null when called form here?

Thanks all

除了要检索的数据之外,还有一个行索引。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM