Solved

HTML Editor Project

Posted on 2001-06-16
13
216 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
ID: 6199144
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
ID: 6199149
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
ID: 6199151
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:bruwmac
ID: 6199190
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
ID: 6199256
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
ID: 6199286
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
 
LVL 6

Expert Comment

by:sharmon
ID: 6199299
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
ID: 6199304
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
ID: 6199312
I understood you about the caret:)
0
 

Author Comment

by:bruwmac
ID: 6199313
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
ID: 6199319
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
ID: 6199322
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
ID: 6199325
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

790 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