Passing value to a Forms TextBox

  • Thread starter Thread starter Scott M.
  • Start date Start date
S

Scott M.

Is this an ASP.NET Textbox control? If so, just use the name of the
control, as in:

txtUser.Text = someValue
 
In the Public Sub in your class, you need a reference to the form concerned.
There are many ways of achieving this but one way is:

Dim _f As Form = My.Application.OpenForms("frmName")

An object of type Form does not, of course know anything about txtBoxName,
so you need to cast the reference to a variable of type frmName:

Dim _f As frmName = CType(My.Application.OpenForms("frmName"), frmName)

Now that you have the reference, you can manipulate the TextBox directly:

_f.txtBoxName.text = "The quick brown fox ..."

There are a number of nuances to this approach but if the app is a basic
Windows Forms app then this approach should work just fine in most cases.
 
Here's another way of doing it:

- Start Windows App
- Add a button to form1
- Add a second form
- Add a textbox to form2

Open form2 in code view & add this code:

Public Shadows Sub ShowDialog(ByVal s As String)
TextBox1.Text = s
MyBase.ShowDialog()
End Sub

Public Shadows Sub Show(ByVal s As String)
TextBox1.Text = s
MyBase.Show()
End Sub

Double-click the button in form1 & add one of these snippets:

' Show modal:

Dim frm As New Form2
frm.ShowDialog("Hello, World!")
frm.Dispose()

' Show form:

Dim frm As New Form2
frm.Show("Hello, World!")

I hope this helps,

Newbie Coder
 
Hi Again Stephany,

Thanks again for your help. Just what I needed, I'll give it a go later
today, it's 1 a.m. just now.

regards

Terry
 
Hi Scott,

I'm using a Windows form, hopefully should be into ASP later this year,
thanks.

Regards
 
Hi,

I'm guessing that form2 is discarded after the magic has worked. Usefull, I
have seen this example on another site but it's not quite what I need just
now. One for later in the project.

Regards

Terry
 
At least I could help you in the future, Terry

I guess you are on UK time like I am :)

Take it easy,

Newbie Coder
(It's just a name)
 
Terry,

The one is given by Newbie is the one if you pass from a owner to a slave,
the one given by Stephany is the one if you pass from a slave to an owner.
You did in my opinion not tell that direction.

In the latter case (from a child to a parent) I prefer however

Dim frmSlave as Form2
frmSlave.owner = me
frmSlave.Show

and in the slave
DirectCast(owner.TextboxA) = "whatever"

Cor
 
Hi Cor,

Thanks for distinguishing between the two, I've no doubt that I will need
both shortly.

Regards
Terry
 
I haven't seen that syntax since Access VBA. Are you attempting to interact
with a textbox in this manner: Forms!frmName!txtBoxName?

If so,

Try using a dot instead of an exclamation point.

forms.item(myformindex).txtMyTextbox

Example

public sub Test(someValue as string)
dim strValue as string

strValue = "Test:" & someValue

for each f as form in forms
if typeof f is frmTarget then
f.txtTarget.text = someValue
end if
next

end sub
 
Hi Amdrit,

Yup, good old Access VBA. I understand your example, I have used something
similar in VBA to test whether a form was open or not.

Thanks for the help.
Regards
 
How do I pass a value from a Public Sub in a class to a forms textbox.

I get 'Name Forms is not declared' with Forms!frmName!txtBoxName.



Regards
 
Back
Top