Sign in to follow this  

A few windows forms questions

This topic is 2842 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

In regard to C#, windows forms. 1. How would I go about changing the colour of text in a textbox for each word? For example, Im wanting to do some simple syntax highlighting. Something like, - if the current word being drawn, is a word from this array, set colour to blue - if the current word being drawn, is a word from this array, set colour to red - if the current word being drawn, is a word from this array, set colour to green else - set colour to black I gather than I'll need to catch, and overide the drawing message for this text box? But what to do from there? 2. How do I keep my text boxes always fully scrolled down? So that when there is enough text causing the vertical scrollbar to become visible, it is always scrolled far enough so that the last line added is visible?

Share this post


Link to post
Share on other sites
Quote:
Original post by maya18222
1. How would I go about changing the colour of text in a textbox for each word? For example, Im wanting to do some simple syntax highlighting.
Use a RichTextBox rather than a TextBox.

You can then select blocks of text, and use SelectionFont, SelectionColor, and other similar methods to modify the way it appears.

Quote:
2. How do I keep my text boxes always fully scrolled down? So that when there is enough text causing the vertical scrollbar to become visible, it is always scrolled far enough so that the last line added is visible?
If you are simply appending text, AppendText() will usually do the right thing for scrolling. Normally the right thing is to scroll down if they were already at the end, or stay where they were if they weren't at the end.

Share this post


Link to post
Share on other sites
Thanks for the reply. I managed to implement what you suggested, but Im having a few problems. Currently, I have a function named, UpdateSyntax(), which when called, goes through the text in the richtextbox, selecting and changing font colour. Atm it gets called whenever a key is pressed in the textbox, but I may changing this to some sort of timer.

Now, this all works fine, until the text box is scrolled down, due to the user being at the bottom of the page. When UpdateSyntax() is now called, the select method, causes the window to scroll back up to the selection point. Any ideas how to solve this issue?

And as a side note. How would I go about manually scrolling a text box? Im wanting it so that, whenever some specific textbox is scrolled, the same scrolling also happens in another textbox.

Share this post


Link to post
Share on other sites
Quote:
Original post by maya18222
Now, this all works fine, until the text box is scrolled down, due to the user being at the bottom of the page. When UpdateSyntax() is now called, the select method, causes the window to scroll back up to the selection point. Any ideas how to solve this issue?
Several, but I'll only share the best one. :-)

Call SetWindowRedraw on the window. (C++ specific? Maybe BeginUpdate() or equivalent in the class.) Store the selection so you don't upset your user. Copy the text from the box into an RTF string. UpdateSyntax on the rtf string. Set the RTF text. Restore the selection. Clear the SetWindowRedraw flag / EndUpdate().

This will be *MUCH* faster than trying to update it piecewise while it remains inside the control, which at wost could cause a redraw every time you changed it. Icky.

Out of curiosity, have you looked for implementations of this? It seems this implementation or several others could save you hours of grief.

Quote:
And as a side note. How would I go about manually scrolling a text box? Im wanting it so that, whenever some specific textbox is scrolled, the same scrolling also happens in another textbox.


There's the events WM_VSCROLL and WM_HSCROLL. I'm not sure of the C# equivalent, other than using p/invoke to send the messages. Basically it is a message to scroll by n lines.

Share this post


Link to post
Share on other sites
My next question is on implementing Redo/undo. Ive noticed that the RichTextBox contains its own methods for these. But As soon as I add a menu the keyboard shortcuts for the richtextbox dont work, due them being overidden by the menu ones. Which is fine. But Im now wondering how I get that functionality back. I know I can just remove the shortcuts from the menu, but I kinda want to keep that. So I thought about this approach.


void FunctionUndo() // Gets called when user selects undo from menu or hits cntl/z
{
// Get Control that has the focus
Control c = ActiveControl();

if( c.HasUndo() )
c.Undo();
}

void FunctionCopy() // Gets called when user selects Copy from menu or hits cntl/c
{
// Get Control that has the focus
Control c = ActiveControl();

if( c.HasCopy() )
c.Copy();
}

//etc etc



But I cant seem to find anything like this in the Control type. SO how would I go about doing this?

Share this post


Link to post
Share on other sites
I suppose you could use reflection:

void FunctionUndo() {
var c = this.ActiveControl;
if (c != null) {
var undoMethod = c.GetType().GetMethod("Undo");
if (undoMethod != null) undoMethod.Invoke(c, null);
}
}

An alternative would be to hard-code support for different controls:

void FunctionUndo() {
var c = this.ActiveControl;
if (c is RichTextBox) {
((RichTextBox)c).Undo();
} else if (c is /* ... */) {
((/* ... */)c).Undo();
}
}

If you're just going to support the RichTextBox, you could just do this:

void FunctionUndo() {
var c = this.ActiveControl as RichTextBox;
if (c != null) c.Undo();
}

Share this post


Link to post
Share on other sites

This topic is 2842 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