1

Hope you can help - I have small issue with code. I have programmatically create rich textbox and text populated from database - which is then added to a panel where I have another button programmatically created.

As displayed:

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 
        }
    }

Which Works great - i have my panels, textbox and button created.

I then have this code :

 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;
            }

        }


    }

Which is working to a degree - it get's what panel i have clicked on & I get the ID which is stored in the tag.

Next I want to get the text value from the text box. So i have added the

RichTextBox rxtNotes = (RichTextBox)sender; 

this throws error :

{"Unable to cast object of type 'System.Windows.Forms.Button' to type 'System.Windows.Forms.RichTextBox'."}

So I would like to get the RtxtBox value when I click a "ActivateToDo" button.

Hope this makes sense -

thanks

1
  • Sender of Click event is the Button. To use the RichTextBox in that eent handler, when creating the RichTextBox assign a name to its Name property and then use var rtb = (RichTextBox)this.Controls.Find("the name", true).FirstOrDefault();. Commented Dec 3, 2017 at 17:36

1 Answer 1

2

Store a reference to the associated RichTextBox in the Tag() property of your Button:

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

Now the RichTextBox can be retrieved in the handler:

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

    ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.