网上找到的一个方法,速度很快,但有些瑕疵,搞了半天,没解决掉瑕疵,请高手帮忙改改;
源码:using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace csTest
{
public class Form1 : System.Windows.Forms.Form
{
[DllImport("user32")]
public static extern bool GetCaretPos(ref System.Drawing.Point lpPoint);
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button buttonGetResult;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.buttonGetResult = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(16, 24);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(528, 232);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// buttonGetResult
//
this.buttonGetResult.Location = new System.Drawing.Point(456, 272);
this.buttonGetResult.Name = "buttonGetResult";
this.buttonGetResult.TabIndex = 1;
this.buttonGetResult.Text = "Clike Me!";
this.buttonGetResult.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(560, 301);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.buttonGetResult,
this.richTextBox1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private int X=0;
private int Y=0;
private void button1_Click(object sender, System.EventArgs e)
{
Point P=new Point(0);
GetCaretPos(ref P);
int Pos=this.richTextBox1.GetCharIndexFromPosition(P);
this.Y=this.richTextBox1.GetLineFromCharIndex(Pos);
if (this.Y>0)
{
int offset=1;
while(this.richTextBox1.Text[Pos-offset]!='\n')
offset++;
this.X=offset;
this.Y++;
}
else
{
this.X=Pos+1;
this.Y=1;
}
MessageBox.Show("当前位置: 第"+this.Y.ToString()+"行 "+"第"+this.X.ToString()+"列");
}
}
}
这个方法问题:在前两行列显示不对
1.由第一行第一列敲1个空格时,仍显示第一列,敲两个空格后,显示第二列,依次类推。
2.由第一行第一列敲1个回车时,仍显示第一行,敲两个回车后,显示第二行,依次类推。
总结:在第二行,第二列时显示第一行,第一列。
3.由第一行回车到第二行时,列数不是回到1.而是又加上了1;
除去前两行后,其他行列都显示正确。
不知道我说明白了没。高手试下看
源码:using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace csTest
{
public class Form1 : System.Windows.Forms.Form
{
[DllImport("user32")]
public static extern bool GetCaretPos(ref System.Drawing.Point lpPoint);
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button buttonGetResult;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.buttonGetResult = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(16, 24);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(528, 232);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// buttonGetResult
//
this.buttonGetResult.Location = new System.Drawing.Point(456, 272);
this.buttonGetResult.Name = "buttonGetResult";
this.buttonGetResult.TabIndex = 1;
this.buttonGetResult.Text = "Clike Me!";
this.buttonGetResult.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(560, 301);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.buttonGetResult,
this.richTextBox1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private int X=0;
private int Y=0;
private void button1_Click(object sender, System.EventArgs e)
{
Point P=new Point(0);
GetCaretPos(ref P);
int Pos=this.richTextBox1.GetCharIndexFromPosition(P);
this.Y=this.richTextBox1.GetLineFromCharIndex(Pos);
if (this.Y>0)
{
int offset=1;
while(this.richTextBox1.Text[Pos-offset]!='\n')
offset++;
this.X=offset;
this.Y++;
}
else
{
this.X=Pos+1;
this.Y=1;
}
MessageBox.Show("当前位置: 第"+this.Y.ToString()+"行 "+"第"+this.X.ToString()+"列");
}
}
}
这个方法问题:在前两行列显示不对
1.由第一行第一列敲1个空格时,仍显示第一列,敲两个空格后,显示第二列,依次类推。
2.由第一行第一列敲1个回车时,仍显示第一行,敲两个回车后,显示第二行,依次类推。
总结:在第二行,第二列时显示第一行,第一列。
3.由第一行回车到第二行时,列数不是回到1.而是又加上了1;
除去前两行后,其他行列都显示正确。
不知道我说明白了没。高手试下看
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货