הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Tuesday, May 29, 2007 11:30 PM
I have form1,form2, and Form3.
Form1 calls form2, form2 calls form3.
I want to Write some text to a textbox on form2 from form3.
Is there a way to do this. Baby step this for me, please.
All replies (7)
Tuesday, May 29, 2007 11:47 PM ✅Answered | 1 vote
Hi Brad,
There are a few ways to do it. The easiest way is to make the textbox public and then to call the text property of the textbox directly. Here are the steps (these steps assume you leave everything to the default values, unless told otherwise to change it):
In form 3, add a textbox.
In the properties window of the new textbox, find the property called "Modifiers". It will default to "Friend", change it to "Public".
In form 2, add a textbox and add a button.
Double-click the button to bring up the code window for the button's click event, add the following:
textbox1.text = form3.textbox1.text
This assumes the textboxes are named "textbox1"; which would be the case if they are the only existing textboxes on the forms and you did not change the name property. For this to work you would have to have both forms showing; atleast you would have to have form3 loaded at some point. Easiest method would be to load the form up, then hide the form. You can do this by adding a button to form3 and double-click it to get the button's code view of the click event and add the following:
me.hide()
The code in the button's click event could actually be placed anywhere and activated in any method you wish; however, this approach does not offer any type of data integrity or error catching. So, it would be possible to produce an error if you were to click the button and form3 does not exist.
As for the other methods, you could create a module in your application and have it hold a public variable. You would then set this variable while you are in form3 and then when you return to form2 you could then call the variable and load it into the textbox.
An example would be to Add a module to the project, view the code-window and add the following (the following code examples assume the module is named "Module1":
Public strGlobalVariable as String
Then in form3, add a textbox and two buttons. Double click Button1 to get the code window and add the following:
Module1.strGlobalVariable = textbox1.text
Now, double-click the Button2 to get the code window and add the following:
me.hide()
Now in form2, add a textbox and a button. Double-click button1 to get the code window and add the following:
textbox1.text = Module1.strGlobalVariable
As you will see in both of these examples it will all depend on how you want to handle the task, but no matter what way you go you must have either a textbox set as public or a variable set as public. The module method is probably more preferred because you don't have to worry about loading up a form and hiding it. Once the variable in the module is set it no longer matters if form3 exists or not, because the Module will exist throughout your application's life-cycle. Then you can also preset a value in the global variable if you wish to avoid an error caused from non-exisiting data.
I hope the above helps you!
Thanks,
James
Wednesday, May 30, 2007 12:26 AM ✅Answered
When you create Form3 - you would set a variable which contains a reference to form2 (the calling form). Then from Form3 you can access all the properties on the calling form. The following provides and example
Code Snippet
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As New Form2
x.CallerFormRef = Me
x.Show()
End Sub
End Class
Public Class Form2
Public CallerFormRef As Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If callerformref IsNot Nothing Then
CType(CallerFormRef, Form1).TextBox1.Text = "Ta da!!!!"
End If
End Sub
End Class
Form1 button 1 opens form2. Click on Button1 on form2 and it sets the textbox.text contents on the calling form.
Wednesday, January 30, 2008 10:20 PM
I am trying to do a simular thing
I have many forms that use a complicated sub that is stored in a module.
Each of the forms has a textbox on and i need the Sub in the Module to add output text to the textbox on the form that called it.
Steve
Wednesday, January 30, 2008 11:26 PM
Steve,
You’d probably be best to use a function for what you want. A function is typically used when you want to send a parameter (such as text or mathematical quotations) to be processed by some sort of code block and then to return the result(s).
Here is a simple example of how to append text to a the original text calling the function. To replicate this code you will need to have a form with 1 button (named: btnProcess) , 2 textboxes (named: txtOriginalText, and txtProcessedText), and a function in a module (named: Add_Text).
The key here is that the function within the module MUST be declared as PUBLIC. If you use the default scope or declare it as PRIVATE then it will not be able to pass back the results. You’d use the default scope / PRIVATE in cases of where a function is being called internally and you are passing it back internally (meaning within the module the function is contained).
The coding in the module is:
Code Snippet
Module Module1
Public Function Add_Text(ByVal stOrigText As String) As String
stOrigText &= " This was added while being processed in the module."
Return stOrigText
End Function
End Module
The coding in the Form is:
Code Snippet
Public Class Form1
Private Sub btnProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProcess.Click
Dim stTextOriginal, stTextNew As String
stTextOriginal = txtOriginalText.Text
stTextNew = Module1.Add_Text(stTextOriginal)
txtProcessedText.Text = stTextNew
End Sub
End Class
Of course, you’d probably want to add in some sort of validation. Also, take note that in my function example I use the ‘&=’ operand; I believe this was introduced in .NET Framework 1.1 (or later). If you’d like to use this in example in earlier VB.Net versions (.Net Framework 1.0) you’d change the operand to be:
stOrigText = stOrigText & “ This was added…”
Functions are a great way to keep the logic of processing in modules (that can be updated separately from the main application). You can find more information on MSDN at: http://msdn2.microsoft.com/en-us/library/sect4ck6(VS.80).aspx
I hope this helps!
Thanks,
James
Thursday, January 31, 2008 9:32 AM
James thanks for the info
This part is to big and dificult to use in my code as it is needed 100's of times
Dim stTextOriginal, stTextNew As String
stTextOriginal = txtOriginalText.Text
stTextNew = Module1.Add_Text(stTextOriginal)
txtProcessedText.Text = stTextNew
Ok here is my code now the problem bit is in RED
Public declaration
Public CallerFormRef As Form
In the module
Public Sub prt(ByRef p1 As String, Optional ByVal p2 As String = "", Optional ByVal p3 As String = "", Optional ByVal p4 As String = "", Optional ByVal p5 As String = "", Optional ByVal p6 As String = "", Optional ByVal p7 As String = "")
If CallerFormRef IsNot Nothing Then
CType(CallerFormRef, DESIGN_TOOL).TextBox1.Text += p0 : CType(CallerFormRef, DESIGN_TOOL).TextBox1.SelectAll() : CType(CallerFormRef, DESIGN_TOOL).TextBox1.ScrollToCaret()
'(DESIGN_TOOL is a form name but here i want to just use the sending form automatically, now i need to add a what if for each form. Also the select all & scrolltocaret are only to scroll to the current position of the text box any better way to do this ?)
End If
End Sub
On Each form
Private Sub DESIGN_TOOL_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CallerFormRef = Me
End Sub
'then a button to do something
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
prt("Mean GL", GLMEAN,X,Y,Z)
'there are 100's of these lines
End Sub
The problem is having to add a what if to check for which form sent the request and remembering in future to add more forms as required
Steve
Thursday, January 31, 2008 4:43 PM
Steve,
My example was just to show you how simple it was to send text into a module and to handle the results. Ultimately you are using two lines (assuming you already have the original text loaded into a variable). You basically need a line that calls the module's function and then a second line to handle the returned results.
I noticed first in your code posting, in particular the module you have a sub-routine called prt. The first problem is that a "sub" cannot return results, it is only used to process the code without any expected returns. So, first thing is to change the module sub to a function. When changing it to a function check to ensure the end statement changed to "End Function"; some VB.Net versions don't automatically handle this for you. It should now read:
Code Snippet
Public Function prt(ByRef.........
......
......
......
End Function
Also remember that now you are returning a value to the calling form so you'd need to add a line at the end that returns the results. (HINT: If you recode the module and calling code you could much more simply append text to the textboxes, instead of using the 'ScrollToCaret')
The second part I noticed is that you could easily incorporate the calling form within your 'prt' sub/function. Since you know that every call to this routine (I'm assuming every call to it) will need to know the calling form's name you would add it as a parameter requirement; the key here is that every call to it requires the knowledge of the form's name, so you'd want to make this your first or second parameter (before the 'optional' parameters). You would then change all the references from 'DESIGN_TOOL' to the parameter. Here's an example....
Code Snippet
Public Function prt(ByVal CallingForm As String, ByRef p1 As String,......
..........
'Now you'd replace all references to "DESIGN_TOOL" with "CallingForm"
........
........
End Function
By adding this first parameter you had just made it so your module is storing the form's name. As long as you pass it the form's name, of course. Here's an example of how to pass the name of the calling form....
Code Snippet
prtResults = prt(Form1.Name, "Mean_GL".......
Remember since you are wanting to pass back a modified text for the textbox and a function returns a result you will need to have a variable in place to receive the results. In my example I used 'prtResults', but use whatever you already have in place to handle the returned results (you want to escape the notion of having your module directly modify the textbox because of overhead, bad coding practice, and nearly impossible to maintain validation/scalability methods)....not to mention the security risks that get introduced!
Now, as far as the ScrollToCaret. It all just depends. Ask yourself if you need to append text to the end of the text within a textbox, or do you need to append text from a certain location (say in between certain letters/numbers)? In either case you would want to read up on "StringBuilder". This is a very powerful function built-into VB.Net (Framework 2.0) and you can do most everything you need.
This is a lot of information and may seem difficult and/or daunting because of the requirement to re-code; but in reality you'll eventually have to re-code regardless. I understand you have 100's of forms...but, hopefully you are using best practices and created a CONST or global variable and minimized the repetitive calls to the module?? This would allow you to simply change the coding in the function, change the CONST and to perform a simple find/replace with 1 line of coding on each form throughout the entire project...maybe take 15 minutes?
I hope the above helps, it's only a pointer into the right direction not the final resolution. You'll have to determine the best method to resolve your issues. We are always here to help...you might want to consider opening a new thread since you are getting way off topic of this original thread posting.
Thanks,
James
Thursday, January 31, 2008 5:53 PM
James
Thanks for this very imformative reply I see now the resons for changing the code method and I am going to test as to do the changes is no real big problem but will give me the flexability in the future to add more forms etc.
Thank you very much
Steve