Solved

Null reference exception while using try cast on rich text box

Posted on 2013-01-07
4
653 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
[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
4 Comments
 
LVL 143

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

635 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