Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic .NET (http://www.programmingforums.org/forum19.html)
-   -   html tag extractor problem (http://www.programmingforums.org/showthread.php?t=4342)

Mikeshaw Jun 7th, 2005 4:56 AM

html tag extractor problem
 
Hi,
As youcan probably guess im new here, im also quite new to the whole programming thing but have been beggining to learn vb.net due to the beta being free!

Anyway, Im trying to make a simple application to help making html pages in notepad and im stuck with making a button to extract html tags from a rich text box, remove them from it and put them into another box. this bit of code works fine up until a certain point when it starts returning stuff that quite clearly isnt right ie. if the text in rtb1 is:

<html>
<head>
<title>Michael's site</title>
</head>
<body bgcolor=black text=F0AF link=yellow alink=red vlink=F0AF>
<font face="verdana">

<h1 align=center>Hello and welcome</h1>

at the third click it puts "<title>m" into the second textbox

i cant for the life of me work out whats going on!

<code> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sparvar
Dim strlen
Dim htmlstr
Dim rbloc
Dim lbloc
Dim newvar

rbloc = 0
lbloc = 0
sparvar = ""
htmlstr = ""
strlen = 0

htmlstr = rtb1.Text
strlen = Len(htmlstr)


lbloc = InStr(htmlstr, "<")
rbloc = InStr(htmlstr, ">")
sparvar = Mid(htmlstr, lbloc, rbloc)
htmlstr = Mid(htmlstr, rbloc + 1, strlen)
rtb1.Text = htmlstr
newvar = rtb2.Text
rtb2.Text = newvar + " " + sparvar

End Sub
</code>

Also as a side note, are there any naming conventions for variables and objects?

Thanks

Mike

uman Jun 7th, 2005 1:00 PM

the tags in PFO are delimited by '[' and ']' not '<' and '>'. writing <code> does nothing.


Unfortunately, I don't know VB though, so I don't know how to help you with your problem.

Ooble Jun 7th, 2005 2:16 PM

First of all, specify a type when defining a variable: Dim str As String, etc. Next, you don't always need to declare a variable - you could just as easily write it like this:
:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim lbloc As Int16
        Dim rbloc As Int16
       
        lbloc = InStr(rtb1.Text, "<")
        rbloc = InStr(rtb1.Text, ">")
        rtb2.Text = rtb2.Text + " " + Mid(rtb1.Text, lbloc, rbloc)
        rtb1.Text = Mid(rtb1.Text, rbloc + 1, rtb1.Text.Length)
End Sub


Gilward Kukel Jun 7th, 2005 2:31 PM

I think it's because of the third parameter of the mid-function. It should be the length of the string you want, not the position of the last character.

Mikeshaw Jun 10th, 2005 8:18 AM

Thanks everyone, espcially gilward, i didnt realise that and i think it should sort the problem! gonna nip back off and see if i can sort it.

ta again

Mike

Wraith Daquell Jun 11th, 2005 1:30 AM

If you are new to VB.NET, I would suggest starting to get those old adages concrete before you start another major project:

1) Name variables wisely.
rtb1 could be txtUnstrippedHTML ; lbloc could be strLeftPosition

2) Declare variables with specific types.
Dim rtb1? Better, Dim rtb1 as Integer
This improves speed vastly, and helps to catch errors

3) Initialize variables with the declaration if possible.
Dim rtb1 as Integer = 0 ; Dim lbloc as String = InStr(htmlstr, "<")

4) Even though it is possible, try not to declare variables on the fly.
In a large application with hundreds of variables, this can save hours of debugging.

Just my two cents here. Ignore if need be :)

uman Jun 11th, 2005 2:12 AM

bah. Maybe I'm just too used to the C/C++/other cool languages/etc. culture, but I don't like the "fooBarQuux" type variable names. I much greaterly suggest "foo_bar_quux".

Ooble Jun 11th, 2005 6:45 AM

In VB and C#, most people use fooBarWibble, so it's generally expected that you do too.

Rory Jun 11th, 2005 12:35 PM

Quote:

Originally Posted by Ooble
Next, you don't always need to declare a variable.

Santa won't be giving YOU any presents this year.

Add an Option Explicit line and let's pretend this never happened :)

Ooble Jun 12th, 2005 8:33 AM

Erm... read my code. By that, I meant that instead of recording things in weirdly-named variables, he could just plug them straight into the functions he was using them in.


All times are GMT -5. The time now is 2:02 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC