We help IT Professionals succeed at work.

Problem Upgrading VB6 Picturebox to .NET

JR2003
JR2003 asked
on
Medium Priority
464 Views
Last Modified: 2008-02-01
I am upgrading an VB6 application to .NET.

The application uses pictureboxes controls.
I need a .NET control that has the following attirbutes:

1. Is a container control
2. Can get the focus
3. Has Keystroke events
4. Has a drawing canvas

Is there a control in .NET that will do all this?
Or there a way to write a custom control that will do this?

The picturebox control in .NET is no longer a container control.

Thanks
Comment
Watch Question

Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Hi JR2003;

How about placing the PictureBox in a Panel control, the Panel control will provide the container.

Fernando
Yes,

Then dock the PictureBox with the "Fill" option.

objPictureBox.Dock = DockStyle.Fill


OR

Place your Picturebox in a usercontrol.  Dock if you need to.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
I think you need to elaborate on item #4:

    4. Has a drawing canvas

The approach and methodology of drawing in .Net is significantly different from that of VB6.

From your requirements it sounds like this will not simply be a matter of replacing one control with another.  Can you give us a bigger picture?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Hey Mikey,

4.  Graphics handle for drawing lines, text, rectangles

This is how I read that ;)

Bob
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Right...I agree.

My comment was more geared towards the fact that many VB6 drawing routines use the ~persistent~ state of a PictureBox with its AutoRedraw property set to True...often resulting in VB.Net induced "drawing shock" when they can't do the same "simple thing" in .Net.  =)
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Yes, Mike, I agree that VB6 developers do go through some serious "shock" when transitioning to VB.NET.

Example of drawing persistent graphics in the Paint event handler:

  Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    e.Graphics.DrawRectangle(Pens.Red, CType(sender, PictureBox).DisplayRectangle)
  End Sub

Let's just carry on a conversation between ourselves =:).  It will not be so beneficial, though, since we both know a considerable amount about this stuff already.

Bob

Author

Commented:
A picturebox doesn't have keystroke events neither does the panel so putting a picturebox in a panel won't satisfy my requirements.

I must be able to intercept keystroke events.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
You can work around that limitation this way:

1) Set Form's KeyPreview = True

2) Handle the KeyDown event, and check the form's active control:

  Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    If Me.ActiveControl Is Me.PictureBox1 Then
      If e.KeyCode = Keys.A And e.Control Then
        ' Select all text.
       e.Handled = True
      End If
    End If
  End Sub

Bob

Author

Commented:
The control I'm upgrading is an ActiveX ocx usercontrol that contains 2 independent sub-controls.
These sub-controls need to receive the focus and keystrokes.
A picturebox is non-selectable so cannot be the ActiveControl.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Ok, you win. =:)  You will have to stay with a UserControl:

Public Class ContainerPanel
  Inherits System.Windows.Forms.UserControl

End Class

Bob

Author

Commented:
Can a user control get the focus?
Currently when I run the line
MyControl.Focus
The GotFocus event on the control doesn't fire.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
How about the Enter event?

Bob

Author

Commented:
Ok thanks.
When I use the arrow keys they move to the next control on the form.
I need to intercept the arrow keys and for focus to stay on the usercontrol.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
Paste this into your UserControl class:

  Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
     Return keyData <> Keys.Right AndAlso keyData <> Keys.Left
  End Function

The arrow keys are dialog keys, and are processed differently than normal keypresses.  If you return True, they are handled, and if you return False, then they are blocked.  So, we are blocking Right and Left arrow keys.

Bob

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.