Home > other >  Is there a method, in visual studio forms, that runs when the user clicks on a new position inside o
Is there a method, in visual studio forms, that runs when the user clicks on a new position inside o

Time:12-10

Right now, I'm currently working on a C# project where I'm replicating Notepad. Right now, I'm trying to implement the "Ln 0, Col 0" feature in the status bar in Notepad, so the user knows where they're at in the Notepad. Here's my code for updating the status bar label:

    // Updates the status bar label to display the row and column the user's on
    private void UpdateRowColStatusBar() => CurrentRowAndCol_StatusLabel_NotepadNoteEditor.Text = "Ln "   (TextBody_NotepadNoteEditor.GetLineFromCharIndex(TextBody_NotepadNoteEditor.SelectionStart)   1)   ", Col "   (TextBody_NotepadNoteEditor.SelectionStart - TextBody_NotepadNoteEditor.GetFirstCharIndexOfCurrentLine()   1);

However, I'm not sure how I can implement this method into my form so that it'll run whenever the user updates the text box. I've figured out how to run this method when the user changes the text inside of the text box (via the TextBox_TextChanged method), but I have yet to find a method that runs when the user "clicks" on a new spot in the text box. I've already tried the TextBox_Click method, which doesn't work BTW.

I would really appreciate it if anyone knows the answer to my question, and have a great day.

Just in case I didn't make what feature I wanted to implement clear, I have a photo of what I'm trying to do below: enter image description here

CodePudding user response:

You can either create an event handler for every event that could possibly move the current position, or you can use a RichTextBox instead and use the SelectionChanged event. It still has the SelectionStart property, so your code will still work.

CodePudding user response:

Nevermind everyone, I've figured it out!

So in order to do this, you simply need to implement the UpdateRowAndCol() method that I wrote into a YourTextBoxName_TextChanged(object sender, EventArgs e) and YourTextBoxName_Click(object sender, EventArgs e) methods. HOWEVER you need to make sure that your TextChanged and Click methods are initialized in the InitializedComponent() method.

The file should be named ProjectName.Designer.cs, and then look for these two lines:

// 
// RichTextBox
// 
...
this.RichTextBox.TextChanged  = new System.EventHandler(this.TextBody_NotepadNoteEditor_TextChanged);
this.RichTextBox.Click  = new System.EventHandler(this.TextBody_NotepadNoteEditor_Click);

If you have these methods initialized, you should be able to do a Find Reference command on them (highlight method name, then do ctrl k, then r) and see that your method's referenced inside of the InitializeComponent() method.

CodePudding user response:

Let's create a new project using Visual Studio 2017.

Select New Project-->Visual C#--->Windows Forms App (.NET Framework), give your project a name and click OK.

Let's add a TextBox control to the form by dragging it from Toolbox and dropping it to the form. You will see a TextBox 1 is added to the form. This control is now available to you in the code behind.

Now, let's add another control to the form by dragging the other control from Toolbox to the form. You may also want to change the properties of the other controls. Let's add a Label and a Button control to the form.

You can add a button click event handler by simply double-clicking on the button control.

  • Related