Results 1 to 20 of 20

Thread: passing strings to other forms..

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2004
    Posts
    908

    passing strings to other forms..

    Private Sub frmPurchaseRequisition2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim OpentheForm As New form1
    lblWelcome.Text = "Welome" + OpentheForm.txtLogin.Text


    how come i could not retrieve the txtLogin.text from form1?...
    the code above is written under form2_Load.....
    thanks

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This best tech you can do is to create a property in Form1 the exposes your string in the txtLogin . Like this :

    VB Code:
    1. Public Property MyTxtString() As String
    2.         Get
    3.             Return yourtxtbox.text
    4.         End Get
    5.         Set(ByVal Value As String)
    6.             yourtxtbox.text = Value
    7.         End Set
    8.     End Property
    9.  
    10. Then you just call it like you call any other sub .

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Forget about the above code .

    Ok . I've made for you a little demo . It's in VB.NET 2003 . It's commented also . Check it out here :
    Attached Files Attached Files

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

    Sorry to be a pain but I keep missing out on these code snippets you often post.

    When I download them and try to run them I get messages saying they can't be found. Do I have to download them to a specific location?

    Many thanks,
    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.

  5. #5
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252
    Hi taxes,

    Try saving them to the hard disk first, and then Open.

    McoreD

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by taxes
    Hi Pirate,

    Sorry to be a pain but I keep missing out on these code snippets you often post.

    When I download them and try to run them I get messages saying they can't be found. Do I have to download them to a specific location?

    Many thanks,
    If you have any problems while unpacking the file , use winrar . This project was created by VS.NET 2003 . If you don't have this version , use the converter under my sig to get it converted to VS.NET 2002 . No other issues with this sample .

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi ~*McoreD*~ & Pirate,


    I was unpacking to my hard drive.

    I guess I did not realise that I had to unpack them to the directory containing my other VB.NET solutions. Now I've done that, I'm OK.

    I do have VB.NET 2003.

    Many thanks, now I can experiment with Pilate's gems.
    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.

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

    I am having difficulty in following what you are doing.Please tell me if I have misunderstood.

    You have started the project from Form1. So form1 is instantiated from the beginning. Therefore, why in form2 do you use the code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim f1 As New Form1
    Me.Label1.Text = Me.Fm1TxtBox.Text
    End Sub


    Arnt you creating a new instance of form1, in a separate workspace, every time this event occurs? If so, why? I realise that only one form1 will be visible, but the others are still in existance, taking up resources.


    Also, in the form2 code'

    Public Sub New(ByVal hostct As TextBox)
    InitializeComponent()

    Me.Fm1TxtBox = hostct
    End Sub

    you seem to be creating a new instance of a form1.textbox1 (albeit invisible) and then again passing the entire properties thereof to that new instance (Fm1TxtBox).

    Whilst your code does work, it seems a long way round - or is their a reason for this approach?

    Many thanks,
    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.

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by taxes
    Hi Pirate,

    I am having difficulty in following what you are doing.Please tell me if I have misunderstood.

    You have started the project from Form1. So form1 is instantiated from the beginning. Therefore, why in form2 do you use the code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim f1 As New Form1
    Me.Label1.Text = Me.Fm1TxtBox.Text
    End Sub


    Arnt you creating a new instance of form1, in a separate workspace, every time this event occurs? If so, why? I realise that only one form1 will be visible, but the others are still in existance, taking up resources.


    Also, in the form2 code'

    Public Sub New(ByVal hostct As TextBox)
    InitializeComponent()

    Me.Fm1TxtBox = hostct
    End Sub

    you seem to be creating a new instance of a form1.textbox1 (albeit invisible) and then again passing the entire properties thereof to that new instance (Fm1TxtBox).

    Whilst your code does work, it seems a long way round - or is their a reason for this approach?

    Many thanks,
    Forms in .NET are classes and classes are passed ByrRef by default . So When I do this
    Dim f1 As New Form1
    It means , f1 points to the address of Form1 , so anything is changed in f1 obj would reflect in Form1 .


    The other issue with the constructor , I created a paramter to be passed from Form1 to and referenced to Form2 . In Form2 , I stored it in local variable of the same type , then I can use my local var like this

    Me.Label1.Text = Me.Fm1TxtBox.Text

    You need to read more about passing byref and OOP in .NET if this doesn't make sense to you
    clear ?

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

    "You need to read more about passing byref and OOP in .NET if this doesn't make sense to you
    clear ?"

    Yes, I am aware of this and I see that your code is one method of passing data between forms. But you are not answering my questions.

    I see the quicker way, and tying up less resources, as :

    Create an instance of form1 in a module:
    public frm1 as new form1

    Start the project from an Application.Run in a Sub Main in the module.

    In form1:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim f2 As New Form2
    f2.Show()
    End Sub

    In form2:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.Label1.Text = frm1.TextBox1.Text
    End Sub

    Is there a reason why you prefer your way?
    Last edited by taxes; Feb 23rd, 2004 at 04:25 AM.
    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.

  11. #11
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    'Activated' Is there such method in the Form Class ?

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

    No. When I compiled my remarks I was thinking that somewhere it was necessary to see if f2 had previously been instanced and put that in as a reminder for me to see what code could be used. I then forgot to alter my post before submitting.

    My apologies. I have amended it.

    Any views on the main point?

    I have also just noticed your remarks

    "Dim f1 As New Form1
    It means , f1 points to the address of Form1 , so anything is changed in f1 obj would reflect in Form1 . "

    Surely F1 is simply an instance of Form1 and nothing you do to F1 will affect the class from which it is instanced?

    Do you mean:

    so anything is changed in Form1 would reflect in F1 . "

    If you meant to use the variable F1 as a reference to Form1, then shouldn't the declaration omit the "NEW" keyword?
    Last edited by taxes; Feb 23rd, 2004 at 04:41 AM.
    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.

  13. #13
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    It's ok .

    hehe , I understand what all this confusing about . .
    I just forgot to delete this unnecessary line in the click event of Form2 (where I'm trying to get reference of what's been typed in the textbox of Form1) :
    VB Code:
    1. Dim f1 As New Form1

    Though , it's working fine even with that line , but it really makes no big sense .

    sorry for this . Is there something else wrong ?

  14. #14
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Is this all the code for that sub:
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2. Dim f1 As New Form1
    3. Me.Label1.Text = Me.Fm1TxtBox.Text
    4. End Sub

    I didn't look at the sample so I'm probably missing something but in this code here f1 is never used and could be deleted without effecting the code. Since its declared in the method it only has scope for the life of the method and it is never referenced again in the method once its created.

    As a side note the Form object has a property named 'Owner' which can be used to assign a parent/child relationship to the form. It can be referenced in the child via the Owner property and in the Parent via the OwnedForms collection property. If you want to access controls on the forms then you'll need to cast them to the strong type that they are instead of the generic form type. An example of this can be found here.

    EDIT: Sorry Pirate didn't see your last post. I must be typing slow tonight.
    Last edited by Edneeis; Feb 23rd, 2004 at 04:42 AM.

  15. #15
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I didn't look at the sample so I'm probably missing something but in this code here f1 is never used and could be deleted without effecting the code. Since its declared in the method it only has scope for the life of the method and it is never referenced again in the method once its created.
    Yeah , exactly . I just forgot to delete this line . It's my first time I review this sample , so .

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


    I did not think you guys would still be awake!!

    I'm afraid I have edited my last post while you were making your posts.

    Please check my last post again.
    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.

  17. #17
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by taxes
    If you meant to use the variable F1 as a reference to Form1, then shouldn't the declaration omit the "NEW" keyword?
    VB Code:
    1. Dim Frm As Form
    This creates enough space(that fits Form Class) on the stack for Frm variable .

    VB Code:
    1. Frm =New Form1
    Here , we instantiated (created the actual object that holds reference to Form1) .

    This rule depends on the type of class you're working with . For example :
    String Class is sealed (don't know what's called in VB) , which is not inheritable so can't be instantiated .

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

    I'm afraid you are losing me. I will give up and stick with the little I know.

    Thanks for trying.
    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.

  19. #19
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Come on just say it , say I'm the worst teacher in the world .



    I admit it .

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



    Could be I'm the worst learner.

    One of my bosses once said to me

    "I know I told you I did not mind you making a mistake once, but I did not expect you to make EVERY mistake in the book once!!"
    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.

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