4

I am a beginner to web application development. I have the code of a windows application. Same functionality i have to convert into a web application. I have a text box control. I am loading some text to that text box. I want to find the current cursor position, line number and column number. The code for the windows application is below:

    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();
        }
    }
Manoj Nayak
  • 2,449
  • 9
  • 31
  • 53

1 Answers1

2

as the accepted answer in this post, you have to use javascript to get the SelectionStart and SelectionEnd in your clinet side. then, post the result (may be using a hidden input value) to the server with the reset of data:

How to get selected text from textbox control with javascript

Community
  • 1
  • 1
Afshin
  • 1,222
  • 11
  • 29
  • what is equivalent method in asp.net, for GetLinefromCharIndex() and GetFirstCharIndexFRomLine() methods in c# – Manoj Nayak Jan 17 '12 at 12:48
  • 1
    I think I need to correct your question to : 'what is the equivalent method in JavaScript?" as ASP.NET in consists of a client-side part and a server side one. in the client side, you can add JavaScript commands to get/set data from/to interface before submitting to the server. I guess you are trying to convert your C# code to JavaScript, I'm not quiet sure if there are any equivalent JavaScript methods for those we have in .NET. I just had a quick look in MSDN and a few Google searches does not show any useful result, may be it's time to change your implementation based on existing methods. – Afshin Jan 17 '12 at 21:20
  • just to give you a good start point: http://www.w3schools.com/jsref/dom_obj_all.asp – Afshin Jan 17 '12 at 21:30