Results 1 to 14 of 14

Thread: more confused than before.. such a simple probme

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    Ma
    Posts
    19

    Angry more confused than before.. such a simple probme

    Ive tried searching, and Ive tried everything ive found, and I cant get anything to work

    I need to do this-

    A click event on form 2 will change a textbox's text on form1.

    On form 2:

    public frm1 as form1


    Private Sub BtnGo_click (blah balh blah) handles btnGo.click

    frm1.textbox1.text = "hello"

    End Sub


    From what Ive read, shouldnt this work?

  2. #2
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    that should work. post the exact code you're using please.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    Ma
    Posts
    19
    in form 2, i also tried:


    public frm1 as form1

    private sub btnGo_click (blah.....) handles btnGo.click

    txtBoxonForm2.text = "hello"

    frm1.textbox1.text = txtBoxOnForm2.text

    end sub




    help

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    Ma
    Posts
    19
    Originally posted by Andy
    that should work. post the exact code you're using please.
    On form1:

    VB Code:
    1. Public frm5 As Form5
    2.  
    3.   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Dim form5 As New Form5
    5.  
    6.         form5.Show()
    7.  
    8.     End Sub


    Form 5 loads, with a textbox, I want to change the text in the box using a button on form1.

    VB Code:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.  
    3.                 frm5.TextBox1.Text = "hello"
    4. End Sub



    I get this error message:

    Object reference not set to an instance of an object.

  5. #5
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    Why is it that you are using Frm5 instead of Form5 here:

    VB Code:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.  
    3.                 frm5.TextBox1.Text = "hello"
    4. End Sub

    ??? ???

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    Ma
    Posts
    19
    Originally posted by squirrelly1
    Why is it that you are using Frm5 instead of Form5 here:

    VB Code:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.  
    3.                 frm5.TextBox1.Text = "hello"
    4. End Sub

    ??? ???

    Squirrelly1
    I was using it because I set that as the name in public, so I thought I had to?

  7. #7
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    Button 2 is on your Form1, right?


    Declare the Form5 object as Friend or something like that at the top of your Form1.. initialize it then too...

    Then... open it whenever you want to...

    At the Button2 click event, make sure you use the same name for the Form5 object as you declared earlier...


    If you're actually using frm5 in the button2 click event and Form5 when you set it to be an object, then you will get the error that you reported...

    then just use the same command that you were using to set the textbox...

    form5.textbox1.text = "whatever"

    that should do it...

    ~Squirrelly1

    btw... 9/10 times it's something simple that we just wouldn't expect to be there that keeps our software from running correctly... you'll scratch your head thinking about the huge algorithms and such and not even notice that you left a piece of code commented or you forgot a closing brace...
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  8. #8

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    Ma
    Posts
    19
    VB Code:
    1. Friend form5
    2.     Public frm5 As Form5

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim form5 As New Form5
    3.  
    4.  
    5.         form5.Show()
    6.  
    7.  
    8.     End Sub
    9.  
    10.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    11.         Dim form5 As Form5
    12.  
    13.         form5.TextBox1.Text()
    14.  
    15.     End Sub


    Still doesnt work...
    What am I doing wrong?

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Why do you keep dimming Form5?
    It should be dimmed once, loaded once, and be done with it.

    VB Code:
    1. Public frm5 As Form5
    2.  
    3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         frm5 = New Form5
    5.  
    6.  
    7.         frm5 .Show()
    8.  
    9.  
    10.     End Sub
    11.  
    12.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    13.  
    14.         frm5 .TextBox1.Text()
    15.  
    16.     End Sub


    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    In your last example, you've got several instances of form5, although only one is instantiated.
    You could declare and instantiate the form in a module. Alternatively, in your form:
    VB Code:
    1. 'Form level declaration
    2. Dim frm5 as New Form5
    3.  
    4. Private Sub Button1_Click(ByVal......)
    5.    frm5.Show
    6.    frm5.Textbox1.Text = "Hey! Look at me!"
    7. End Sub
    This assumes you actually have a form in your project named Form5.
    If you're just referencing frm5 from this form, you don't need to declare it Public. If you declare it in a module, then declare it Public so you can reference it from other forms.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    Ma
    Posts
    19
    Do I need the frm5 = new form 5 in the button2 click event?

    or can I do this:
    VB Code:
    1. Friend form5
    2.     Dim frm5 As New form5
    3.  
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.  
    7.         frm5.Show()
    8.  
    9.  
    10.     End Sub
    11.  
    12.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    13.  
    14.         frm5.TextBox1.Text = "TTHH"
    15.     End Sub

    Cause that works too

    Thanks guys.

    -Nick


    Originally posted by techgnome
    Why do you keep dimming Form5?
    It should be dimmed once, loaded once, and be done with it.

    VB Code:
    1. Public frm5 As Form5
    2.  
    3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         frm5 = New Form5
    5.  
    6.  
    7.         frm5 .Show()
    8.  
    9.  
    10.     End Sub
    11.  
    12.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    13.  
    14.         frm5 .TextBox1.Text()
    15.  
    16.     End Sub


    TG

  12. #12
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    You don't need the Friend line.
    If you dim frm5 as New Form5 again in button2, you'll get a second instance of Form5, a new, separate Form5. That's probably not what you want.
    There's plenty of threads on this subject. You could do a search if we're not explaining it well for you.

  13. #13
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Have a look at the post by blue_rabbit in thread
    http://www.vbforums.com/showthread.p...hreadid=288940
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  14. #14
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    Dude, it's pretty simple...

    You only have to declare and initialize a Global (object OR variable) one time... and then use it from anywhere... Think about that, look at ur code, and you'll get it.

    Squirrelly1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

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