A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: VB.net question for you...

  1. #1
    Griffhiggins 2.2 clifgriffin's Avatar
    Join Date
    May 2001
    Location
    Your Bathroom! Bwahahaha!
    Posts
    1,383

    VB.net question for you...

    Okay...I know this isn't a development forum, but this is driving me crazy. I have a text box named txtDisplay in a Tab Control named TabControl1 on page 2 named Tab2.

    How do I reference it correctly from the code???

    I keep getting a "Index and length must refer to a location within the string." error.

    This is the code I'm currently using..

    Private Sub DisplayText(ByVal t As String)
    txtDisplay.AppendText(t)
    End Sub
    If I move the text box off of the tab control it works...

    How do I properly reference the text box??

    Thanks in advance!

    Clif

  2. #2
    .NET addict
    Join Date
    Sep 2000
    Location
    St Albans, UK
    Posts
    12
    Im a C# man myself so ive not done this in vb.net - but seeing as theyre all the same im 99.999% sure that what applies here for c# applies for vb.

    In a windows form whether a textbox is in a tab, a panel or right on the form itself the syntax is always:

    this.textBox1.Text = "Hi World";

    so your VB equiv. should be:

    Me.textBox1.Text = "Hi World"

    Go up to the top of your form in code view and double check the name of the textbox, maybe you got it wrong. You can also check this in properties window.

    The "me." bit should be optional unless you maybe have something in local scope with the same name.... using Me. will ensure that the runtime knows you are referencing the global one rather than anything local with the same name.
    C#ley?

  3. #3
    Griffhiggins 2.2 clifgriffin's Avatar
    Join Date
    May 2001
    Location
    Your Bathroom! Bwahahaha!
    Posts
    1,383
    Thanks for the reply!

    Still doesn't work. If I drag the control off of the tab box...it works fine!

    I'm using the microsoft example of using tcp sockets for a chat program...just can't get it to work when the text box is nested in the tab box.

    Anyone else have any idea? Gerbs? Where are you?
    Clif

  4. #4
    .NET addict
    Join Date
    Sep 2000
    Location
    St Albans, UK
    Posts
    12
    You're obviously not a very seasoned bugslayer s'ok ill walk you through it.

    1. VS.NET has a VERY good debugger, I find it a dream to debug in even compared to VS6 standards. Set a breakpoint at or near the problem, and examine the variables and objects when you get to that point - worst case...set the breakpoint when the form loads and slowly step through until the problem occurs - sometimes it can take stepping up to it a few times before you work out what is wrong.

    2. .NET has some great disagnostic features to help you debug your app while its running. Checkout the System.Diagnostics namespace.

    3. When exceptions are thrown a whole range of info is available, from the error message (which you posted) to the Target of the exception(where it was thrown) as well as rich info if it were say a SQLServer error. Most importantly there is a great stack trace which will tell you the audit trail through your code and on what lines of the relevant file we were at. This in itself will usually help you find the soure of the error to set some breakpoints.

    On a less theoretical note, I can reproduce your problem and its as I thought - a local reference to an object of the same name. By the way, what I wrote above was spot on - you refer to a textbox the same no matter what container it is physicxally on the surface of.

    To test I dragged a TextBox to the form called TextBox1. a tab control - whose name is irrelevant, and then i dragged a second textbox to tabPage1 and called it TextBox2. I then wote 2 versions of your function above named AppendText1 & 2 respectively. I wont post all the code that VS puts in for you but the relevant section that I wrote is this (note one commented out line)

    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_
     Handles MyBase.Load
        Me.AppendText1("This is 1")
        Me.AppendText2("This is 2")
    End Sub
    
    Private Sub AppendText1(ByVal s As String)
        'Dim TextBox1 As New TextBox()
        TextBox1.AppendText(s)
    End Sub
    Private Sub AppendText2(ByVal s As String)
        TextBox2.AppendText(s)
    End Sub
    as you can see some text is appended to text boxes 1 & 2 when the page loads. This works fine for both textboxes (remember 2 is in a tab control). Now uncomment that line that declares a new TextBox with the same name as the global one on my form and the exception you posted above is thrown. If you put Me. in front of TextBox1.AppendText(s) then it works fine with the line uncommented.... which is again what I said above.

    What I think your doing is declaring a local variable with the same name, with the intention of referencing the global instance - but somewhere youve used the new keyword when assigning a reference..... but im just second guessing because youve posted no code *hint*

    Still need gerbs?
    Last edited by jamiec; 10-17-2002 at 05:31 PM.
    C#ley?

  5. #5
    Griffhiggins 2.2 clifgriffin's Avatar
    Join Date
    May 2001
    Location
    Your Bathroom! Bwahahaha!
    Posts
    1,383
    Thanks bunches for all your help!

    I've searched for the problem you think is their but can't find it.

    Here's the the VB project itself. http://files.wyoa.com/klip.zip

    If you have time maybe you could glance at it?

    You'll see that the compiler gives it an okay...but upon execution the exception is thrown.

    As far as I can tell, txtDisplay is only used once. I'm sure I'm wrong though.

    I know I'm asking a lot, but I've been working for hours on this.

    The code hasn't been commented all that well, but as far as finding a double instance, I bet a pro like you could do it.

    Thanks for all the work you've done so far!
    Clif

  6. #6
    Griffhiggins 2.2 clifgriffin's Avatar
    Join Date
    May 2001
    Location
    Your Bathroom! Bwahahaha!
    Posts
    1,383
    Here's the exact error message, BTW. (Oh and I used the debugger to find the line that was causing the crash.)

    System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
    Parameter name: length
    at System.String.Substring(Int32 startIndex, Int32 length)
    at System.Windows.Forms.TextBoxBase.set_SelectedText( String value)
    at System.Windows.Forms.TextBoxBase.AppendText(String text)
    at Klip.Form1.DisplayText(String t) in C:\Documents and Settings\Clifton Griffin\My Documents\Visual Studio Projects\Klip\Form1.vb:line 423
    at Klip.Form1.Form1_Load(Object sender, EventArgs e) in C:\Documents and Settings\Clifton Griffin\My Documents\Visual Studio Projects\Klip\Form1.vb:line 368
    at System.Windows.Forms.Form.OnLoad(EventArgs e)
    at System.Windows.Forms.Form.OnCreateControl()
    at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
    at System.Windows.Forms.Control.CreateControl()
    at System.Windows.Forms.Control.WmShowWindow(Message& m)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ScrollableControl.WndProc(Mes sage& m)
    at System.Windows.Forms.ContainerControl.WndProc(Mess age& m)
    at System.Windows.Forms.Form.WmShowWindow(Message& m)
    at System.Windows.Forms.Form.WndProc(Message& m)
    at System.Windows.Forms.ControlNativeWindow.OnMessage (Message& m)
    at System.Windows.Forms.ControlNativeWindow.WndProc(M essage& m)
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


    ************** Loaded Assemblies **************
    mscorlib
    Assembly Version: 1.0.3300.0
    Win32 Version: 1.0.3705.0
    CodeBase: file:///c:/windows/microsoft.net/framework/v1.0.3705/mscorlib.dll
    ----------------------------------------
    Klip
    Assembly Version: 1.0.1020.17865
    Win32 Version: 1.0.1020.17865
    CodeBase: file:///C:/Documents%20and%20Settings/Clifton%20Griffin/My%20Documents/Visual%20Studio%20Projects/Klip/bin/Klip.exe
    ----------------------------------------
    System.Windows.Forms
    Assembly Version: 1.0.3300.0
    Win32 Version: 1.0.3617.0
    CodeBase: file:///c:/windows/assembly/gac/system.windows.forms/1.0.3300.0__b77a5c561934e089/system.windows.forms.dll
    ----------------------------------------
    System
    Assembly Version: 1.0.3300.0
    Win32 Version: 1.0.3617.0
    CodeBase: file:///c:/windows/assembly/gac/system/1.0.3300.0__b77a5c561934e089/system.dll
    ----------------------------------------
    System.Drawing
    Assembly Version: 1.0.3300.0
    Win32 Version: 1.0.3617.0
    CodeBase: file:///c:/windows/assembly/gac/system.drawing/1.0.3300.0__b03f5f7f11d50a3a/system.drawing.dll
    ----------------------------------------
    System.Xml
    Assembly Version: 1.0.3300.0
    Win32 Version: 1.0.3617.0
    CodeBase: file:///c:/windows/assembly/gac/system.xml/1.0.3300.0__b77a5c561934e089/system.xml.dll
    ----------------------------------------
    Accessibility
    Assembly Version: 1.0.3300.0
    Win32 Version: 1.0.3617.0
    CodeBase: file:///c:/windows/assembly/gac/accessibility/1.0.3300.0__b03f5f7f11d50a3a/accessibility.dll
    ----------------------------------------
    Microsoft.VisualBasic
    Assembly Version: 7.0.3300.0
    Win32 Version: 7.00.9447
    CodeBase: file:///c:/windows/assembly/gac/microsoft.visualbasic/7.0.3300.0__b03f5f7f11d50a3a/microsoft.visualbasic.dll

  7. #7
    .NET addict
    Join Date
    Sep 2000
    Location
    St Albans, UK
    Posts
    12
    Firstly, its no problem i was only sitting in front of VS wondering what to do anyway .

    This one had me going for a while - and I still dont understand the problem but I fixed it and thats whats important.

    My first words of advice - pick up C#, and thats from a 4yr VB6 programmer with no c++/low level programming experience at all! It is 10times easier and quicker to develop with than vb.net - the code you produce is much more maintainable and IMHO much more readable. Its the best programming language ive used (ooh out of a total of about 6)

    VB adds this DebbuggerStepthroughAttribute to the InitializeComponents() method which is a real *****. You can set a breakpoint in the method but you cant step through it at all!!! Thats to protect us thick VB programmers from messing up the Form Generator.... C# doesnt add it so I had a heluva time debugging!!! Anyhow, what this attribute does do is lets the debugger pass it, and the proggie to run, and the only error shown is in the task/output window. Look closely when you run the program and youll see a message along the lines of "Close conflicts with base object method Close()" - this is occuring in your InitializeCOmponents method. Basically it was getting to a line that said somethign like

    Me.Close = new System.Windows.Forms.MenuItem()

    on that line it says "hold on, Me.Close is a method in my base class Form which is responsible for shutting down the instance of this form. I cannot have 2 Close methods so ill flag this (as i cant break because i have a supid DebuggerStepThroughAttribute)" - grrr C# would just thrown an exception for me.

    Anyhoo, moral of that story was to watch what you name menu items - we used to use prefixes for things mnuClose txtUsername iInteger sString to avoid problems like this.

    I diverse....

    When i tried this earlier it worked fine - one of my initial thoughts - based on the message - was that if the textbox was empty then maybe AppendText would throw an exception (strictly by definition you cannot append to nothing). However when I tried this on an empty textbox it worked fine.....

    After scratching my head for a minute or so i just changed the line to:

    Me.txtDisplay.Text &= t

    which is of course the same as

    Me.txtDisplay.Text = Me.txtDisplay.Text & t

    not the best of solutions especially if that textbox will be updated frrequently as im sure it will by definition of the fact its a chat window. So more elegant and less intensive:

    Me.txtDisplay.Text = String.Format("{0}{1}", Me.txtDisplay.Text, t)

    At least that does it without forming 2 new strings each time. To be honest either is a horrible way to update live data to a textbox.

    Further word of advice - for a network application you need to be very tight on error management, you cant just consider coding errors but also the hundreds of exceptional circumstances that can cause networked applications to fail (starting with no connection leading to proxy servers, firewalls server outage etc etc etc etc). I can see some Try...Catch in your code but only 1 is doing anything. Determine what should be done with errors - are some recoverable or are all fatal? retrys on certain exceptions?

    Didnt really dig too deep in, the UpdateNow method is a bit dubious - you could read that XMLDoc alot easier if you wrote a separate class that managed all that for you. Other than that it looks OK. Youve written it all in one form and its one object, you may want to get a book on Object Orientated programming (OOP) Im sure you'll benefit from it loads.

    I love the UI it looks cool - thats what my apps are always missing a nice simple good looking design.

    Have a good day - mines almost over.
    C#ley?

  8. #8
    Griffhiggins 2.2 clifgriffin's Avatar
    Join Date
    May 2001
    Location
    Your Bathroom! Bwahahaha!
    Posts
    1,383
    Awesom! Thanks man!

    I was wondering if there was a way to make the program a bit more modular...I'll look into it.

    As this is a beta, I haven't yet taken the time to fully comment it and add error messages and stuff. I added the try and catches to prevent ugly error messages, but that is aabout it.

    Thanks again!
    Clif

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center