马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

C# 控件 RichTextBox 显示行号,并且与Panel相互联动

[复制链接]
查看3312 | 回复2 | 2019-11-8 09:55:42 | 显示全部楼层 |阅读模式

我们在利用到WINFORM窗体工作中,要求RichTextBox 参加行号;

之前有看到大牛们写的,但是太复杂繁多,而且有效双TextBox举行联动,非常不错,本日我们实行RichTextBox +Panel相互联动如下效果.

095703w3sptgzt6bjqsxgs.png

左侧灰色为Panel,右侧为RichTextBox 控件

1:准备Panel画布如下代码,当接到文件字符后举行坐标剖析,绘制行号。

095703r3yhys4ija4b34v4.gif
095704b03wdbrctj4iddiu.gif
  1. 1 private void showLineNo()
  2. 2 {
  3. 3 //获恰当前坐标信息
  4. 4 Point p = this.txtFileView.Location;
  5. 5 int crntFirstIndex = this.txtFileView.GetCharIndexFromPosition(p);
  6. 6
  7. 7 int crntFirstLine = this.txtFileView.GetLineFromCharIndex(crntFirstIndex);
  8. 8
  9. 9 Point crntFirstPos = this.txtFileView.GetPositionFromCharIndex(crntFirstIndex);
  10. 10
  11. 11 p.Y += this.txtFileView.Height;
  12. 12
  13. 13 int crntLastIndex = this.txtFileView.GetCharIndexFromPosition(p);
  14. 14
  15. 15 int crntLastLine = this.txtFileView.GetLineFromCharIndex(crntLastIndex);
  16. 16 Point crntLastPos = this.txtFileView.GetPositionFromCharIndex(crntLastIndex);
  17. 17
  18. 18 //准备绘图
  19. 19 Graphics g = this.panel2.CreateGraphics();
  20. 20
  21. 21 Font font = new Font(this.txtFileView.Font, this.txtFileView.Font.Style);
  22. 22
  23. 23 SolidBrush brush = new SolidBrush(Color.Green);
  24. 24
  25. 25 //绘图开始
  26. 26
  27. 27 //革新画布
  28. 28
  29. 29 Rectangle rect = this.panel2.ClientRectangle;
  30. 30 brush.Color = this.panel2.BackColor;
  31. 31
  32. 32 g.FillRectangle(brush, 0, 0, this.panel2.ClientRectangle.Width, this.panel2.ClientRectangle.Height);
  33. 33
  34. 34 brush.Color = Color.White;//重置画笔颜色
  35. 35
  36. 36 //绘制行号
  37. 37
  38. 38 int lineSpace = 0;
  39. 39
  40. 40 if (crntFirstLine != crntLastLine)
  41. 41 {
  42. 42 lineSpace = (crntLastPos.Y - crntFirstPos.Y) / (crntLastLine - crntFirstLine);
  43. 43
  44. 44 }
  45. 45
  46. 46 else
  47. 47 {
  48. 48 lineSpace = Convert.ToInt32(this.txtFileView.Font.Size);
  49. 49
  50. 50 }
  51. 51 int brushX = this.panel2.ClientRectangle.Width - Convert.ToInt32(font.Size * 3);
  52. 52
  53. 53 int brushY = crntLastPos.Y + Convert.ToInt32(font.Size * 0.21f);
  54. 54 for (int i = crntLastLine; i >= crntFirstLine; i--)
  55. 55 {
  56. 56
  57. 57 g.DrawString((i + 1).ToString(), font, brush, brushX, brushY);
  58. 58
  59. 59 brushY -= lineSpace;
  60. 60 }
  61. 61
  62. 62 g.Dispose();
  63. 63
  64. 64 font.Dispose();
  65. 65
  66. 66 brush.Dispose();
  67. 67 }
复制代码
View Code

2:事故准备(启用)如下事故

控件加载事故

095704rccglc82fz11gxgf.gif
095704kn22n2a6intovz0t.gif
  1. 1 private void txtFileView_TextChanged(object sender, EventArgs e)
  2. 2 {
  3. 3 showLineNo();
  4. 4 }
复制代码
View Code

控件滚动事故(当算出的行数大于本控件长度)

095704iz1potc41043utso.gif
095704bwi8qolzxiy8klrp.gif
  1. 1 private void txtFileView_VScroll(object sender, EventArgs e)
  2. 2 {
  3. 3 showLineNo();
  4. 4 }
复制代码
View Code

完成后,直接启用运行,Demo事例中的效果就出来,方便各人用于各种应用上.

C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则