Solved

VB Clearing Controls

Posted on 2014-10-07
2
138 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
Comment Utility
>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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 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

19 Experts available now in Live!

Get 1:1 Help Now