Solved

Null reference exception while using try cast on rich text box

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
More on Time zones in vb 2010 12 37
Trying to use colormap to replace color in png, with a gradient to transparent 7 31
DataGridView Events ? 3 37
Angular JS Route 3 47
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

26 Experts available now in Live!

Get 1:1 Help Now