Link to home
Start Free TrialLog in
Avatar of Todd MacPherson
Todd MacPhersonFlag for Canada

asked on

How do I pass lablel.text on one form to textbox.name on another form?

I have a label on one form who's contents are supposed to be the name of a textbox on Form7

How I do I grab the textbox on Form7 so I can pass a subitem value from a listview to it? See my code below for more details and a better explanation:

This code resides on Form21

        If clsGlobals.Form7 Is Nothing Then
            clsGlobals.Form7 = New Form7
        End If

        Dim lvi As ListViewItem = lsvList.Items(lsvList.SelectedIndices(0))
             clsGlobals.Form7. & lblText.Text = lvi.SubItems(1).Text 'this does not work but I hope you see what I am after. lblText.Text is the name of the textbox on Form7.
        clsGlobals.Form7.Show()
        clsGlobals.Form7.Focus()
    End Sub

Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

What version VB.Net are you using?   VB.Net 2003 or VB.Net 2005?

I'm guessing VB.Net 2003 since you seem to have your own "global" form collection called "clsGlobals"...but I could be wrong.
Avatar of Todd MacPherson

ASKER

Sorry

I am using Vb.Net 2005
compact framework
Set the Modifiers for the control on the target form to Friend, and reference the control like this:

Dim lvi As ListViewItem = Form7.lsvList.Items(lsvList.SelectedIndices(0))

Bob
2005 should make it super easy then...

Something like:

    clsGlobals.Form7.Controls(lblText.Text) = lvi.SubItems(1).Text

This assumes that the control in question is contained DIRECTLY by the form itself.

If it is in another container, then you would need something like:

    clsGlobals.Form7.Panel1.Controls(lblText.Text) = lvi.SubItems(1).Text

Or you can "search" for the control using the Form as the root using the new Controls.Find() method.

Bob

The listview is on form21. I am trying to pass the selected subitem text value to a textbox on form7.

Form7's textbox name is = Form21's lblText.text
If textbox control is placed on the Form (not on some container)

Form7.Controls[lblText.Text].Text = lvi.SubItems(1).Text

Goran
Idle_Mind I tried your solution but it all is blue underlined. The error generated is:

Property 'Item' is 'ReadOnly'
How have you declared "clsGlobals.Form7"

Why do you need this in 2005 anyways?  VB.Net 2005 allows you to work with the "default instance" of a form simply by referring to its name:
 
    Form7.Show()  ' <--- WITHOUT ever using "New Form7"

You can use "Form7" from ANYWHERE in the project to access this default instance of Form7.

This project started out in VB.2003 and then was upscaled to Vb.2005. I do not have the time to go back and change it all.

I declared as follows:

Public Class clsGlobals
#Region "Singleton Forms"
    Public Shared Form1 As Form1
    Public Shared Form2 As Form2
    Public Shared Form3 As Form3
    Public Shared Form4 As Form4
    Public Shared Form5 As Form5
    Public Shared Form6 As Form6
    Public Shared Form7 As Form7
    Public Shared Form8 As Form8
    Public Shared Form9 As Form9
    Public Shared Form10 As Form10
    Public Shared Form11 As Form11
    Public Shared Form12 As Form12
    Public Shared Form13 As Form13
    Public Shared Form14 As Form14
    Public Shared Form15 As Form15
    Public Shared Form16 As Form16
    Public Shared Form17 As Form17
    Public Shared Form18 As Form18
    Public Shared Form19 As Form19
    Public Shared Form20 As Form20
    Public Shared Form21 As Form21
#End Region
End Class
Sorry, the mistake was mine.

I forgot to put the ".Text" in there after Controls().

So change this:

    clsGlobals.Form7.Controls(lblText.Text) = lvi.SubItems(1).Text

To:

    clsGlobals.Form7.Controls(lblText.Text).Text = lvi.SubItems(1).Text

Priest04 your solution generates the following error:
Property access must assign to the property or use its value
:) I was wirting it in c#, so I forgot to change brackets from [] to ().

Goran
Avatar of natloz
natloz

Can you not pass By Ref?
SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Soory to take so long to get back on this. Something came up suddenly and I had to go...anyway I still can not get it to work properly.

This line: clsGlobals.Form7.Controls(lblText.Text).Text = lvi.SubItems(1).Text

gives an error: Conversion from string "County" to type 'Integer' is not valid.

Where County was the name of the textbox for this example.

PBLack
       Dim lvi As ListViewItem = lsvList.Items(lsvList.SelectedIndices(0))
        Form7.Controls(lblText.Text).Text = lvi.SubItems(1).Text
        Form7.Show()
        Me.Close()

Sorry... here is the code now with the clsGlobals class removed. Same result though

Error: Conversion from string "County" to type 'Integer' is not valid.

I tried it by sending to an established textbox called txtProduct and it worked ( Form7.txtProduct.Text = lvi.SubItems(1).Text ) so I do not know where it is failing.
DateTimePciker.Format = Custom, DateTimePciker.CustomFormat = HH:mm

Place this code in form load event

if textBox1.Text = "" then
    dateTimePicker1.Value = new DateTime(2007,1,1,0,0,0); // we need to pass any year, month and day
else
    dim textBoxDate as DateTime
    if DateTime.TryParse(textBox1.Text, textBoxDate) then
        dateTimePicker1.Value = textBoxDate
    else
        MessageBox.Show("Unrecognized time format!")
    end if
end if

Just for info, value is not just time, but date + time. It only displays time part of the full date.

Goran
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ahhh, I again forgot to translate [] to (). It shoud be

frm.Controls(i).Text = lvi.SubItems(1).Text

Goran
Thanks for the help. It is working now.

PBLack
Sorry PBLack, I don't know how I missed at the top where you indicated this was for the "compact framework"...   =\