C# TextBox滚动实现解析

开发 后端
我们在C# TextBox操作的时候实现C# TextBox滚动的具体操作是什么呢?在C# TextBox使用的时候需要注意什么呢?那么本文就向你介绍这些具体内容。

C# TextBox滚动实现具体的内容是什么?使用C# TextBox时需要注意什么呢?作为我们编程的实现C# TextBox滚动的操作细节是什么呢?那么下面我们来看看具体的C# TextBox滚动的操作实现以及C# TextBox使用需要注意的问题。

C# TextBox滚动实例代码:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using System.Runtime.InteropServices;  
  9. namespace WindowsApplication27  
  10. ...{  
  11. /**//// <summary>  
  12. /// 演示如何在TextBox中让文字循环滚动:  
  13. ///   
  14. /// C#中WinForm的TextBox循环自动滚动  
  15. /// </summary>  
  16. public partial class Form1 : Form  
  17. ...{  
  18. public Form1()  
  19. ...{  
  20. InitializeComponent();  
  21.  
  22. this.textBox1.Clear();  
  23. for (int i = 0; i <= 20;i++ )  
  24. ...{  
  25. this.textBox1.Text += string.Format("{0}:jinjazz__{1} ", i,i);  
  26. }  
  27. this.timer1.Interval = 200;  
  28. this.timer1.Start();  
  29. }  
  30.  
  31. //发送消息  
  32. [DllImport("user32.dll", EntryPoint = "SendMessage")]  
  33. public static extern int SendMessage(  
  34. IntPtr hWnd, int wMsg, int wParam, int lParam);  
  35. //获取滚动条位置  
  36. [DllImport("user32")]  
  37. public static extern int GetScrollPos(IntPtr hwnd, int nBar);  
  38. //设置滚动条位置  
  39. [DllImport("user32.dll")]  
  40. static extern int SetScrollPos(IntPtr hWnd, int nBar,  
  41.    int nPos, bool bRedraw);  
  42.  
  43. public const int EM_LINESCROLL = 0xb6;  
  44.    
  45. private void timer1_Tick(object sender, EventArgs e)  
  46. ...{  
  47. int i=  GetScrollPos(this.textBox1.Handle,1);  
  48.  
  49. //向下滚动一行  
  50. SendMessage(this.textBox1.Handle,   
  51. EM_LINESCROLL, 0, 1);//0,1代表垂直滚动条向下滚动  
  52.  
  53. //判断是否有位置变化,如果没有则说明到了底部,返回开始处  
  54. if (i == GetScrollPos(this.textBox1.Handle, 1))  
  55. ...{  
  56. //回到顶部,这里用SetScrollPos似乎有问题,滚动条和文字不是同步更新  
  57.  
  58. this.textBox1.SelectionStart = 0;  
  59. this.textBox1.SelectionLength = 1;  
  60. this.textBox1.ScrollToCaret();  
  61. this.textBox1.SelectionLength = 0;  
  62. }  
  63. Console.WriteLine(i);  
  64. }  
  65.  
  66. private void textBox1_MouseEnter(  
  67. object sender, EventArgs e)  
  68. ...{  
  69. this.timer1.Stop();  
  70. }  
  71.  
  72. private void textBox1_MouseLeave(  
  73. object sender, EventArgs e)  
  74. ...{  
  75. this.timer1.Start();  
  76. }  
  77. }  
  78. }  

C# TextBox使用是要注意:

1、如何在多行TextBox中写入文本时实现换行:由于Windows系统中,回车符需两上字符。因此方法是使用\r\n标记,如

  1. Label="Calculation "+":.......SUM\r\n";  
  2. textBox.AppendText(Label); 

另外还有一个办法是用Environment.Newline的方法,可以兼容Windows和Linux系统。

2、如何在多行TextBox中用滚动条,使添加文本后自动滚动显示到最后一行:方法是使用ScrollToCaret方法,自动滚动到插入符的位置,如:

  1. textBox.AppendText(Label);  
  2. textBox.ScrollToCaret(); 

C# TextBox滚动的实现以及C# TextBox使用时需要注意的基本内容就向你介绍到这里,希望对你了解和学习C# TextBox滚动、换行等等有所帮助。

【编辑推荐】

  1. C# CheckBox控件概念以及用途浅析
  2. 学习C# MessageBox用法的一点体会
  3. 浅析C# TextBox事件实现体会
  4. 浅析ASP.NET回车提交事件
  5. C# TextBox事件实现实例详解
责任编辑:仲衡 来源: 网易博客
相关推荐

2009-09-02 17:29:10

C# TextBox换

2009-09-09 11:29:32

C# TextBox事

2009-09-09 12:55:59

C# TextBox事

2009-09-08 22:53:39

c# textbox数

2009-09-09 13:31:15

C# TextBox

2009-09-10 09:10:17

C# TextBox换

2009-09-09 21:56:29

2009-09-10 10:22:05

C# TextBox

2009-09-09 22:31:21

c# textbox失

2009-09-02 16:30:20

C#定义数组

2009-08-31 17:16:12

C#实现接口

2009-08-31 18:01:41

C#接口事件

2009-08-26 10:43:14

C#实现打印功能

2009-09-08 22:58:00

c# textbox数

2009-09-07 15:27:04

C# MessageB

2009-09-08 23:35:12

c# textbox失

2009-09-01 13:59:01

C#操作Excel

2009-09-10 09:42:53

C# TextBox

2009-09-01 16:59:25

C#画直线

2009-09-09 16:30:59

C# BinaryFo
点赞
收藏

51CTO技术栈公众号