Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie question from someone moving over from VB6 3

Status
Not open for further replies.

WantsToLearn

Programmer
Feb 15, 2003
147
0
0
US
Hello All,

If VB6, you could access particular form controls by using Controls("ControlName"), etc. It appears you must use the index in .Net. but they can change if you add or delete a control on the form.

I am passing a form reference into a class so that I can read some of the control values when I instantiate the class. Is there an easier way than setting up properties on the form to retrieve the information? Thanks in advance!
 
this faq

faq796-5698 (mine)

and this

faq796-5773 (thatrickguy)

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
If you're new to .Net then the topics above may seem a bit complicated to start with.
If I understand your question correctly, you basically would like to access one or more of the controls on a form (whether you've passed the reference or not to a class doesn't matter)
Code:
dim control as control
for each control in form.controls
 name=control.name
 text=control.text
etc etc
next

to determine whether the control has certain properties such as text or whatever you can use the gettype method
Code:
if control.gettype is gettype(textbox) then
end if

if you need to change the generic control object to it's proper type to use it then
Code:
dim tb as textbox
tb=ctype(control,textbox)
 
Thanks for the quick responses which of course created a few more questions.

chrissie1,

In your FAQ, to what does the Me refer?
Code:
Dim info As System.Reflection.FieldInfo = Me.GetType().GetField("_"
Here is the declaration I am using:
Code:
Public Sub FormatText(ByVal CurrentForm As Windows.Forms.Form) 
With CurrentForm.Controls 
    Do Some Processing
End With
In VB6 I could say .Controls("Text1"), .Controls("Text2"), etc. Since I can't do that in .Net, is the best alternative to create Public Properties on the form?

Right now I am using the indexes but of course that could change if controls are added or deleted so I know that is not a long term solution. Thanks again!





 
Me refers to the current instance of the class.

Read this FAQ real quick faq796-5611, it's pretty intro level, but it sets up this description.

Continuing the House analogy. In the blueprints of the house lets say we want to put the owner's name on the door. We don't know who the owner is though, and we won't until the owner moves in (ie, the house class is instantiated). But we can refer to the specific house that is going to be built from the blue prints from the blue prints. In this case we could say that the name on the door should be Me.OwnerName

Every house you build from those blueprints will have a different owner, and this way you can have the blueprints specify the name on the door as soon as the owner moves in.

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Rick,

Your house analogy was very helpful in the FAQs.

Here is what I ended up doing pretty quickly thanks to the responses in this thread. The current code resides in a standard module with a form object as a function argument. So I created a class based on chrissie1's FAQ with one public shared function as below (I had to CType the return value because I have Option Strict On):
Code:
Imports System.Windows.Forms
Imports System.Reflection.FieldInfo

Public Class ReflectionInfo
    Public Shared Function GetControlByName(ByVal Name As String, _
                           ByVal CurForm As Form) As Control
        Dim info As System.Reflection.FieldInfo = _
            CurForm.GetType().GetField("_" & Name, _
                System.Reflection.BindingFlags.NonPublic Or _
                System.Reflection.BindingFlags.Instance Or _
                System.Reflection.BindingFlags.Public Or _
                System.Reflection.BindingFlags.IgnoreCase)
        If info Is Nothing Then
            Return Nothing
        Else
            Dim o As Object = info.GetValue(CurForm)
            Return CType(o, Control)
        End If
    End Function
End Class
That way I can use the code in a standard module as below:
Code:
Imports SolutionName.ClassName
Public Sub FormatText(ByVal CurForm As Windows.Forms.Form)
    Dim temp As String = _
        "LongDistDelim=" & GetControlByName("txtLongDistance", CurForm).Text & ";" & _
        "AreaCodeDelim=" & GetControlByName("txtAreaCode", CurForm).Text & ";" & _
        "ExchangeDelim=" & GetControlByName("txtExchange", CurForm).Text & ";" & _
        "DefLongDistPrefix=" & GetControlByName("txtDefaultLongDistance", CurForm).Text & ";" & _
        "IgnoreLongDistPrefix=" & CStr(GetControlByName("chkIgnoreLongDistance", CurForm).Tag) & ";" & _
        "SuppressEmptyAreaCode=" & CStr(GetControlByName("chkSuppressAreaCode", CurForm).Tag) & ";" & _
        "AddEmptyAreaCode=" & CStr(GetControlByName("chkAddAreaCode", CurForm).Tag) & ";" & _
        "DotReplacementChar=" & GetControlByName("txtDotReplacement", CurForm).Text
There is no way I would have come up with this solution without your help! THANK YOU ALL!





 
As an aside to the above code which I forgot to mention, the reason I use the Tag property for CheckBox controls is because the base Control class does not have a Checked property.
Code:
CStr(GetControlByName("chkIgnoreLongDistance", CurForm).Tag)
I had to use CStr because it did not recognize .ToString. Feel free to comment if there is a better solution. Thanks!
 
Here is a "final" version with CType to CheckBox so I can validate the Checked property directly. Note that I had to add an Imports statement for forms to do this.
Code:
Imports System.Windows.Forms
Imports SolutionName.ClassName
Public Sub FormatText(ByVal CurForm As Windows.Forms.Form)
    Dim temp As String = _
        "LongDistDelim=" & GetControlByName("txtLongDistance", CurForm).Text & ";" & _
        "AreaCodeDelim=" & GetControlByName("txtAreaCode", CurForm).Text & ";" & _
        "ExchangeDelim=" & GetControlByName("txtExchange", CurForm).Text & ";" & _
        "DefLongDistPrefix=" & GetControlByName("txtDefaultLongDistance", CurForm).Text & ";" & _
        "IgnoreLongDistPrefix=" & CType(GetControlByName("chkIgnoreLongDistance", CurForm), CheckBox).Checked & ";" & _
        "SuppressEmptyAreaCode=" & CType(GetControlByName("chkSuppressAreaCode", CurForm), CheckBox).Checked & ";" & _
        "AddEmptyAreaCode=" & CType(GetControlByName("chkAddAreaCode", CurForm), CheckBox).Checked & ";" & _
        "DotReplacementChar=" & GetControlByName("txtDotReplacement", CurForm).Text
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top