Solved

VB Clearing Controls

Posted on 2014-10-07
2
143 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

751 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