Solved

HTML Editor Project

Posted on 2001-06-16
13
210 Views
Last Modified: 2013-12-16
I had an idea for an html editor project that would add closing tags when you enter the opening tags.
You know, you type <center> and the program changes it to <center> | </center> with the cursor positioned to the right of the opening tab so you can just keep on typing.
I did not get far before I realized I don't know how to proceed. (Yes, I am very much a VB newbie).
Case Select or If statements won't work by themselves, because after you have the first set of tags the text is never going to be only the condition of the selection.
Is there a way to refer to individual lines of text as a new condition, if you know what I mean?
bruwmac  
0
Comment
Question by:bruwmac
  • 8
  • 4
13 Comments
 
LVL 6

Expert Comment

by:sharmon
Comment Utility
Here is a quick idea I came up with to solve this problem a little easier.  You will have to expand on this idea to make it complete but it should get you where you need to be.  For this example create a form and put one textbox on it named Text1 and copy this code to your form.  If you have any questions, please feel free to ask.


Option Explicit

Dim fWatching As Boolean
Dim lngPosA As Long

Private Sub Text1_KeyPress(KeyAscii As Integer)

  If Not fWatching And KeyAscii = 60 Then
   
    lngPosA = Text1.SelStart + 1
    fWatching = True
 
  ElseIf fWatching And KeyAscii = 62 Then
    Dim lngPosSpace As Long
    Dim lngPosB As Long
    Dim strTag As String
 
    lngPosB = Text1.SelStart + 1
    strTag = Mid$(Text1.Text, lngPosA, lngPosB - lngPosA) & ">"
   
    'Clean up the tag to get just the first part,
    'no tag attributes if they exists
    strTag = Mid$(strTag, 2, Len(strTag) - 2)
    strTag = Trim$(strTag)
   
    lngPosSpace = InStr(1, strTag, " ")
    If lngPosSpace > 0 Then
      strTag = Mid$(strTag, 1, lngPosSpace - 1)
    End If
   
    'Insert the close tag
    KeyAscii = 0
    Text1.SelLength = 0
    Text1.SelText = "></" & strTag & ">"
    Text1.SelStart = lngPosB
   
    fWatching = False
 
  End If
End Sub
0
 
LVL 6

Expert Comment

by:sharmon
Comment Utility
Also, as a side note, this will assume that anything within the start of the < and the end of an > is a tag, just so you don't have to update it for certain tags.  But you could easily validate that it is a tag you want to process by adding a function that tests the tag against known tags before it inserts the closing tag.  Also, you would have to evolve it more to watch for things like backspacing over the < or cutting a bunch of text out, etc...Anyhow...just thought I would let you know.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
What a did a long time ago was let the user select the entire text that should be between tags and put those tags with a menu choice (and a toolbar).
I simply copy the entire text to clipboard and on click event of menu, paste the entire text surrounded with the corresponding tags.
Some like this:

clipboard.settext "<center>" & text1.seltext & "</center>"
clipboard.gettext
0
 

Author Comment

by:bruwmac
Comment Utility
First to Richie: Thanks for the additional input. Can't say as I understand how to make it go at the moment, but it gives me another angle to consider.

sharmon: Your code works slick, could you possibly add some more documentation so I can better figure out which part is controlling what. (If it is not askng too much) For instance, which snippet is locating the cursor?
0
 
LVL 6

Expert Comment

by:sharmon
Comment Utility
Sure, give me a few minutes to copy it back to a code editor and then I'll comment it all for you.  Be back in a few minutes.
0
 

Author Comment

by:bruwmac
Comment Utility
Thanks - I think I meant to say caret, not cursor position,but you probably knew what I meant.
I want common tags to be in place when the form loads so I did this:

Private Sub Form_Load()
    'display splash form
    frmSplash.Show
    'preset common tags
     Text1.Text = "<html>" _
    & vbCrLf & "<head>" _
    & vbCrLf & "<title> </title>" _
    & vbCrLf & "</head>" _
    & vbCrLf & "<body>" _
    & vbCrLf & "</body>" _
    & vbCrLf & "</html>"
   
End Sub

Of course the caret is left positioned at this point, which is not very handy. I looked in help for caret control, but I have not had much success finding anything that makes sense (to me). Hopefully the comments you are adding will help.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 6

Expert Comment

by:sharmon
Comment Utility
Option Explicit

Dim fWatching As Boolean
'fWatching is a flag (true/false) I set as soon as
'the text1 keypress event sees the character "<"

Dim lngPosA As Long
'lngPosA is the position the cursor was in the text
'box when we set the fWatching flag to true.

