Solved

Null reference exception while using try cast on rich text box

Posted on 2013-01-07
4
651 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error import assembly 19 24
Difference between Leaflet and MapBox? 5 45
Manage big list of parameter list 8 21
VB .net 2010 Byte array 2 21
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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