By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,289 Members | 1,457 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,289 IT Pros & Developers. It's quick & easy.

Passing info from one forms code to the other

P: 14
This is Visual Basic 2008:

I have an application where I have the main textbox on the main form, and the search window on a seperate form, and whenever someone clicks the mouse in the main forms text, I want the search window to detect that change so that if during a search, someone want to go back and change something, they can just click the mouse in the textbox and restart the search from that location. (So I am just trying to update the seach start location whenever someone clicks the mouse in the text.) Problem is, I don't know how to pass information from one forms code to another forms code. I have the frmMain, which has the search text and I have the frmSearch that has all the search controls on it.
Expand|Select|Wrap|Line Numbers
  1. '  Get the cursor location when someone clicks in the text box
  2. Private Sub RichTextBox1_Click(bla bla bla) Handles RichTextBox1.Click
  3.          CursorLocNow = Me.RichTextBox1.SelectionStart
  4.          cursorLocationChanged = True
  5. End Sub
Now I have to have some way to pass these two data items to the second form, and so far everything I have tried has failed. Any suggestions?
Jun 3 '09 #1
Share this Question
Share on Google+
6 Replies


Frinavale
Expert Mod 5K+
P: 9,731
Have you ever used Properties before?

Public Properties let you expose data in one Form or Object to other Forms or Objects.

For example, say you want to expose the Text in the Search TextBox in your main Form to your second From. You could create a Public Property (public because if you give anything a Public scope it makes it available to other Objects) that exposes the SearchText:
Expand|Select|Wrap|Line Numbers
  1. Public Property SearchText As String
  2.   Get
  3.     return Me.RichTextBox1.Text 'Or whatever you need to return
  4.   End Get
  5.   Set(ByVal value As String)
  6.      Me.RichTextBox1.Text  = value
  7.   End Set
  8. End Property
So now in your second form you can access the data in the main form simply using the Form1.SearchText.


You can use the PropertyChangedEvent to indicate to all Objects that this property was changed.

Or if you feel like learning things the hard way you can raise an event whenever the text in the search text box changes. You'd create a Custom EventArgs class that can be used to pass event information from the broadcasting class (Form1) to any listening classes (Form2).

For more information on Events please see the article about how to use events in .NET.

What you'd want to do is create a custom class that inherits from the EventArgs class and provides a property that lets the broadcasting class provide event information and lets the listening class retrieve it:

Expand|Select|Wrap|Line Numbers
  1. <Serializable()> Public Class TSearchInfoEventArgs
  2.     Inherits EventArgs
  3.         Private _searchText As String
  4.         Public Sub New(ByVal searchText As String)
  5.             _searchText = searchText
  6.         End Sub
  7.         Public Property SearchText As String
  8.            Get
  9.                return _searchText
  10.            End Get
  11.            Set(ByVal value As String)
  12.                _searchText = value
  13.            End Set
  14.         End Property
  15. End Class

Now when the text changes in the search text box you need to raise an event, passing the event the custom EventArgs class that you'll use to relay the text to the:

Expand|Select|Wrap|Line Numbers
  1. Public Event SearchTextChanged As EventHandler(Of TSearchInfoEventArgs)
  2.  
  3. Private Sub RichTextBox1_KeyUp(bla bla bla) Handles RichTextBox1.KeyUp 
  4.         RaiseEvent SearchTextChanged(Me, New TSearchInfoEventArgs(RichTextBox1.Text))
  5. End Sub

Or just do some research on Properties and the PropertyChangedEvent :)

-Frinny
Jun 5 '09 #2

P: 14
Thanks for the info . . ..

I tried this earlier in a class module, and got a stack overflow error, then I added a module to the project, and created functions to get and set for each of the two things I was passing and it worked fine. I have tried using properties quite a few times before, but they seem to be quite buggy.
Jun 5 '09 #3

tlhintoq
Expert 2.5K+
P: 3,525
@desertavataraz
Just a guess because I've done it with methods...
"stack overflow" most commonly happens to me when I create a circular loop.

Properties actually work great and are used a LOT by everyone.

My my guess would be that you are referencing the property from within the property 'set'... thus creating an endless loop.

Expand|Select|Wrap|Line Numbers
  1. public string bob
  2. {
  3.     set
  4.     {
  5.         bob = value;
  6.     }
  7. }
Jun 6 '09 #4

P: 14
I believe you are correct on that . . . . the thing that I have not understood is:

