Creating a simple text editor (Visual Studio 2008) advice

R

Rob W

Greetings,

I am designing a simple notepad in vb.net (visual studio 2008) for an
assignment.

Initially I thought to use the textbox "TextChanged" event, which I could
then set a global variable of type boolean "Changesmade" to

monitor if a document needs to be saved based on if any text changes have
been made.

Shortly after I realised anytime a document was opened or a new document
initalised this triggered the textbox "textchanged" event.

I then introduced another global variable of type boolean
"DocumentOperation" which would be set to true when a document operation
such as new file or open file had been actioned.

This code allowed to track changes within the textbox ONLY after the initial
document had been opened or initialised (see code below):-

'Ensure changesmade flag is only set when text is changed on a new or opened
document

'and not on an initial opening or creation of document

If DocumentOperation = True Then

DocumentOperation = False

Else

'Set the ChangesMade variable to TRUE as the user has made some change
to the document

ChangesMade = True

End If

To summarise the code above tracks if any changes have been made to the
document to allow the ChangesMade variable to be used to prompt the user to
save the current document on a New, Open or Exit action.

As there are always more than one solution are there any inbuilt methods or
other solutions that are more efficient than the one I choose?

Thanks

Rob
 
C

Cor Ligthert[MVP]

Rob,

Probably drawing one pixel on screen cost more then this kind of code. Don't
spent to time with it.

Cor
 
T

Tom Dacon

Rob W said:
Greetings,

I am designing a simple notepad in vb.net (visual studio 2008) for an
assignment.

Initially I thought to use the textbox "TextChanged" event, which I could
then set a global variable of type boolean "Changesmade" to

monitor if a document needs to be saved based on if any text changes have
been made.

Rob, the TextBox has a Modified property which goes True whenever a change
occurs in the contents of the TextBox, either programmatically or through
user interaction with the control. You can set the Modified property to
False after you've loaded an existing file, thus setting it to its initially
unmodified state, and then test it later. In effect, this works just fine as
what is sometimes called a "dirty bit".

You may have other things you want to do when the user makes a change to the
contents, such as displaying some indication that the contents have been
modified. Some applications, for instance, display an asterisk after the
file name in the title bar when it's "dirty". If you want to maintain such
user interface features, you'll still have to handle TextChanged and do
whatever you want there.

If you want a challenge, use a RichTextBox instead of a TextBox, and support
editing of RTF files. It's an interesting extension of the problem, once
you've surmounted the other challenges such as find/replace.

Tom Dacon
Dacon Software Consulting
 
R

Rob W

Thanks for the idea, when I create a new file or open a file the textChanged
event shows the textbox modified property to be False(I didn't expect this).

Only when something is entered in the textbox is it set to True.
This is very useful and simplifies my source code.

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top