cType error?

D

Dave

I'm using several routines in my program that work on properties in a
textbox. The KeyPress is one of them. They all work fine for all of the
textboxes listed in the Handles ... part of the sub shown below. Except for
one. I have one text box, txtAdd2, that ends up being passed to the subs as
'Nothing'. So when I try to reference the object in my subroutines I get the
following error:

"An unhandled exception of type 'System.NullReferenceException' occurred in
system.windows.forms.dll
Additional information: Object reference not set to an instance of an
object."

Here is the KeyPress event code. Stepping through the program the error
triggers on the Me.SelectNextControl line because the sender is 'Nothing'. I
get similar errors when sending a reference to txtAdd2 to other routines;

Private Sub Enter_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles txtAdd1.KeyPress,
txtAdd2.KeyPress, txtAdded.KeyPress, txtBDay.KeyPress, txtCity.KeyPress,
txtEMail.KeyPress, txtFName.KeyPress, txtHPhone.KeyPress,
txtJoined.KeyPress, txtLName.KeyPress, txtMPhone.KeyPress,
txtState.KeyPress, txtStatus.KeyPress, txtWeb.KeyPress, txtWPhone.KeyPress,
txtZip.KeyPress

If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
Me.SelectNextControl(cType(sender, TextBox), True, True, False,
True)
End If

End Sub

Any help would be appreciated.

Thanks,
Dave
 
L

Linda Liu [MSFT]

Hi Dave,

Based on my understanding, you are trying to navigate through the textbox
controls on your form by hitting Enter continuously. It works fine on all
textbox except one textbox, i.e. txtAdd2. When the txtAdd2 gets focused and
you press Enter, an exception of type 'System.NullReferenceException'
occurs. The txtAdd2 passes Nothing to the KeyPress event handler.

If I have any misunderstanding, please feel free to correct me.

I have performed a test based on your description, but I couldn't reproduce
the problem. I have a try setting the TabStop property one textbox on my
form to False. When the program is run and I hit Enter continuously, all
the TextBox except the one whose TabStop property is set to False get
focused in order. There's no any problem.

Is the 'txtAdd2' of type TextBox? Do you have any special process on
txtAdd2? If possible, could you please send me your sample project that
could just reproduce the problem? To get my actual email address, remove
'online' from my displayed email address.

I look forward to your reply.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top