Solved

Null reference exception while using try cast on rich text box

Posted on 2013-01-07
4
647 Views
Last Modified: 2013-03-06
I am trying to cast the sender which is menu item to rich text box. but it throws a null reference exception.  Can anyone please point me in the direction where am I doing wrong? Thank you.

Here is the code.

 
 Private Sub mnuItemCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuItemCopy.Click
Dim c As RichTextBox = TryCast(sender, RichTextBox)
        If c.SelectedText <> Nothing Then
            c.Copy()
        End If

Open in new window


Thanks!
0
Comment
Question by:developer2012
  • 2
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 38752640
you have first to check if "c" is nothing also (aka if the TryCast worked):
 Private Sub mnuItemCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuItemCopy.Click
  Dim c As RichTextBox = TryCast(sender, RichTextBox)
  if c <> Nothing Then
        If c.SelectedText <> Nothing Then
            c.Copy()
        End If
  end if

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38752659
Can your sender ever be a RTB? It seems like you've got your handler only handling a menu-item selection.
0
 

Author Comment

by:developer2012
ID: 38752713
@ angeIII as you said, the condition fails at line 3 i.e. if c <> Nothing.

@ Kaufmed...Yes. I tried your idea. did not work for me.

Thanks
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38752735
I'm not sure what you mean by, "I tried your idea." I'm saying that it doesn't appear--even if you do sort out the syntax, which angelIII has given you--that your logic is going to give you the result you are looking for. sender is the control which generated the event, not (necessarily) the control you are looking to target for some bit of logic.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

777 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