新聞中心
C# TextBox滾動實現(xiàn)具體的內(nèi)容是什么?使用C# TextBox時需要注意什么呢?作為我們編程的實現(xiàn)C# TextBox滾動的操作細節(jié)是什么呢?那么下面我們來看看具體的C# TextBox滾動的操作實現(xiàn)以及C# TextBox使用需要注意的問題。

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供坡頭網(wǎng)站建設、坡頭做網(wǎng)站、坡頭網(wǎng)站設計、坡頭網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、坡頭企業(yè)網(wǎng)站模板建站服務,十余年坡頭做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。
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
- ...{
- /**////
- /// 演示如何在TextBox中讓文字循環(huán)滾動:
- ///
- /// C#中WinForm的TextBox循環(huán)自動滾動
- ///
- 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();
- }
- //發(fā)送消息
- [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();
- }
- }
- }
C# TextBox使用是要注意:
1、如何在多行TextBox中寫入文本時實現(xiàn)換行:由于Windows系統(tǒng)中,回車符需兩上字符。因此方法是使用\r\n標記,如
- Label="Calculation "+":.......SUM\r\n";
- textBox.AppendText(Label);
另外還有一個辦法是用Environment.Newline的方法,可以兼容Windows和Linux系統(tǒng)。
2、如何在多行TextBox中用滾動條,使添加文本后自動滾動顯示到最后一行:方法是使用ScrollToCaret方法,自動滾動到插入符的位置,如:
- textBox.AppendText(Label);
- textBox.ScrollToCaret();
C# TextBox滾動的實現(xiàn)以及C# TextBox使用時需要注意的基本內(nèi)容就向你介紹到這里,希望對你了解和學習C# TextBox滾動、換行等等有所幫助。
本文題目:C#TextBox滾動實現(xiàn)解析
當前網(wǎng)址:http://m.fisionsoft.com.cn/article/dhhppip.html


咨詢
建站咨詢
