Sign in to follow this  

[.net] How to display line number indicator in a textBox?

This topic is 3589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am developing a text comparer and editor. I use 2 textBoxes. I need to display the line number and also the column number when user is active on one of the textBox. Or both of the textBox.. It is something like the Visual Studio compiler has. Ln 100 Col 4 somekind like that. How to do that? And also if users click Ok on openFileDialog window, the file will be loaded into the text. When the file is loaded, I also want to display the line number, how to do that? Thank you very much..

Share this post


Link to post
Share on other sites
Please don't post multiple related questions. Especially ones that can be solved with minimal research.

One of the text box's properties gives the info you need. You'll have to figure out which one, though :) .

That same property is what you need for another of your questions, too.

Share this post


Link to post
Share on other sites
I have tried like this to display line number indicator:

private: System::Void textBox1_KeyDown(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) {
int lnid = Convert::ToInt32(lnLbl->Text);
int colid = Convert::ToInt32(colLbl->Text);
if ( e->KeyChar == Keys::Up && lnid > 0 ) {
lnid -= 1;
lnLbl->Text = lnid.ToString();
}
else if ( e->KeyChar == Keys::Down && lnid < textBox1->Lines->Length ) {
lnid += 1;
lnLbl->Text = lnid.ToString();
}
else if ( e->KeyChar == Keys::Left && colid > 0 ) {
colid -= 1;
colLbl->Text = colid.ToString();
}
else if ( e->KeyChar == Keys::Right && colid < textBox1->Lines[Convert::ToInt32(lnLbl->Text)]->Length ) {
colid += 1;
colLbl->Text = colid.ToString();
}
}

lnLbl is a toolStripStatusLabel indicating line number,

colLbl is a toolStripStatusLabel indicating column number.

My problem on the last line (RED COLOR) is how to know the column number to stop the cursor from being over the maximum column number, even though I have set the maximum length using

colid < textBox1->Lines[Convert::ToInt32(lnLbl->Text)]->Length

the colid is still messy. What was wrong?

Also, how to update the line number and column number when textBox got focus and lost focus?

I tried for the got focus event here:

private: System::Void textBox1_Enter(System::Object^ sender, System::EventArgs^ e) {
int selStart = textBox1->SelectionStart;
String ^a = textBox1->GetLineFromCharIndex(selStart).ToString();
lnLbl->Text = a;
int ^b = textBox1->GetFirstCharIndexOfCurrentLine().ToString();
}

But the PROBLEM IS selStart is always ZERO valued no matter where I put the CARET.

What was wrong?



Thank you very much..

Share this post


Link to post
Share on other sites
Have you tried:

int line = textBox->GetLineFromCharIndex(textBox->SelectionStart);
int column = textBox->SelectionStart - textBox->GetFirstCharIndexOfCurrentLine();

EDIT: Doh I missed that you're already using that. Regarding your KeyDown handler, I don't think that'll work well when the user begins copy and pasting.

Have you considered using a RichTextBox and updating the line/column labels in the handler for RichTextBox.SelectionChanged?

Share this post


Link to post
Share on other sites
No, I haven't tried it. I will try it soon.

Yah you're right I don't think that it will work after users copy and paste. How to handle that condition?

No, I cannot use the RichTextBox, I don't have much time to change it.

Is there any other alternatives to do with textBox?

Thank you very much.

Share this post


Link to post
Share on other sites

This topic is 3589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this