Lock a textbox and richtextbox together
-
If that makes any sense... I have a textbox to the left of a richtextbox. The textbox displays line numbers of the richtextbox. As you type into the richtextbox, line numbers appear in the textbox. If you type beyond the limits of the form, the numbers continue to roll with the flow, so to speak. However, if you scroll back up in the richtextbox, the textbox doesn't follow. How can I lock the two together so they move with each other up or down?
-
If that makes any sense... I have a textbox to the left of a richtextbox. The textbox displays line numbers of the richtextbox. As you type into the richtextbox, line numbers appear in the textbox. If you type beyond the limits of the form, the numbers continue to roll with the flow, so to speak. However, if you scroll back up in the richtextbox, the textbox doesn't follow. How can I lock the two together so they move with each other up or down?
What have you already done ? It seems to me that there's no 'lock' property, you need to write the code to do it, so you need to post the code you've written so we can see what's wrong with it
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
What have you already done ? It seems to me that there's no 'lock' property, you need to write the code to do it, so you need to post the code you've written so we can see what's wrong with it
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
I haven't written any code for "locking" the two together. I have searched for relevant code snippets and ideas to do this, but haven't found any. I'm not asking anyone to write it for me -- I can't learn this stuff if every time I need help someone just does it for me. I just need some pointers. Thanks...
-
I haven't written any code for "locking" the two together. I have searched for relevant code snippets and ideas to do this, but haven't found any. I'm not asking anyone to write it for me -- I can't learn this stuff if every time I need help someone just does it for me. I just need some pointers. Thanks...
"If you type beyond the limits of the form, the numbers continue to roll with the flow, so to speak." How is this working then ? Or did I misunderstand and this is a description of what you want, not what you have ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
"If you type beyond the limits of the form, the numbers continue to roll with the flow, so to speak." How is this working then ? Or did I misunderstand and this is a description of what you want, not what you have ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Ah, ok...I misunderstood what you were asking. See the code below that creates the line numbers in textbox1: Here's the code that does the work:
private void updateNumberList() { Point pos = new Point(0, 0); int firstIndex = richTextBox1.GetCharIndexFromPosition(pos); int firstLine = richTextBox1.GetLineFromCharIndex(firstIndex); pos.X = ClientRectangle.Width; pos.Y = ClientRectangle.Height; int lastIndex = richTextBox1.GetCharIndexFromPosition(pos); int lastLine = richTextBox1.GetLineFromCharIndex(lastIndex); pos = textBox1.GetPositionFromCharIndex(lastIndex); textBox1.Text = ""; for (int i = firstLine; i <= lastLine; i++) { textBox1.Text += i + 1 + " \\r\\n"; } }
Here's the code that calls it:
private void richTextBox1\_TextChanged(object sender, EventArgs e) { updateNumberList(); }
-
Ah, ok...I misunderstood what you were asking. See the code below that creates the line numbers in textbox1: Here's the code that does the work:
private void updateNumberList() { Point pos = new Point(0, 0); int firstIndex = richTextBox1.GetCharIndexFromPosition(pos); int firstLine = richTextBox1.GetLineFromCharIndex(firstIndex); pos.X = ClientRectangle.Width; pos.Y = ClientRectangle.Height; int lastIndex = richTextBox1.GetCharIndexFromPosition(pos); int lastLine = richTextBox1.GetLineFromCharIndex(lastIndex); pos = textBox1.GetPositionFromCharIndex(lastIndex); textBox1.Text = ""; for (int i = firstLine; i <= lastLine; i++) { textBox1.Text += i + 1 + " \\r\\n"; } }
Here's the code that calls it:
private void richTextBox1\_TextChanged(object sender, EventArgs e) { updateNumberList(); }
OK - the problem is probably to do with GetCharIndexFromPosition. I don't see why you're doing that. You should be able to get the index from the cursor position, which is retrieved from the selection start property. If you step through the code when you step back, you should see which line is not giving the value you'd hope, but I do think that's the way to get the correct index, all the time.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
If that makes any sense... I have a textbox to the left of a richtextbox. The textbox displays line numbers of the richtextbox. As you type into the richtextbox, line numbers appear in the textbox. If you type beyond the limits of the form, the numbers continue to roll with the flow, so to speak. However, if you scroll back up in the richtextbox, the textbox doesn't follow. How can I lock the two together so they move with each other up or down?
Instead of scrolling the textbox, find the number of the first visible line in the richtextbox.Say if it is 15 and the text box can show only 'n' numbers, textBox1.text=""; for i=15 to 15+n textBox1.Text+=i+"\n";
Regards, Arun Kumar.A
-
If that makes any sense... I have a textbox to the left of a richtextbox. The textbox displays line numbers of the richtextbox. As you type into the richtextbox, line numbers appear in the textbox. If you type beyond the limits of the form, the numbers continue to roll with the flow, so to speak. However, if you scroll back up in the richtextbox, the textbox doesn't follow. How can I lock the two together so they move with each other up or down?
Hi! I'm not sure if I understand correctly what you try to achieve, but there a quite useful article about line numbers for the RichTextBox[^] here on CP.
Regards, mav -- Black holes are the places where God divided by 0...