Results 1 to 5 of 5

Thread: "What the hell man?!"-moment in VB

  1. #1

    "What the hell man?!"-moment in VB

    Grand love to all reading, (<- I'm in a really good mood today )

    Well I got a "What the hell man" moment in VB that I thought I'd share. Both for entertainment (I'm bored too) and in hope that this is just some common known thing in VB coding and there is an easy solution to it.

    OK, so here's the situation: I'm writting a RTF viewer that is meant to open a RTF file and display it when a user logs in. It's basically a "Message of the day" type app. The whole program (the editor and viewer) basically work but I got a strange bug that I've narrowed down to one line of code.

    The viewer program has the ability to take in variables through command line in URL-encoded like format.
    Eg: "MOTDViewer.exe -variable=value&etc=ongoing" passes variable=value and etc=ongoing into the viewer.

    These variables are used in the viewer to replace words. The variables are written in the message in the format <#variable>. This is so the message can be made to change slightly when opened and be customised to the individual user.
    Eg:
    Code:
     
    Hello <#name>,
     
    Your computer name is: <#computer>.
    Execute: MOTDViewer.exe -name=Allan&computer=27-Printer Room.
    Output:
    Code:
     
    Hello Allan,
     
    Your computer name is: 27-Printer Room.
    OK NOW FOR THE BUG! For some reason when the program is replacing the <#variables> with the values it's removing the formatting of the message. Specifically it's this function:
    Code:
     
    Msg.Text = Msg.Text.Replace("<#" & Search & ">", Replace)
    If I comment this out, I stop my replacing of the words, but the formatting stays untouched. If I let this run then all the formatting of the text, including font-size, font, boldness, italic .. everything for the whole message is set to the font of the first character in the message.

    Here's a little bit more of the code if your interested:
    Code:
    Status = "Info"
    ' Get the parameters based through command line if available.
    ' URL-Encoded format. Var_1=Val_1&Var_2=Val_2&... etc etc
     
     
     
     
    
    If Len(Command()) > 0 Then
    Try
    Dim Parameters AsString
    Parameters = Mid(Command(), 2, Len(Command()))
    Dim Replacement AsString
    Dim i AsInteger
    Dim Search AsString
    Dim Replace AsString
    For i = 1 To CountWords(Parameters, "&")
    Debug.Print("i=" & i)
    Replacement = FindWord(Parameters, i, "&")
    Search = FindWord(Replacement, 1, "=")
    Replace = FindWord(Replacement, 2, "=")
    If Search = "status" Then
    Status = Replace
    EndIf Msg.Text = Msg.Text.Replace("<#" & Search & ">", Replace)
    Next i
    Catch Ex As Exception
    MsgBox("... something bad happening while trying to insert the parameter words, don't know what though", MsgBoxStyle.OkOnly, "Error... dang.")
    EndTry
    EndIf ' Affects of Status If Status = "MUSTREAD" Then
    Time = My.Computer.Clock.TickCount
    EndIf
    So it works... but replacing text kills the formatting, so like, what the hell man?

    PS: If I'm doing this the wrong way PLEASE tell me, I want to know the right way. I started working at a new job only a couple weeks ago and they've asked me to make this for them. The problem is I come from a mostly C++/PHP background... and I rocked up on the first day to be told "We need you to do it in VB"... I was like "Yea sure, no problem"... and now I'm trying to learn VB and become pro at it in a couple of days. SO ANY HELP IS WELCOME! Please post any suggestions on how to fix this.
    Last edited by aussiedude; February 18th, 2008 at 08:14 PM.

  2. #2
    We'll usually you use regex... but for what your doing its not needed.

    If your editing text that has RTF applied then you need to edit the text on the
    textbox.rtf instead of textbox.text

    If you want to understand it better simply msgbox(textbox.rtf);

    Hmm... if you can msgbox the contents before the replace tell me what it says exactly.

    (meh shouldn't be too hard, rtf syntax highlighter I coded a long time ago probably can be applied here to solve it)
    Last edited by Templarian; February 18th, 2008 at 09:00 PM.

    Join #kirupa | Click the under my Username | CL | Jeff

  3. #3
    I'm afraid it's a bit late.. shortly after posting that I decided "The hell with it!, I'll write my OWN replace function!"... so I did. Works now.... also I didn't keep a copy of the broken version.

    But what you've said makes sense, I'm going to experiment with it right now. I'll write up some test apps and see what results I get from using textbox.text and textbox.rtf.

  4. #4
    YES! That was it! My god I wish I had of actually checked back here instead of just giving up and going to the extreme of writing my own replace function...

    Basically (in only 30mins or so) I just made a RTF editor and gave it a find and replace function. The first time I used .text, then I changed it to .rtf. rtf works, not text.

    Good stuff Templarian, give yourself a gold star.

  5. #5
    np. *Puts star on shirt*

    My little brothers blowing my old little syntax highlighter to smithereens by coding it all advanced and everything (and in C#). rtf is so much fun and simple.

    Join #kirupa | Click the under my Username | CL | Jeff

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012