Click here to Skip to main content
15,892,746 members
Articles / Desktop Programming / Windows Forms

Issues & WorkArounds while Migrating Application from VB to C#

Rate me:
Please Sign up or sign in to vote.
3.44/5 (5 votes)
21 Apr 2009CPOL2 min read 20.2K   5   4
Issues & WorkArounds while Migrating Application from VB to C#

Introduction

While working on a Migration Project from VB6 to C#, I found some very interesting issues which are inbuilt functionalities existing in VB6 environment but it's very hard to replicate the same kind of functionality in our C# application.

Background

Gradually when we drilled down our application, we found some very interesting kind of issues which are handled in VB6 but not available in our C# Winforms side. Below are some of the issues which I want to share because there might be some other developers who need these kind of workarounds.

  1. In VB if we have two buttons having the same shortcut key, it will handle automatically with the help of Tab Key, while in C# Winforms if we have two buttons having the same shortcut Key, it will work only for the first button.
  2. In Windows MDI Applications, if our button having shortcut Key (Alt + -) ex. button &- , VB6 handled it and works perfectly while in C# MDI Application, it will open the Control Menu on the Top Left corner.
  3. In MDI Applications, if we are using MenuStrip, and we have set some menuItem to its MDIWindowListItem property so that it should show the current activated child forms under this menu item, in VB once any child is opened it takes the caption text to this MenuItem list, even after changing the caption of the child form, it reflects to the MenuItem List (i.e. it refreshes itself to show the current caption of child form). In case of C# MDI Application, we have to handle this feature so that it should show the latest caption of activated child form.

Using the Code

To handle the first two issues, we have our savior ProcessCmdKey, generally in our VB Apps we had only max two buttons having this kind of issue (have the same shortcut Key). So to handle this, I wrote my workaround in this way. Here we have two buttons button1 & button2 having the same shortcut key Alt + A.

C#
/// <summary>
/// Handle the Same Shortcut Keys on form.
/// </summary>
private const Keys ShortCutKeysWithAlt = Keys.Alt | Keys.A;
private bool _isFocusOnFirstButton = false;
 protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
 {
    if (keyData == ShortCutKeysWithAlt)
      {
        if (_isFocusOnFirstButton)
          {
             button1.Focus();
             _isFocusOnFirstButton = false;
          }
        else
          {
             button2.Focus();
             _isFocusOnFirstButton = true;
          }
           return true;
      }
    else
      {
         return base.ProcessCmdKey(ref msg, keyData);
      }
 }

Using the same ProcessCmdKey, we can easily handle the second issue. We just need to add a small piece of code.

C#
/// <summary>
/// Handle the Same Shortcut Keys on form.
/// </summary>

private const Keys ShortCutKeysWithAlt = Keys.Alt | Keys.OemMinus;
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == ShortCutKeysWithAlt)
    {
        button.PerformClick();
        return true;
    }
    else
    {
        return base.ProcessCmdKey(ref msg, keyData);
    }
}

To resolve the third issue, we have to add DropDownOpening Event to the Menu Item which is MDIWindowList Item of MenuStrip control. We have rectified one more issue here after if block, the issue was in C# when we open any child form its Caption added in Window Menu and when we close the child form, it leaves a Separator under this Menu Item, which is inbuilt handle by VB Apps. To handle this, we manually have to remove this ToolStripSeperator from the end of menu Item.

C#
/// <summary>
/// mnuWindow_DropDownOpening event
/// </summary>
/// <param name=""sender""></param>
/// <param name=""e""></param>
void mnuWindow_DropDownOpening(object sender, EventArgs e)
{
    if (this.ActiveMdiChild != null)
    {
        Form activeChild = this.ActiveMdiChild;
        ActivateMdiChild(null);
        ActivateMdiChild(activeChild);
    }

    if (mnuWindow.DropDownItems[mnuWindow.DropDownItems.Count - 1] is        
          System.Windows.Forms.ToolStripSeparator)
    {
         mnuWindow.DropDownItems.RemoveAt(mnuWindow.DropDownItems.Count - 1);
    }
}

I will continuously add my experiences while working on this migration project. If someone has experience with the same kind of interesting issues and their workarounds, please share with me.

History

  • 21st April, 2009: Initial post 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Aristocrat Technologies India Pvt. Ltd.
India India
I am MCAD certified ,working with Aristocrat Technologies(www.aristocratgaming.com).

Comments and Discussions

 
GeneralMy vote of 1 Pin
changalfgggggggggg14-Oct-10 4:17
changalfgggggggggg14-Oct-10 4:17 
GeneralRe: My vote of 1 Pin
Rob Philpott21-Dec-10 0:44
Rob Philpott21-Dec-10 0:44 
Generalgood article Pin
Donsw19-May-09 4:13
Donsw19-May-09 4:13 
GeneralRe: good article Pin
vivekgaur29-Jun-09 23:24
vivekgaur29-Jun-09 23:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.