private void Lablel_ActualizarLinea() { //Obtenemos índice del primer caracter visible y //número de la primera línea visible Point pos = new Point(0, 0); int firstIndex = Panel_Codigo.GetCharIndexFromPosition(pos); int firstLine = Panel_Codigo.GetLineFromCharIndex(firstIndex); //Ahora obtenemos el índice del último carácter visible y //el número de la última línea visible. pos.X = ClientRectangle.Width; pos.Y = ClientRectangle.Height; int lastIndex = Panel_Codigo.GetCharIndexFromPosition(pos); int lastLine = Panel_Codigo.GetLineFromCharIndex(lastIndex); //esta es la posición del punto del último carácter visible, //usaremos su valor Y para calcular el tamaño de la etiqueta pos = Panel_Codigo.GetPositionFromCharIndex(lastIndex); //finalmente, se renumera etiqueta label1.Text = ""; for (int i = firstLine; i <= lastLine; i++) { label1.Text += i + 1 + "\n"; } }
/////////////////////////////////////////////////////////////////////////////////////////// #region Metodo_Colocar_Numero_Linea private void Panel_Codigo_VScroll(object sender, EventArgs e) { //move location of numberLabel for amount //of pixels caused by scrollbar int d = Panel_Codigo.GetPositionFromCharIndex(0).Y % (Panel_Codigo.Font.Height + 1); label1.Location = new Point(0, d); Lablel_ActualizarLinea(); }