Private Sub Text1_KeyPress(KeyAscii As Integer)

  If Not fWatching And KeyAscii = 60 Then
    'This means we aren't currently watching
    'and the key pressed was "<" (Ascii value
    'of 60)
   
    'If we having set the fWatching flag to
    'true, then set our starting position
    'which would be Text1.SelStart + 1
    'The +1 is because the character
    'hasn't actually been entered into
    'the textbox at this point, it won't
    'be until this routine finishes.
   
    lngPosA = Text1.SelStart + 1
    fWatching = True
 
  ElseIf fWatching And KeyAscii = 62 Then
    'This means we are currently watching
    'and the key pressed was ">" (Ascii value
    'of 62)
 
    'If we have set the fWatching flag to true
    'then we want to test for the ">" to be pressed,
    'so we can process the tag and auto-insert the
    'close tag.

    Dim lngPosSpace As Long
    'lngPosSpace is to test the tag for it having
    'any attributes, since the ending tag will not
    'have attributes, we want to just grab the first
    'word after the beggining of the "<" and get rid of
    'any attributes afterwards, which will be designated
    'by a space before the attributes.  This is so a tag
    'entered like <BODY bgColor=Red> will get parsed to
    'only <BODY>.
   
   
    Dim lngPosB As Long
    'lngPosB is the position the cursor was in the text
    'at this point, we are going to add one to it, because
    'since the character ">" hasn't actually been entered
    'into the textbox yet, (it won't until this TextBox event
    'is finished), we need to grab the position it will be at.
   
    Dim strTag As String
    'strTag is the tag that was entered by the user.  We
    'are going to parse it out to get just the first actual
    'word typed in the tag as to remove any attributes.
   
    lngPosB = Text1.SelStart + 1
    'Grab the current position of the cursor as described above.
   
    strTag = Mid$(Text1.Text, lngPosA, lngPosB - lngPosA) & ">"
    'Now we have the positions of the textbox that the tag was
    'typed in so we are going to just use Mid$ to grab that tag.
    'Mid will get the text in a string from a position to a certain
    'length.  So if the "<" was entered at Position one in the textbox
    'and the ">" was entered in position 10, this function says grab
    'the text from Text1.Text, starting at position 1, and grab 10-1
    'characters.  Then I add the closing tag to it, even though I am
    'just going to strip it back off, I put it there so it would look
    'normal to you if you are debugging.
   
    strTag = Mid$(strTag, 2, Len(strTag) - 2)
    'Now we are going to strip off the "<" at the beginning and the ">"
    'at the end to just get the text between the "<" and the ">"
   
   
    strTag = Trim$(strTag)
    'This will trim the spaces from the right and the left of the tag.
   
   
    lngPosSpace = InStr(1, strTag, " ")
    'Now, we are going to find the first space from the left in the tag
    'this is to strip off any attributes like I described above.
    'So this says, lngPosSpace = First position found of " " in strTag
    'starting at character 1, so lngPosSpace would be the position of the
    'first space found.
   
    If lngPosSpace > 0 Then
     strTag = Mid$(strTag, 1, lngPosSpace - 1)
    End If
    'This says if a space was found at a position, (it will be 0 if a space
    'wasn't found) then use the Mid$ function to get the text from the
    'left all the way to the position before the space was found.
   
    KeyAscii = 0
    'This says not to allow the ">" character that the user pressed
    'to be entered.  The reason for this is that it won't actually
    'get entered until this function is finished, and we don't want
    'it to get put in twice.
   
    Text1.SelLength = 0
    'This says to make sure that the selected text length is 0
    'even though it should be already, just to make sure.  This
    'is done because we are going to basically do an insert of the
    'text by changing the selected text, so we don't have to build
    'a string and set Text1.Text equal to it, it wouldn't be smooth
    'and is not the right way to do it.
   
    Text1.SelText = "></" & strTag & ">"
    'This says that the selected text will change to the string
    'I passed.  Since the selected text is currently nothing, it
    'will just insert it at the current position of the cursor.
   
    Text1.SelStart = lngPosB
    'This will set the cursor position back to between the ><
    'so the user will have a smooth transition to start typing
    'between the tag open and close.
   
    fWatching = False
    'Turn off our fWatching flag (set to false), so that the
    'function will look for "<" to be entered again so this
    'process can start all over.
 
  End If
End Sub

0
 
LVL 6

Expert Comment

by:sharmon
Comment Utility
I tried to comment it like you didn't know much of anything, so forgive me if some of it is too basic for you.  I am just unsure what you need to know.  Also, I am prety decent at making the code, but not the guy to write the documentation, so I write like I think.  Hopefully you will be able to make sense out of it.

Regards,
Shannon
0
 
LVL 6

Expert Comment

by:sharmon
Comment Utility
I understood you about the caret:)
0
 

Author Comment

by:bruwmac
Comment Utility
Shannon,

I have only been at VB for a couple or three weeks, so I really don't know much of anything. I have some other programming knowledge, but even that has not totally gelled. I would like to keep this line open for awhile if you don't mind me bugging you some more. I am sure as I try to develop this thing I will run into confusion.
Thanks for your time.

Bruwmac
0
 
LVL 6

Accepted Solution

by:
sharmon earned 300 total points
Comment Utility
Bruwmac,

You really should close the question if what I have given you is satisfying.  As far as future help with your needs on this, I am happy to help you regarding this matter.  You can feel free to email me via my personal email address, shannonh@theharmonfamily.com

Of course if you want to try to get a better solution that mine, by all means leave it open.

Take care,
Shannon
0
 

Author Comment

by:bruwmac
Comment Utility
Shannon,
You have been more than helpful and I am most likely going to take you up on your email offer.
Thanks again.
Bruwmac
0
 
LVL 6

Expert Comment

by:sharmon
Comment Utility
Your welcome, and like I said, feel free to use my email address for this matter.  The routine will need some other proccessing done on it because you need to watch for some other things like what if the user presses enter, what if they backspace over the position that we started processing, what if they paste text in, etc...  But if it's just for your use it's not a big deal at this point.  You need to now take the code and think of all the things that need to be considered, then write it with all of that it mind.  If you get stuck, feel free to ask.

Shannon
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The purpose of this video is to demonstrate how to make a WordPress Site faster and smaller in size by cleaning up the database. This will be demonstrated using a Windows 8 PC. Plugin WP Optimize will be used. Go to your WordPress login page. T…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now