how could I define I m coming to the end of a rtf box?

I've thought for ages and maybe its just something simple missing that I cant see. What I want to do is 'do something' when I m coming to the end or at the end of a rtf box when typing (either way not bothered) in a way like below:

private void defineendofrtf(richtextbox rtf)

if ('end of rtf is coming - code I need here')

{

do something;

}

Anyone have any ideas anything to do with rtf.lines



Answer this question

how could I define I m coming to the end of a rtf box?

  • Milos Velikovsky

    Well let me see if I can help.

    If I understand correctly you want to detect when someone is coming close to the end of your richtextbox while typing.

    All you need to do is add an eventhandler for the TextChanged event. To do this, go to the properties window with your RTB selected, click on the events button that looks like a lightning bolt, and double click textchanged and add this code. Be sure to set your maxlength to something reasonable to test this. (50,100,150.. etc)

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
    if ((sender as RichTextBox).Text.Length == (sender as RichTextBox).MaxLength)
    MessageBox.Show("max");
    }

    or perhaps you can add a label like so to show how many chars you can still type.

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
    int charleft = (sender as RichTextBox).MaxLength - (sender as RichTextBox).Text.Length;

    lblChars.Text = charleft.ToString();

    if (charleft == 0)
    MessageBox.Show("max");
    }



  • Sucker in HK

    well thanks for the quick reply! I will try it very soon and see how it goes and will mark as answer accordingly if it works!
  • Norbert Mika

    Well I'm not sure exactly what you are trying to accomplish. You may want to write you own text box type of control or extend RichTextBox and add functionality or change it. You may want to read about writing your own controls. If all you want to do is check the height of the font in one small place you can try something like this.

    int maxlines = rtb.Height / rtb.Font.Height;

    if (rtb.Lines.Length == maxlines)
    // do something

    basically the maximum number of lines that can fit in that space would be the textbox's heigh divided by the line height of its current font. This is very basic though, so you will probably have to use some imagination and a lot of research to figure out what it is that you are trying to accomplish. Maybe if you post your idea here some guru (better than I) will step in and show you how, but you may take more pride and learn more by working through it.

    =]



  • Bradley Grainger

    That worked great! but lets say I have a rtf box size of 1184 x 832 (a4 size) and whatever the size of the font I dont want the user to go beyond the a4 size above, because 18pt font will go on for a longer distance that a 10pt font size. So how could I change you good code above to reference to the height of the rtf box
  • how could I define I m coming to the end of a rtf box?