C# TextBox滚动实现解析

我们在C# TextBox操作的时候实现C# TextBox滚动的具体操作是什么呢?在C# Text
首页 新闻资讯 行业资讯 C# TextBox滚动实现解析

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

C# TextBox滚动实例代码:

复制

using System;  using System.Collections.Generic;  using System.ComponentModel;  using System.Data;  using System.Drawing;  using System.Text;  using System.Windows.Forms;  using System.Runtime.InteropServices;  namespace WindowsApplication27  ...{  /**//// <summary>  /// 演示如何在TextBox中让文字循环滚动:  ///   /// C#中WinForm的TextBox循环自动滚动  /// </summary>  public partial class Form1 : Form  ...{  public Form1()  ...{  InitializeComponent();   this.textBox1.Clear();  for (int i = 0; i <= 20;i++ )  ...{  this.textBox1.Text += string.Format("{0}:jinjazz__{1} ", i,i);  }  this.timer1.Interval = 200;  this.timer1.Start();  }   //发送消息  [DllImport("user32.dll", EntryPoint = "SendMessage")]  public static extern int SendMessage(  IntPtr hWnd, int wMsg, int wParam, int lParam);  //获取滚动条位置  [DllImport("user32")]  public static extern int GetScrollPos(IntPtr hwnd, int nBar);  //设置滚动条位置  [DllImport("user32.dll")]  static extern int SetScrollPos(IntPtr hWnd, int nBar,     int nPos, bool bRedraw);   public const int EM_LINESCROLL = 0xb6;     private void timer1_Tick(object sender, EventArgs e)  ...{  int i=  GetScrollPos(this.textBox1.Handle,1);   //向下滚动一行  SendMessage(this.textBox1.Handle,   EM_LINESCROLL, 0, 1);//0,1代表垂直滚动条向下滚动   //判断是否有位置变化,如果没有则说明到了底部,返回开始处  if (i == GetScrollPos(this.textBox1.Handle, 1))  ...{  //回到顶部,这里用SetScrollPos似乎有问题,滚动条和文字不是同步更新   this.textBox1.SelectionStart = 0;  this.textBox1.SelectionLength = 1;  this.textBox1.ScrollToCaret();  this.textBox1.SelectionLength = 0;  }  Console.WriteLine(i);  }   private void textBox1_MouseEnter(  object sender, EventArgs e)  ...{  this.timer1.Stop();  }   private void textBox1_MouseLeave(  object sender, EventArgs e)  ...{  this.timer1.Start();  }  }  }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

  • 25.

  • 26.

  • 27.

  • 28.

  • 29.

  • 30.

  • 31.

  • 32.

  • 33.

  • 34.

  • 35.

  • 36.

  • 37.

  • 38.

  • 39.

  • 40.

  • 41.

  • 42.

  • 43.

  • 44.

  • 45.

  • 46.

  • 47.

  • 48.

  • 49.

  • 50.

  • 51.

  • 52.

  • 53.

  • 54.

  • 55.

  • 56.

  • 57.

  • 58.

  • 59.

  • 60.

  • 61.

  • 62.

  • 63.

  • 64.

  • 65.

  • 66.

  • 67.

  • 68.

  • 69.

  • 70.

  • 71.

  • 72.

  • 73.

  • 74.

  • 75.

  • 76.

  • 77.

  • 78.

C# TextBox使用是要注意:

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

复制

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

  • 2.

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

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

复制

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

  • 2.

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

【编辑推荐】

  1. C# CheckBox控件概念以及用途浅析

  2. 学习C# MessageBox用法的一点体会

  3. 浅析C# TextBox事件实现体会

  4. 浅析ASP.NET回车提交事件

  5. C# TextBox事件实现实例详解

16    2009-09-09 13:18:10    C# TextBox滚动 C# TextBox