Solved

Null reference exception while using try cast on rich text box

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

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 74

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now