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
Clickevent is theButton. To use theRichTextBoxin that eent handler, when creating theRichTextBoxassign a name to itsNameproperty and then usevar rtb = (RichTextBox)this.Controls.Find("the name", true).FirstOrDefault();.