How to select from the end of a textbox

Asked

Viewed 48 times

-1

Good afternoon, you guys!

My question is simple and a little silly, but it’s making me crack my head.

My program has a textbox where a log is saved, at all times new data (characters) are added to this textbox. What I really care about in this log are your last 7 characters. I was thinking of using the "textbox.Selection" control to select them, the problem is that there is (as far as I know) a "Selectionend". The Selectionstart arrow command from where the selection starts and the Length how many characters this selection understands, what I need is to set where the selection should end (as I said, something like "Selectionend") and use "Selectionlength" to set that it is from this point up to 7 characters ago that the selection must understand. How can I do this as simply as possible? These last 7 characters will be used as log file name in automatic saving of the same.

Follow code for auto save. (where my problem is)

{
            CaptureInfo.CaptureFrame();

            textBox1.SelectionStart = 99999;
            textBox1.SelectionLength = -7;

            Thread.Sleep(1000);

            caminhoImagemSalva = @"\\172.21.xxx.xxx\Impresa tal\Arquivo Digital\Arquivo de imagem atendimento\Guichê 04\"
                 + "Atendente 04"
                 + " "
                 + textBox1.SelectedText
                 + " "
                 + DateTime.Now.Day.ToString()
                 + "."
                 + DateTime.Now.Month.ToString()
                 + "."
                 + DateTime.Now.Year.ToString()
                 + " as "
                 + DateTime.Now.Hour.ToString()
                 + "h"
                 + DateTime.Now.Minute.ToString()
                 + "min"
                 + ".jpg";
            picWebCam.Image.Save(caminhoImagemSalva, ImageFormat.Jpeg);


        }

Thank you in advance!

  • If you know the size of the text in Textbox and how many you want to grab it is easy to know which is the start.

  • In the case in question the textbox does not have a predefined size.

  • I don’t understand... what do you want with this? Capture this content or make it available on the clip board?

1 answer

0


The way to select the last 7 characters is simple, and you can recover this information simply through the attribute SelectedText, as in the example below.

var numeroDeCaracteres = 7;
var startIndex = textBox1.Text.Length - numeroDeCaracteres;

textBox1.SelectionStart = startIndex;
textBox1.SelectionLength = numeroDeCaracteres;

But this type of operation is more common for interface manipulation or if you want to for example release this content in clipbard, crop and or paste in some other control... if what interests you content, it makes more sense just a Text Substring...

var numeroDeCaracteres = 7;
var startIndex = textBox1.Text.Length - numeroDeCaracteres;
var conteudo = textBox1.Text.Substring(startIndex);
  • Perfect! The idea is to use part of the textbox content (in this case the last 7 characters) as the name variable for an image file. I think the first method works better in my code because I use "textBox1.Selectedtext" as output. I will take the test of both and then tell you which worked best. Thank you Leandro!

  • Tried and tested and approved! Both methods worked perfectly, but I preferred to use the first because it did not need any other modification in my code. Once again my sincere thanks to Leandro! Valeuu!

  • I recommend the second option, you will not need to change so much and what to change will be for the better. It makes much more sense...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.