7 Replies - 1269 Views - Last Post: 19 February 2013 - 09:19 AM Rate Topic: -----

#1 msz_900  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 40
  • Joined: 09-February 11

c# richtextbox help in clearing text

Posted 16 February 2013 - 06:58 PM

hy guy's i have a tbcontrol in my winform and i add richtextbox control in every new tab, when i click on the new button a new tab will open with richhtextbox control, now i want to clear the current/active tab text mean's the current visited tab, i tried many times but no logic work on it.

richtextbox1.text="";
or
richtextbox1.clear();




the above code will only work on first tab and clear the first tab text only..
please if any one have the logic then put it into my below winform code...


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace my_notepad
{
    public partial class Form1 : Form
    { 
       
        public Form1()
        {
            InitializeComponent();
        }
               
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RichTextBox rh = new RichTextBox();
            rh.Size = new System.Drawing.Size(1015, 750);
            TabPage tb = new TabPage("new");
            tabControl1.TabPages.Add(tb);
            tb.Controls.Add(rh);
          
        }

        private void clearTextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = -1;
            richTextBox1.Text = "";
        }

        private void colourToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() != DialogResult.Cancel)
            {
                richTextBox1.ForeColor = colorDialog1.Color;
            }
        }

        private void fontToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (fontDialog1.ShowDialog() != DialogResult.Cancel)
            {
                richTextBox1.Font = fontDialog1.Font;
            }
        }
    }
}





Is This A Good Question/Topic? 0
  • +

Replies To: c# richtextbox help in clearing text

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,257
  • Joined: 27-December 08

Re: c# richtextbox help in clearing text

Posted 16 February 2013 - 07:08 PM

Moved to C#. Please don't post help questions in the Challenges forum.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,463
  • Joined: 05-May 12

Re: c# richtextbox help in clearing text

Posted 16 February 2013 - 09:58 PM

You'll need a helper property like CurrentRichTextBox. The property gets the currently selected TabPage and digs through its Controls collection and returns the RichTextBox which you inserted on line 26.

I'm very puzzled, though. On lines 22-26 you knew enough about adding a new tab page and a child rich edit control, but in your later code, you somehow lost that knowledge and started trying to use the member variable richTextBox1. Did you steal the code for lines 22-26 from someplace else without understanding what it was doing? Or were you expecting that richTextBox1 would somehow magically reference the RichTextBox in the current tab with you doing any extra work?
Was This Post Helpful? 0
  • +
  • -

#4 msz_900  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 40
  • Joined: 09-February 11

Re: c# richtextbox help in clearing text

Posted 18 February 2013 - 10:53 AM

still not working
Was This Post Helpful? -1
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,463
  • Joined: 05-May 12

Re: c# richtextbox help in clearing text

Posted 18 February 2013 - 12:01 PM

Saying "Still not working" doesn't help us help you. Post your updated code and tell us what is happening.

This post has been edited by Skydiver: 18 February 2013 - 12:02 PM

Was This Post Helpful? 0
  • +
  • -

#6 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1182
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: c# richtextbox help in clearing text

Posted 18 February 2013 - 12:07 PM

Of course it's still not working. You don't understand your code, Skydiver tried to engage you but you ignored it and bumped your thread. Go to wherever you found that code and look for documentation or a tutorial on how to use it.
Was This Post Helpful? 0
  • +
  • -

#7 msz_900  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 40
  • Joined: 09-February 11

Re: c# richtextbox help in clearing text

Posted 19 February 2013 - 09:00 AM

from "still not working " i mean that some user's tell me to try..
tb.text="";
or
tb=null; 


but both of the two are not working. i search from many places and one thing i found that many user's have problem with many tab,s and if they perform some action they face almost the same error, and ask for help that "how to remove tab's text or how to reset it" because it work's on the first tab as i mentioned in my above code. the problem i am facing know is "how clear my current tab text".?
Was This Post Helpful? 0
  • +
  • -

#8 msz_900  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 40
  • Joined: 09-February 11

Re: c# richtextbox help in clearing text

Posted 19 February 2013 - 09:19 AM

the mistry has been solved. thank's for every one try to help me. the solved is ...


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace my_notepad
{
    public partial class Form1 : Form
    { 
       
        public Form1()
        {
            InitializeComponent();
        }
        RichTextBox rh = new RichTextBox();
        TabPage tb = new TabPage();
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RichTextBox rh = new RichTextBox();
            rh.Size = new System.Drawing.Size(1015, 750);
            TabPage tb = new TabPage("new");
            tabControl1.TabPages.Add(tb);
            tb.Controls.Add(rh);
            tabControl1.SelectedIndex = tabControl1.TabPages.Count - 1;
            rh.Focus();
            tb.Focus();
        }

        private void clearTextToolStripMenuItem_Click(object sender, EventArgs e)
        {

            foreach (Control l_control in tabControl1.SelectedTab.Controls)
            {
                if (l_control is RichTextBox)
                {
                    (l_control as RichTextBox).Clear();
                    break;
                }
            }
            
        }

        private void colourToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() != DialogResult.Cancel)
            {
                richTextBox1.ForeColor = colorDialog1.Color;
            }
        }

        private void fontToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (fontDialog1.ShowDialog() != DialogResult.Cancel)
            {
                richTextBox1.Font = fontDialog1.Font;
            }
        }

       
    }
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1