Click here to Skip to main content
15,902,893 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually when I check this button it inserts Date and time at the top of rich text box which is fine till here but now I want that when I click again on this button and I uncheck it, it should remove the date and time it inserted when I checked it

what happens till now is when I check the button it inserts date and time at top and when I uncheck it nothing happens but when I check it again it again inserts date and time at top
I've also tried using the checked state changed event but it clears the whole rich text box.

here is what I've done for this

What I have tried:

C#
private RichTextBox Getnew()
{
    RichTextBox box = null;
    TabPage z = tabControl1.SelectedTab;
    if (z != null | z == null)
    {
        box = z.Controls[0] as RichTextBox;
    }
    return box;
}

private void dateTimeToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (dateTimeToolStripMenuItem.Checked == true)
    {
        Getnew().Text = Getnew().Text.Insert(0, DateTime.Now.ToLongDateString() + DateTime.Now.ToLongTimeString()+"\n");
    }
}

private void dateTimeToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
{
    // Sorry I've Removed this.
}
Posted
Updated 1-Jun-18 5:18am
v3
Comments
Richard MacCutchan 1-Jun-18 10:45am    
You need to add the code to the dateTimeToolStripMenuItem_Click method, as the else part of your if statement.
[no name] 1-Jun-18 12:14pm    
What's the point?

Just have a separate "date time" field. If it needs to be "added" to the RTB "output", do it at "commit" time.

1 solution

0) As pointed out by Richard, part of your problem is that you need an else condition to remove the datetime.

1) The other part of your problem is that you have no way of knowing what to remove because you're not storing the datetime (that was inserted) in a variable. How can you possibly remove it if you don't know what to remove?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900