Passing info from one forms code to the other

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • desertavataraz
    New Member
    • Jan 2009
    • 14

    Passing info from one forms code to the other

    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.
    Code:
    '  Get the cursor location when someone clicks in the text box
    Private Sub RichTextBox1_Click(bla bla bla) Handles RichTextBox1.Click
             CursorLocNow = Me.RichTextBox1.SelectionStart
             cursorLocationChanged = True
    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?
    Last edited by Frinavale; Jun 5 '09, 03:21 PM. Reason: Added code tags. Please posto code in [code] [/code] tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    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:
    Code:
    Public Property SearchText As String
      Get
        return Me.RichTextBox1.Text 'Or whatever you need to return
      End Get
      Set(ByVal value As String)
         Me.RichTextBox1.Text  = value
      End Set
    End Property
    So now in your second form you can access the data in the main form simply using the Form1.SearchTex t.


    You can use the PropertyChanged Event 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:

    Code:
    <Serializable()> Public Class TSearchInfoEventArgs
        Inherits EventArgs
            Private _searchText As String
            Public Sub New(ByVal searchText As String)
                _searchText = searchText
            End Sub
            Public Property SearchText As String
               Get
                   return _searchText
               End Get
               Set(ByVal value As String)
                   _searchText = value
               End Set
            End Property
    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:

    Code:
    Public Event SearchTextChanged As EventHandler(Of TSearchInfoEventArgs)
    
    Private Sub RichTextBox1_KeyUp(bla bla bla) Handles RichTextBox1.KeyUp 
            RaiseEvent SearchTextChanged(Me, New TSearchInfoEventArgs(RichTextBox1.Text))
    End Sub

    Or just do some research on Properties and the PropertyChanged Event :)

    -Frinny

    Comment

    • desertavataraz
      New Member
      • Jan 2009
      • 14

      #3
      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.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Originally posted by desertavataraz
        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.
        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.

        Code:
        public string bob
        {
            set
            {
                bob = value;
            }
        }

        Comment

        • desertavataraz
          New Member
          • Jan 2009
          • 14

          #5
          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:
          Code:
               Public Property x() As Integer
          
                  Get
          
                  End Get
                  Set(ByVal value As Integer)
          
                  End Set
              End Property
          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.)
          Last edited by Frinavale; Jun 15 '09, 07:29 PM. Reason: Added code tags. Please post code in [code] [/code] tags.

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            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:

            Code:
            public int Yogi
            {
                 get; set;
            }
            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

            Code:
            private int bear = 5; // This gives you a default value
            public int Yogi
            {
                 get
                    {
                       return bear;
                    }
                 set
                    {
            [B]           if (value > 100)[/B]
            [B]             { 
            [/B]               [B]bear = 100; // A way to set a maximum[/B]
                         [B]}[/B]
                      else if (value < 1)
                         {
                            bear = 1; // a way to set a minimum
                         }
                       else bear = value; // Not greater than 100, not < 1 so value entered is fine
                     }
            }
            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.

            Code:
            private int bear = 5; // This gives you a default value
            public int Yogi
            {
                 get
                    {
                       return bear;
                    }
                 set
                    {
                      if (value > 100)
                         { 
                           bear = 100; // A way to set a maximum
            [B]               PlayErrorBeep();[/B]
            [B]               LogMessage("User can't read instructions. Tried to enter " + value);
            [/B]             }
                      else if (value < 1)
                         {
                            bear = 1; // a way to set a minimum
                         }
                       else bear = value;
                     }
            }
            That's my short way of looking at it. But you really should read the MSDN for a more in-depth understanding of it.

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              Originally posted by desertavataraz
              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.)
              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:
              Code:
              Private bear = 5 ' This gives you a default value
              
              Public Property Yogi As Integer
                   Get
                         return bear
                     End Get
                   Set(ByVal value As Integer)
                         If (value > 100) Then
                             bear = 100 ' A way to set a maximum
                        ElseIf (value < 1) Then
                              bear = 1 ' a way to set a minimum
                        Else 
                            bear = value ' Not greater than 100, not < 1 so value entered is fine
                        End If
              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:
              Code:
              Private bear = 5 ' This gives you a default value
              
              Public Property Yogi As Integer
                   Get
                         return bear
                     End Get
                   Set(ByVal value As Integer)
                            bear = value 
              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).

              Comment

              Working...