Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

415
Visualizações
How do I prevent RichTextBox from jumping to the top of the document when links are clicked?

As part of my GUI in C# (.Net 5.0) I'm using the RichTextBox control (part of System.Windows.Forms) to display text and clickable links. My problem is when I click any links included without the form first having focus the act of clicking a link also focuses the textbox which automatically places the caret at the start of the text, this has the effect of making the textbox jump (or scroll) to the very top of document, losing one's position.

For my particular problem, the RichTextBox classes are assigned to TabPages within a TabControl- and all three of these components are created in response to user interaction (I understood the creation of these at runtime might limit the types of event handling available to me).

I've defined the RichTextBox control with multiline and both horizontal and vertical scrollbars, and to enable clickable links I'm using the DetectUrls option. Here is the full list of properties being used.

var control = new RichTextBox();
control.DetectUrls = true;
control.Dock = DockStyle.Fill;
control.Multiline = true;
control.ReadOnly = true;
control.WordWrap = false;
control.Text = myContent; // large content filling multiple screens
control.ScrollBars = RichTextBoxScrollBars.Both;
control.LinkClicked += new LinkClickedEventHandler(RichTextBoxLinkClicked);

I've included the LinkClicked because I want to provide some interaction for the user, but whether this handler is assigned or not doesn't seem to influence the problem.

Edit1 I came up with a solution that works to some extent; it prevents the scrolling by placing the caret at the beginning of the link (It doesn't work well though if there are more than one of the same link)

private void RichTextBoxLinkClicked(object sender, LinkClickedEventArgs e) {
  int caret = ((RichTextBox) sender).Text.IndexOf(e.LinkText);
  SelectionStart = caret;
}

Edit2 A second solution that I found is to set the focus of the text-box on a MouseEnter event. I've come to think the key to the problem is to set the focus of the text boxes before any user interaction takes place, but I'm having problems finding a satisfactory way to achieve it.

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda