Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 7th, 2005, 4:56 AM   #1
Mikeshaw
Newbie
 
Join Date: Jun 2005
Location: glasgow
Posts: 4
Rep Power: 0 Mikeshaw is on a distinguished road
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
Mikeshaw is offline   Reply With Quote
Old Jun 7th, 2005, 1:00 PM   #2
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
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.
uman is offline   Reply With Quote
Old Jun 7th, 2005, 2:16 PM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jun 7th, 2005, 2:31 PM   #4
Gilward Kukel
Newbie
 
Join Date: Jun 2005
Location: Vienna, Austria
Posts: 15
Rep Power: 0 Gilward Kukel is on a distinguished road
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.
Gilward Kukel is offline   Reply With Quote
Old Jun 11th, 2005, 12:35 PM   #5
Rory
Expert Programmer
 
Rory's Avatar
 
Join Date: Jan 2005
Location: London
Posts: 542
Rep Power: 4 Rory is on a distinguished road
Send a message via MSN to Rory
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
Rory is offline   Reply With Quote
Old Jun 10th, 2005, 8:18 AM   #6
Mikeshaw
Newbie
 
Join Date: Jun 2005
Location: glasgow
Posts: 4
Rep Power: 0 Mikeshaw is on a distinguished road
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
Mikeshaw is offline   Reply With Quote
Old Jun 11th, 2005, 1:30 AM   #7
Wraith Daquell
Programmer
 
Join Date: Feb 2005
Location: Limbo
Posts: 39
Rep Power: 0 Wraith Daquell is on a distinguished road
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
__________________
The meek will inherit the earth.

-WDaquell
Wraith Daquell is offline   Reply With Quote
Old Jun 11th, 2005, 2:12 AM   #8
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
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".
uman is offline   Reply With Quote
Old Jun 11th, 2005, 6:45 AM   #9
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
In VB and C#, most people use fooBarWibble, so it's generally expected that you do too.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jun 12th, 2005, 8:33 AM   #10
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:58 AM.

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