You type in this line: "Public Property x() As Integer" then hit enter
and you get:
Expand|Select|Wrap|Line Numbers
  1.      Public Property x() As Integer
  2.  
  3.         Get
  4.  
  5.         End Get
  6.         Set(ByVal value As Integer)
  7.  
  8.         End Set
  9.     End Property
  10.  
Since there appears to be no way to set the property value within set property, why is there a set property, and how do you set the value of the property, if set property does not allow you to do it without looping . . . that is counter-intuitive. (an a bit loopy, if you'll pardon the pun.)
Jun 6 '09 #5

tlhintoq
Expert 2.5K+
P: 3,525
I'm not sure how it works in VB, but I can tell you the C# way of it. Perhaps Frinavale or someone more versed in VB can clear it up for you in VB.

In C# there are basically two constructs for a property. There is the self instance construct:

Expand|Select|Wrap|Line Numbers
  1. public int Yogi
  2. {
  3.      get; set;
  4. }
That's it. You don't have to do anything to set the value of Yogi except someplace else say

Yogi = 5;


Then there is the more complex form

Expand|Select|Wrap|Line Numbers
  1. private int bear = 5; // This gives you a default value
  2. public int Yogi
  3. {
  4.      get
  5.         {
  6.            return bear;
  7.         }
  8.      set
  9.         {
  10.            if (value > 100)
  11.              { 
  12.                bear = 100; // A way to set a maximum
  13.              }
  14.           else if (value < 1)
  15.              {
  16.                 bear = 1; // a way to set a minimum
  17.              }
  18.            else bear = value; // Not greater than 100, not < 1 so value entered is fine
  19.          }
  20. }
In the more complex form the public property 'Yogi' is just a gateway to the private variable 'bear' - but with some brains. It let's you put the qualification logic in the property one time, rather than have to write this 50 times, everyplace you would assign a value. The rest of your program can now safely try to do this...

Yogi = 500;

and you know that Yogi is smart enough to parse that and only store 100

The logic inside the set or get values can be anything you want. Hopefully something that makes sense every time a value is changed.

Expand|Select|Wrap|Line Numbers
  1. private int bear = 5; // This gives you a default value
  2. public int Yogi
  3. {
  4.      get
  5.         {
  6.            return bear;
  7.         }
  8.      set
  9.         {
  10.           if (value > 100)
  11.              { 
  12.                bear = 100; // A way to set a maximum
  13.                PlayErrorBeep();
  14.                LogMessage("User can't read instructions. Tried to enter " + value);
  15.              }
  16.           else if (value < 1)
  17.              {
  18.                 bear = 1; // a way to set a minimum
  19.              }
  20.            else bear = value;
  21.          }
  22. }
That's my short way of looking at it. But you really should read the MSDN for a more in-depth understanding of it.
Jun 6 '09 #6

Frinavale
Expert Mod 5K+
P: 9,731
@desertavataraz
Properties are used to provide the necessary code required to set/get the private members of your object....the Set method is there to be able to Set the private member value (a way to provide a value to the private member variable)

If you take note to tlhintoq's reply you'll see that he first declared a private Integer variable named "bear" (a private member of the class).

He used the Property "Yogi" to set the private member variable "bear" based on some hypothetical business logic.

In VB it would look like:
Expand|Select|Wrap|Line Numbers
  1. Private bear = 5 ' This gives you a default value
  2.  
  3. Public Property Yogi As Integer
  4.      Get
  5.            return bear
  6.        End Get
  7.      Set(ByVal value As Integer)
  8.            If (value > 100) Then
  9.                bear = 100 ' A way to set a maximum
  10.           ElseIf (value < 1) Then
  11.                 bear = 1 ' a way to set a minimum
  12.           Else 
  13.               bear = value ' Not greater than 100, not < 1 so value entered is fine
  14.           End If
  15. End Property

If you don't have any special business logic required for setting the private member then you don't have to include it and your property would look something like this:
Expand|Select|Wrap|Line Numbers
  1. Private bear = 5 ' This gives you a default value
  2.  
  3. Public Property Yogi As Integer
  4.      Get
  5.            return bear
  6.        End Get
  7.      Set(ByVal value As Integer)
  8.               bear = value 
  9. End Property
The beauty of properties is that it lets you change your internal form/class without having to change the pubic exposed members... for example say you have no business logic for setting the private member "bear" at first but then your client (the person you're developing for) requests some sort of "If" around setting it. You can change the property and the code that calls it won't care at all...it just calls it like it normally would have :)

If you call the Property from within the Set method then you will cause a recursive loop (which never ends).
Jun 15 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.