Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2007-07-20
23
Medium Priority
?
237 Views
Last Modified: 2013-11-17
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

0
Comment
Question by:PBLack
  • 9
  • 6
  • 5
  • +2
23 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19534377
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.
0
 

Author Comment

by:PBLack
ID: 19534461
Sorry

I am using Vb.Net 2005
0
 

Author Comment

by:PBLack
ID: 19534489
compact framework
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 96

Expert Comment

by:Bob Learned
ID: 19534519
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
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19534534
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.

0
 

Author Comment

by:PBLack
ID: 19534556
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
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19534560
0
 
LVL 18

Expert Comment

by:Priest04
ID: 19534561
If textbox control is placed on the Form (not on some container)

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

Goran
0
 

Author Comment

by:PBLack
ID: 19534590
Idle_Mind I tried your solution but it all is blue underlined. The error generated is:

Property 'Item' is 'ReadOnly'
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19534627
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.

0
 

Author Comment

by:PBLack
ID: 19534679
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
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19534746
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

0
 

Author Comment

by:PBLack
ID: 19534759
Priest04 your solution generates the following error:
Property access must assign to the property or use its value
0
 
LVL 18

Expert Comment

by:Priest04
ID: 19534861
:) I was wirting it in c#, so I forgot to change brackets from [] to ().

Goran
0
 
LVL 7

Expert Comment

by:natloz
ID: 19535702
Can you not pass By Ref?
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 600 total points
ID: 19535845
You can remove clsGlobals shared form references, without breaking anything, since that's what 2005 already does for you.

If you can the control Modifiers to Friend, then you can directly reference the control, and not have to go through any other hoops.

Bob
0
 

Author Comment

by:PBLack
ID: 19542351
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
0
 

Author Comment

by:PBLack
ID: 19542361
       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.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 19542416
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
0
 
LVL 18

Accepted Solution

by:
Priest04 earned 1400 total points
ID: 19542475
I am sorry for posting the answer from another topic so please disregard my previous post. :)

Now, error indicates that Controls property accepts only integer value, not string, which is not supported in Compact framework (I have just read the documentation). Since control can be accessed by index, you can try this code

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

for i as integer = 0 to form7.Controls.Count - 1
    if frm.Controls(i).Name = lblText.Text then
        frm.Controls[i].Text = lvi.SubItems(1).Text
        exit for
    end if
next i

Form7.Show()

Goran
0
 
LVL 18

Expert Comment

by:Priest04
ID: 19542480
Ahhh, I again forgot to translate [] to (). It shoud be

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

Goran
0
 

Author Comment

by:PBLack
ID: 19546198
Thanks for the help. It is working now.

PBLack
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19547886
Sorry PBLack, I don't know how I missed at the top where you indicated this was for the "compact framework"...   =\
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question