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.
- 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.
- 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.
- 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.
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.
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.
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