c# - 查找文本框光标位置,行号。和列号在 asp.net 中

标签 c# asp.net

我是网络应用程序开发的初学者。我有一个 Windows 应用程序的代码。我必须将相同的功能转换为 Web 应用程序。我有一个文本框控件。我正在向该文本框加载一些文本。我想找到当前光标位置、行号和列号。 Windows 应用程序的代码如下:

    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();
        }
    }

最佳答案

作为此 post 中接受的答案,您必须使用 javascript 在客户端获取 SelectionStart 和 SelectionEnd。然后,通过重置数据将结果(可能使用隐藏的输入值)发布到服务器:

How to get selected text from textbox control with javascript

关于c# - 查找文本框光标位置,行号。和列号在 asp.net 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8890749/

相关文章:

c# - 使用 if 语句将组合框值与两个文本框值进行比较

C# 算法时间复杂度

c# - asp.net mvc 5异步 Action 方法

css - 在《Professional ASP.NET 4.5 in C# and VB》一书中,为什么CSS相邻选择器定义与示例不匹配?

c# - .NET C# - 将父控件传递给分页 .ascx 控件?

c# - 大型客户端/服务器项目布局和 Ivy 设置的建议

c# - 一个对象订阅自己的事件改变内部数据可以吗?

c# - 如何将图像设置为现有 pdf 文件中的 pdf 字段?

c# - WebClient 默认超时?

c# - Fedex 服务集成错误(无法生成临时类)