12 Replies - 23616 Views - Last Post: 12 March 2008 - 06:13 PM Rate Topic: -----

#1 gram999  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 89
  • Joined: 21-January 08

Pass Data Between Two Forms

Posted 11 March 2008 - 12:40 PM

I am expirimenting with passing data between two VB.Net forms and need some assistance. I am using a constructor method to pass data from a textbox on Form1 to a label on Form2. I have a very short piece of code below and have been able to get data passed between two forms without a problem. What I am stuck on is I am trying to get a little more sophisticated and have a message box appear on the CommandButton Click Event that asks if the user wants to add more items. If the answer is "Yes" I would like to either run a loop and write to the next empty label on Form2 or ideally add a new label on Form2 and then write to it. Here is the code that I currently have:


CODE FOR FORM1
 

Public Class Form1

	Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
		Dim frm As Form2 = New Form2(txtfrm1.Text)
		Dim StrNote As String


		StrNote = MessageBox.Show("Add another item?", "My Application", _
		 MessageBoxButtons.YesNo, MessageBoxIcon.Question)

		If StrNote = DialogResult.Yes Then txtfrm1.Focus()

		If StrNote = DialogResult.No Then 

'????? I AM WORKING ON THIS PART RIGHT NOW

			frm.Show()
	End Sub
End Class




CODE FOR FORM2


Public Class Form2

	Public Sub New(ByVal strTextBox As String)
		InitializeComponent()
		Label1.text = strTextBox
	End Sub
End Class




All suggestions and guidance are greatly appreciated. I am continuing to work on this as well so if I figure it out I will post the update. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Pass Data Between Two Forms

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Pass Data Between Two Forms

Posted 11 March 2008 - 01:24 PM

If it were me doing this project I don't think I'd use the constructor method, first because it's not needed in VB.Net. Unlike C#, in VB.Net you can access the controls on a different form without having to create an instance of it.

Second because each time it runs, you new constructor is going to overwrite what is already in the label with the latest text value.

If it were me Id create a form scope global variable, then a separate function which will add to my global, then check whether the user selected Yes or No, then act accordingly, something like (I commented what I was doing so you can see it) :


Public Class Form1
    Private items As String = String.Empty
    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        'Create a DialogResult variable
        Dim StrNote As DialogResult
        Dim str As String
        'Get the value from the MessageBox
        StrNote = MessageBox.Show("Add another item?", "My Application", _
         MessageBoxButtons.YesNo, MessageBoxIcon.Question)

        'Check to see if the user selected Yes or No
        If StrNote = DialogResult.Yes Then
            'Add our items
            str = SetValue(items)
            'Empty the TextBox
            txtFrm1.Text = String.Empty
            'Set the focus to the TextBox
            txtFrm1.Focus()
        Else
            str = SetValue(items)
            'Set the Text property of our Label to the items variable
            Form2.Label1.Text = str
            Form2.Show()
        End If
    End Sub

    Public Function SetValue(ByVal value As String) As String
        If String.IsNullOrEmpty(value) Then
            'No value present so add a value
            items = txtFrm1.Text + Environment.NewLine
        Else
            'Value exists so append the new value
            items += txtFrm1.Text + Environment.NewLine
        End If
        Return items
    End Function
End Class





I also had to add no code to Form2 for this to work :) Hope that helps
Was This Post Helpful? 0
  • +
  • -

#3 gram999  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 89
  • Joined: 21-January 08

Re: Pass Data Between Two Forms

Posted 11 March 2008 - 01:41 PM

Thanks Psychocoder! Definitely helps. I will probably be back with questions for you after I decipher what you have above. In C# I've used either the constructor, properties or delegates methods since they are the most straight forward for passing data so I have been playing around with those three approaches in VB to see what works and what does not. The constructor seemed to work pretty smoothly until, as I found out, you want to start getting a little more complex. Thanks again!
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Pass Data Between Two Forms

Posted 11 March 2008 - 02:06 PM

No problem, glad I could help. Yeah things are a bit different in C#. In c# you cant just access the controls on a different form directly, what I do is I make the modifier for the controls I need to access public, then create an instance of the other form, then I have access to those controls I need.
Was This Post Helpful? 0
  • +
  • -

#5 gram999  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 89
  • Joined: 21-January 08

Re: Pass Data Between Two Forms

Posted 12 March 2008 - 10:14 AM

Hey Psychocoder. I've got a quick question for you on your code above. Why did you add the Private items As String = String.Empty before the command button click event? Just curious.
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Pass Data Between Two Forms

Posted 12 March 2008 - 10:16 AM

That is a form level global that I use for the text that is being sent to the 2nd form. I did it that way so I can add to it if the user chooses to add more items. If you declare it inside a method it will get overwritten every time they add an item.

I set it to String.Empty so it's initialized when I go to use it, keeps from getting a NullReferenceException

This post has been edited by PsychoCoder: 12 March 2008 - 10:17 AM

Was This Post Helpful? 1

#7 gram999  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 89
  • Joined: 21-January 08

Re: Pass Data Between Two Forms

Posted 12 March 2008 - 11:02 AM

Thanks for the explanation. Sorry to be a pain but I have one more quick question. In the part where you check the message button selection for a Yes or No click by the user you set
str = SetValue(items) In the 2008 version of VB.Net it does not recognize the SetValue (sees it as 'not declared) If it is typed Set Value (Items) It is expecting an expression. Since you declared it above (Dim str As String) I am a bit puzzled as to why it is being viewed as 'not declared'.
Was This Post Helpful? 0
  • +
  • -

#8 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Pass Data Between Two Forms

Posted 12 March 2008 - 11:45 AM

You really don't need to use SetValue, as far as I know that is just used to assign a value to an array.

Replace it with:
str = items

Was This Post Helpful? 0
  • +
  • -

#9 gram999  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 89
  • Joined: 21-January 08

Re: Pass Data Between Two Forms

Posted 12 March 2008 - 12:08 PM

Thanks Jayman. That took care of that one. Now just one last question. In the section where it is checking Yes or No the code reads:


Else
str = SetValue(items)
'Set the Textproperty of our label to the items variable
Form2.Label.Text =str
Form2.Show()

End If




For some reason I am getting the following error: "reference to a non-shared member requires an object reference for these two lines:

Form2.Label1.Text =str
Form2.Show()

I there a form level global line that I need to add in the Public Class to so the individual elements on Form2 are recognized?

Never mind. I figured it out. I had a piece of code on Form2 that was throwing everything off. Thanks for the help though. Greatly appreciated. :D
Was This Post Helpful? 0
  • +
  • -

#10 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Pass Data Between Two Forms

Posted 12 March 2008 - 02:28 PM

If you would have looked at all code I offered there was a function named SetValue that I was using to keep track of what was in the string being passed to the 2nd form :)
Was This Post Helpful? 0
  • +
  • -

#11 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Pass Data Between Two Forms

Posted 12 March 2008 - 03:43 PM

Too funny...I didn't even look at your code when I replied to him, otherwise I would have told him about the SetValue function you created.

I just assumed it was the Array.SetValue method. That is what I get for making assumptions. :(
Was This Post Helpful? 0
  • +
  • -

#12 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Pass Data Between Two Forms

Posted 12 March 2008 - 04:51 PM

No problem Jay, that wasn't directed at you in any way lol :)
Was This Post Helpful? 0
  • +
  • -

#13 gram999  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 89
  • Joined: 21-January 08

Re: Pass Data Between Two Forms

Posted 12 March 2008 - 06:13 PM

Sorry for the confusion guys. Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1