write to textbox on another form when action happens on 1st form

I need to write to a textbox on frmvoucher when a checkbox is selected on frmgeneric.  

I can do this inside the activation code but my problem is when the form has already been opened the activation will not be triggers.

'>>>>> frmvoucher

Private Sub frmPAIssueVouchera_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated

    End Sub
    Public Sub checkreason()
        If chkreason <> "" Then
            Dim generic
            Dim letterrequested
            Dim letteronfile
            Dim copay
            Dim other
            'check to see if textbox2 has any of the reasons in it."
            generic = "Brand name not requested, generic vouchered."
            letterrequested = "Brand name requested, by physician, letter requested."
            letteronfile = "Brand name requested, by physician, letter on file. "
            copay = "Brand name requested, by client, co-pay adjusted."
            other = "Other: Add a note in Staff Notes."
            If InStr(TextBox2.Text, generic) Then
                TextBox2.Text = Replace(TextBox2.Text, generic, " ")
            End If
            If InStr(TextBox2.Text, letterrequested) Then
                TextBox2.Text = Replace(TextBox2.Text, letterrequested, "")
            End If
            If InStr(TextBox2.Text, letteronfile) Then
                TextBox2.Text = Replace(TextBox2.Text, letteronfile, "")
            End If
            If InStr(TextBox2.Text, copay) Then
                TextBox2.Text = Replace(TextBox2.Text, copay, "")
            End If
            If InStr(TextBox2.Text, other) Then
                TextBox2.Text = Replace(TextBox2.Text, other, "")
            End If
            If InStr(TextBox2.Text, F2.UserName, CompareMethod.Text) Then
                TextBox2.Text = TextBox2.Text + "    " + F2.UserName
            End If
        End If
        formopen = False
    End Sub

'>>>>> frmgenerics

    Public ReadOnly Property UserName() As String
            Return chkreason
        End Get
    End Property

Open in new window

Who is Participating?
CodeCruiserConnect With a Mentor Commented:
You can keep a reference to the one form in another form. So if FormA opens FormB and FormB needs to write to FormA's textbox then

1) Declare a public variable in FormB which is of type FormA

Public frmA As FormA

2) When launching FormB from FormA, set the frmA variable

Dim f as new FormB
f.frmA = me

3) Then you can use this variable

frmA.Textbox1.Text = "Some Text"

You could store this reference variable anywhere such as in a module or main form.
Why not just put
frmvoucher.TextBox1.Text = "blahblah"
in the On_Click event of the checkbox?
running32Author Commented:
this is the error I get when I try to put that statement into frmgeneric.

Reference to a non-shared member requires an object reference.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Is your textbox public? If it's private, the other form won't be able to touch it. Did you set up your forms so they could see each other like putting both in the same class?
running32Author Commented:
I was just using a form inside my windows VS project.   I'd really like to learn how to change these forms so both forms can see each others control.  Could you tell me.

Did you start the project as a Windows application or a Windows Forms application?
I don't have VS here so I can't test stuff, but I assume you only use one instance of each form, so you can just declare both of them in the same main form or main application.
running32Author Commented:
The project was started as a Windows application within Visual Studio.   I then right clicked on the application and added a new form.  Form there VS created the template for the form and when you drag a textbox onto the form it takes care of the inital declaration.  

  'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.

    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
I'll have to test it later today when I'm back at home with my computer. Maybe someone else will chime in before then.
If you are declaring the both forms in an mdi form and access those two forms it will be more easy.

Take an mdi form (frmMDI)
declare the two forms in the frmMDI as follows.

public fVoucher as frmVoucher = new frmVoucher
public fGenerics as frmGenerics = new frmGenerics

now in your frmGenerics checkbox checked event you can write the following code to access the text box and enter the text for frmVoucher text box.

dim frMDI  as frmMDI = ctype(me.MdiParent, frmMDI)
frMDI.fVoueher.TextBox1.Text = "Chaged text"

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.