473,320 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.
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
6 1616
Frinavale
9,735 Expert Mod 8TB
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
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
3,525 Expert 2GB
@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
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
3,525 Expert 2GB
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
9,735 Expert Mod 8TB
@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

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

Similar topics

12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
2
by: Bill | last post by:
Can anyone tell me the best way to pass form entries from one page to the other just using javascript? The only ways I've found are coockies or using hidden fields to store the data and passing...
2
by: Richard | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** HI, I am working on a project where I need to input data to a (local) HTML page using multiple form elements, such as text,...
5
by: Jack | last post by:
Hi, I need to pass multple variables in a link in order to go to a asp page with the two varables. The following are the values of the variables using response.write: <%'Response.Write Mypage...
11
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
5
by: manningfan | last post by:
I have a single form that I want to make as generic as possible. The form contains a Calendar control. Using OpenArgs I pass values from the form that's calling the calendar, and the field the...
1
by: TheSailor | last post by:
Forgive me - I am a bit new to cURL and passing form elements from one site to the next... If you can help with a HOW TO or by holding my hand a bit with examples - so I can learn - I would be in...
2
by: Carl Heller | last post by:
Working in VS2003, .Net 1.1 I'm working on a project where I compare data between two databases. This is a lengthy process, and very data intensive, so I decided to create a class, and thread...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.