Solved

VB Clearing Controls

Posted on 2014-10-07
2
140 Views
Last Modified: 2014-10-07
Hi
I create Controls in a Tabpage called extraTabPage

dim txt as new system.windows.forms.TextBox
dim pkr as new system.windows.forms.DateTimePicker
dim btn as new system.windows.forms.Button

extratabpage.Control.Add(txt)
extratabpage.Control.Add(pkr)

I add about 3 or 4 of each setting height and position as I go

Then create a button

extratabpage.Control.Add(btn)
btn.text = "Clear Entries"
add Handler btn.click, address of ClearEntries

End sub

Private Sub ClearEntries(ByVal eventsender As System.Object, ByVal eventargs As System.EventArgs)
            Dim ctrl As Control
            For Each ctrl In mExtraTabPage.Controls
                If (ctrl.GetType() IsNot CType(ctrl, Button)) Then
                    If (ctrl.GetType() Is CType(ctrl, TextBox)) Then
                        Dim txt As TextBox = CType(ctrl, TextBox)
                        txt.Text = ""
                    End If
                    If (ctrl.GetType() Is CType(ctrl, DateTimePicker)) Then
                        Dim Cal As DateTimePicker = CType(ctrl, DateTimePicker)
                        Cal.Format = DateTimePickerFormat.Custom
                        Cal.CustomFormat = " "
                    End If
                End If
            Next
        End Sub

when the clearentries is called I get an error

Unable to cast object of type system.windows.forms.button to type system.windows.forms.TextBox

Thanks
0
Comment
Question by:Kevinfeening
2 Comments
 
LVL 22

Accepted Solution

by:
rspahitz earned 500 total points
ID: 40367376
>If (ctrl.GetType() IsNot CType(ctrl, Button)) Then

In the past I would use TypeOf like this:
If Not TypeOf(ctrl) Is Button Then

Not sure if this still works...
0
 

Author Closing Comment

by:Kevinfeening
ID: 40367428
worked
had to use in body of code as well as
if typeof(ctrl) is textbox then to clear the text box
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

896 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now