2012-01-17 81 views
4

我是一个Web应用程序开发的初学者。我有一个Windows应用程序的代码。同样的功能,我必须转换成一个Web应用程序。我有一个文本框控件。我正在将一些文本加载到该文本框中。我想找到当前的光标位置,行号和列号。对于Windows应用程序的代码如下:找到文本框光标位置,行号。和列号。在asp.net

private void Form1_Load(object sender, EventArgs e) 
    { 
     textBox1.Text = @"This is a demo for text box control 
         I am trying to find the cursor position , 
         line no and column no 
         of the cursor.";   


    } 

    private void textBox1_Click(object sender, EventArgs e) 
    { 
     textBox1.SelectionStart++; 
     label2.Text = textBox1.SelectionStart.ToString(); 
     int i = textBox1.GetLineFromCharIndex(textBox1.SelectionStart); 
     label3.Text = i.ToString(); 
     int j =textBox1.SelectionStart - textBox1. GetFirstCharIndexFromLine(i); 
     label4.Text = j.ToString(); 
    } 

    private void textBox1_KeyDown(object sender, KeyEventArgs e) 
    { 
     if ((e.KeyCode == Keys.Up) || (e.KeyCode == Keys.Right) || 
      (e.KeyCode == Keys.Left) || (e.KeyCode == Keys.Down)) 
     { 
      textBox1.SelectionStart++; 
      label2.Text = textBox1.SelectionStart.ToString(); 
      int i = textBox1.GetLineFromCharIndex(textBox1.SelectionStart); 
      label3.Text = i.ToString(); 
      int j = textBox1.SelectionStart - 
        textBox1.GetFirstCharIndexFromLine(i); 
      label4.Text = j.ToString(); 
     } 
    } 
+1

你将不得不使用JavaScript来获得相同的功能。由于选择是客户端,并且该代码将在服务器端。 – NoLifeKing 2012-01-17 06:58:47

+0

http://stackoverflow.com/questions/263743/how-to-get-cursor-position-in-textarea – 2012-01-23 12:28:51

回答

2

在本post接受的答案,你必须使用JavaScript来让你的客户为例侧SelectionStart和选定结束。然后,发布结果(可以使用隐藏的输入值)与数据的复位服务器:

How to get selected text from textbox control with javascript

+0

什么是在asp.net中的等效方法,用于GetLinefromCharIndex()和GetFirstCharIndexFRomLine()方法在c# – 2012-01-17 12:48:14

+1

我认为我需要更正你的问题:'什么是JavaScript中的等价方法?'作为ASP.NET由客户端部分和服务器端部分组成。在客户端,您可以添加JavaScript命令来获取/设置数据从接口发送到服务器之前,我想你正试图将你的C#代码转换成JavaScript,我不确定是否有任何对应的JavaScript方法用于我们在.NET中的使用。看看MSDN和几个谷歌搜索没有显示任何有用的结果,可能是现在的方法来改变你的实现。 – Afshin 2012-01-17 21:20:05

+0

只是为了给你一个好的开始点:http://www.w3schools.com/jsref/ dom_obj_all.asp – Afshin 2012-01-17 21:30:38