Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB Clearing Controls

Posted on 2014-10-07
2
Medium Priority
?
147 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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

824 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