C#以编程方式创建richtextbox并从按钮获取文本值

时间:2017-12-03 17:11:00

标签: c# winforms

希望你能提供帮助 - 我的代码存在小问题。 我以编程方式创建了从数据库填充的富文本框和文本 - 然后将其添加到我以编程方式创建另一个按钮的面板中。

显示:

private void GetPending()
    {
        SQL = "SELECT notID,notNote FROM Notes WHERE notisActive = @notisActive AND notUser = @notuser ";
        y = 3;
        using (SqlConnection SQLCon = new SqlConnection(ConnectionString))
        {
            SqlCommand cmd = new SqlCommand(SQL, SQLCon);
            cmd.Parameters.Add(new SqlParameter("notIsActive", "Pending"));
            cmd.Parameters.Add(new SqlParameter("notUser", lblUserName.Text));

            try
            {
                SQLCon.Open();
                using (SqlDataReader read = cmd.ExecuteReader())
                {
                    while (read.Read())
                    {
                        //Main Panel
                        Panel pnlPendingNote = new Panel();
                        pnlPendingNote.Size = new System.Drawing.Size(315, 110);
                        pnlPendingNote.Location = new Point(3, y);
                        pnlPendingNote.BorderStyle = BorderStyle.FixedSingle;
                        pnlPendingNote.BackColor = Color.FromArgb(244, 244, 244);

             // Button to Activate To Do
                        Button butActivateToDo = new Button();
                        butActivateToDo.Location = new Point(250, 10);
                        butActivateToDo.Size = new System.Drawing.Size(25, 25);
                        butActivateToDo.BackColor = Color.Transparent;
                        butActivateToDo.FlatStyle = FlatStyle.Flat;
                        butActivateToDo.FlatAppearance.BorderSize = 0;
                        butActivateToDo.FlatAppearance.MouseOverBackColor = Color.FromArgb(244, 244, 244);
                        butActivateToDo.Cursor = Cursors.Hand;

                        butActivateToDo.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.Activate_25));
                        pnlPendingNote.Controls.Add(butActivateToDo);

        RichTextBox rxtNotes = new RichTextBox();
                        rxtNotes.Size = new System.Drawing.Size(307, 68);
                        rxtNotes.Location = new Point(3, 37);
                        rxtNotes.Text = (read["notNote"].ToString());
                        rxtNotes.ReadOnly = true;
                        rxtNotes.BorderStyle = BorderStyle.None;
                        rxtNotes.BackColor = Color.FromArgb(244, 244, 244);
                        pnlPendingNote.Controls.Add(rxtNotes);

            pnlPendingNote.Name = "PenNote" + pendingcounter;
                        pnlPendingNote.Tag = read.GetInt32(0);
                        butActivateToDo.Name = "PenNote" + pendingcounter;
                        butActivateToDo.Tag = read.GetInt32(0);
                        rxtNotes.Name = "PenNote" + pendingcounter;
                        rxtNotes.Tag = read.GetInt32(0);

            // Increase by 1
                        pendingcounter++;
                        // Create Double Click
                        butActivateToDo.Click += new EventHandler(NewbutActivateToDo_Click);
                        pnlPendingNote.DoubleClick += new EventHandler(NewPendingButton_DoubleClick);
                        // Add Pending Note size inside Panding Panel
                        pnlPending.Controls.Add(pnlPendingNote);

                        y = y + 112;
                    }
                }
            }
            catch (System.Exception Error)
            {
                MessageBox.Show(Error.Message); // display error - if unable to connect to server
            }
            SQLCon.Close(); // close the sql connection 
        }
    }

哪个很棒 - 我创建了面板,文本框和按钮。

然后我有了这段代码:

 private void NewbutActivateToDo_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        RichTextBox rxtNotes = (RichTextBox)sender;



        for (int i = 1; i < pendingcounter; i++)
        {
            if (btn.Name == ("PenNote" + i))
            {   

                MessageBox.Show(rxtNotes.Text.ToString());
                break;
            }

        }


    }

这在某种程度上起作用 - 它得到的是我点击过的面板&amp;我得到了存储在标签中的ID。

接下来我想从文本框中获取文本值。 所以我添加了

RichTextBox rxtNotes = (RichTextBox)sender; 

这会引发错误:

{“无法将'System.Windows.Forms.Button'类型的对象强制转换为'System.Windows.Forms.RichTextBox'。”}

所以当我点击“ActivateToDo”按钮时,我想得到RtxtBox值。

希望这是有道理的 -

谢谢

1 个答案:

答案 0 :(得分:2)

在Button的Tag()属性中存储对关联的RichTextBox的引用:

Button butActivateToDo = new Button();
...
RichTextBox rxtNotes = new RichTextBox();
...
butActivateToDo.Tag = rxtNotes

现在可以在处理程序中检索RichTextBox:

private void NewbutActivateToDo_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    RichTextBox rxtNotes = (RichTextBox)btn.Tag;

    ...
}