好像属性里面没有直接可以得到的。
5 个解决方案
#1
调用GetCaretPos来实现。
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(19, 26);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(634, 250);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// buttonGetResult
//
this.buttonGetResult.Location = new System.Drawing.Point(547, 293);
this.buttonGetResult.Name = "buttonGetResult";
this.buttonGetResult.Size = new System.Drawing.Size(90, 25);
this.buttonGetResult.TabIndex = 1;
this.buttonGetResult.Text = "行数列数";
this.buttonGetResult.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(672, 324);
this.Controls.Add(this.buttonGetResult);
this.Controls.Add(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()+"列");
}
}
}
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(19, 26);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(634, 250);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// buttonGetResult
//
this.buttonGetResult.Location = new System.Drawing.Point(547, 293);
this.buttonGetResult.Name = "buttonGetResult";
this.buttonGetResult.Size = new System.Drawing.Size(90, 25);
this.buttonGetResult.TabIndex = 1;
this.buttonGetResult.Text = "行数列数";
this.buttonGetResult.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(672, 324);
this.Controls.Add(this.buttonGetResult);
this.Controls.Add(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()+"列");
}
}
}
#2
再有一个问题 我现在取得了一个空格的位置 我还想取得这一行上一个空格的位置怎么办
#3
up
#4
参考如下代码:
string posinfo="2,16"; //这里假设空格的位置为2行16列 作测试用
Point p=new Point(0);
Point lastp=new Point(0);
p.X=Convert.ToInt32(posinfo.Split(new char[]{','})[0]);
p.Y=Convert.ToInt32(posinfo.Split(new char[]{','})[1]);
this.richTextBox1.GetCharIndexFromPosition(p);
if(this.richTextBox1.Lines.Length >=p.X)
{
string s=this.richTextBox1.Lines[p.X-1];
string a=s.Substring(p.Y-1,1);
int lastpos=s.LastIndexOf(" ",p.Y - 2);
if(lastpos!=-1)
{
lastp.X = p.X;
lastp.Y = lastpos + 1;
MessageBox.Show("上一个空格位置: 第"+lastp.X.ToString()+"行 "+"第"+lastp.Y.ToString()+"列");
}
else
MessageBox.Show("当前行不存在指定条件的空格");
}
else
MessageBox.Show("不存在指定行数据");
string posinfo="2,16"; //这里假设空格的位置为2行16列 作测试用
Point p=new Point(0);
Point lastp=new Point(0);
p.X=Convert.ToInt32(posinfo.Split(new char[]{','})[0]);
p.Y=Convert.ToInt32(posinfo.Split(new char[]{','})[1]);
this.richTextBox1.GetCharIndexFromPosition(p);
if(this.richTextBox1.Lines.Length >=p.X)
{
string s=this.richTextBox1.Lines[p.X-1];
string a=s.Substring(p.Y-1,1);
int lastpos=s.LastIndexOf(" ",p.Y - 2);
if(lastpos!=-1)
{
lastp.X = p.X;
lastp.Y = lastpos + 1;
MessageBox.Show("上一个空格位置: 第"+lastp.X.ToString()+"行 "+"第"+lastp.Y.ToString()+"列");
}
else
MessageBox.Show("当前行不存在指定条件的空格");
}
else
MessageBox.Show("不存在指定行数据");
#5
UP,呵
#1
调用GetCaretPos来实现。
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(19, 26);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(634, 250);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// buttonGetResult
//
this.buttonGetResult.Location = new System.Drawing.Point(547, 293);
this.buttonGetResult.Name = "buttonGetResult";
this.buttonGetResult.Size = new System.Drawing.Size(90, 25);
this.buttonGetResult.TabIndex = 1;
this.buttonGetResult.Text = "行数列数";
this.buttonGetResult.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(672, 324);
this.Controls.Add(this.buttonGetResult);
this.Controls.Add(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()+"列");
}
}
}
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(19, 26);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(634, 250);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// buttonGetResult
//
this.buttonGetResult.Location = new System.Drawing.Point(547, 293);
this.buttonGetResult.Name = "buttonGetResult";
this.buttonGetResult.Size = new System.Drawing.Size(90, 25);
this.buttonGetResult.TabIndex = 1;
this.buttonGetResult.Text = "行数列数";
this.buttonGetResult.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(672, 324);
this.Controls.Add(this.buttonGetResult);
this.Controls.Add(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()+"列");
}
}
}
#2
再有一个问题 我现在取得了一个空格的位置 我还想取得这一行上一个空格的位置怎么办
#3
up
#4
参考如下代码:
string posinfo="2,16"; //这里假设空格的位置为2行16列 作测试用
Point p=new Point(0);
Point lastp=new Point(0);
p.X=Convert.ToInt32(posinfo.Split(new char[]{','})[0]);
p.Y=Convert.ToInt32(posinfo.Split(new char[]{','})[1]);
this.richTextBox1.GetCharIndexFromPosition(p);
if(this.richTextBox1.Lines.Length >=p.X)
{
string s=this.richTextBox1.Lines[p.X-1];
string a=s.Substring(p.Y-1,1);
int lastpos=s.LastIndexOf(" ",p.Y - 2);
if(lastpos!=-1)
{
lastp.X = p.X;
lastp.Y = lastpos + 1;
MessageBox.Show("上一个空格位置: 第"+lastp.X.ToString()+"行 "+"第"+lastp.Y.ToString()+"列");
}
else
MessageBox.Show("当前行不存在指定条件的空格");
}
else
MessageBox.Show("不存在指定行数据");
string posinfo="2,16"; //这里假设空格的位置为2行16列 作测试用
Point p=new Point(0);
Point lastp=new Point(0);
p.X=Convert.ToInt32(posinfo.Split(new char[]{','})[0]);
p.Y=Convert.ToInt32(posinfo.Split(new char[]{','})[1]);
this.richTextBox1.GetCharIndexFromPosition(p);
if(this.richTextBox1.Lines.Length >=p.X)
{
string s=this.richTextBox1.Lines[p.X-1];
string a=s.Substring(p.Y-1,1);
int lastpos=s.LastIndexOf(" ",p.Y - 2);
if(lastpos!=-1)
{
lastp.X = p.X;
lastp.Y = lastpos + 1;
MessageBox.Show("上一个空格位置: 第"+lastp.X.ToString()+"行 "+"第"+lastp.Y.ToString()+"列");
}
else
MessageBox.Show("当前行不存在指定条件的空格");
}
else
MessageBox.Show("不存在指定行数据");
#5
UP,呵