Row position and column that was clicked inside the Richtextbox

Asked

Viewed 17 times

0

I have a simple application that only reads a file and returns what is in it within a richtextbox. The Point is when I click on the inside of the richtextbox, I would have to turn to the position of the row and column where it was clicked, equal to the functionality of a normal Notepad as shown in the figure.

inserir a descrição da imagem aqui

I managed to catch the line and position through the mousedown function.

inserir a descrição da imagem aqui

Is there any ready-made method to return the spine ? I searched the microsoft documentation but did not find.

code I’ve done so far

private void rtbVisualizar_MouseDown(object sender, MouseEventArgs e)
{
    int pos = rtbVisualizar.SelectionStart;
    int linha = rtbVisualizar.GetLineFromCharIndex(pos);
    lblLC.Text = $"Linha: {linha + 1} | Coluna:  | Posição: {pos + 1}";
}
  • Please clarify your problem or provide additional details in order to highlight exactly what you need. The way it’s written these days it’s hard to tell exactly what you’re asking.

  • Tried the CaretPosition? https://docs.microsoft.com/pt-br/dotnet/api/system.windows.controls.richtextbox.caretposition?view=net-5.0

1 answer

0

I have found the Solution! In the variable I take the total position in which the point that was clicked is located. On the variable line I pick up the line on which lies the clicked point, In the variable col I subtract between the position clicked and the first index of the row. thus returning the value of the column clicked.

inserir a descrição da imagem aqui

private void rtbVisualizar_MouseDown(object sender, MouseEventArgs e)
{
    int pos = rtbVisualizar.SelectionStart;
    int linha = rtbVisualizar.GetLineFromCharIndex(pos);
    int col = rtbVisualizar.SelectionStart - rtbVisualizar.GetFirstCharIndexFromLine(linha);
    
    lblLC.Text = $"Linha: {linha + 1} | Coluna: {col + 1} ";
}

Browser other questions tagged

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