Home and Learn: Intermediate Programming
Stick Figure App - Custom Font Dialog Box
We designed a second form in the previous lesson. In this lesson, you'll see how to launch this second from when a user right-clicks on the white drawing surface.
We covered launching a second form in an earlier tutorial. So we won't go into it too deeply here. What we're going to do, though, is to activate the second from when a user right-clicks on the Picture Box. We can do this from the MouseDown event of Form1. There's only four lines of code in the Event, at the moment. These lines (from the C# version):
mouseX = e.X;
mouseY = e.Y;
UndoStack.Push((Bitmap)bitmapObject.Clone());
RedoStack.Clear();
We can add an If ElseIf statement and check which mouse button was clicked, left or right. Add this one in VB Net:
If e.Button = MouseButtons.Right Then
ElseIf e.Button = MouseButtons.Left Then
End If
And add this if else if in C#:
if (e.Button == MouseButtons.Right)
{
} else if (e.Button == MouseButtons.Left)
{
}
(You could have it the other way round, if you want, the Left at the top and the right at the bottom.)
The four lines of code you already have can go in the Left mouse button else if. Your code should then look like this in VB Net:

And this in C#:

We're going to need the X and Y coordinates of the right mouse click. You could use the same e.X and e.Y as before, but it's good to keep them separate. So add these variables inside of the Right mouse button If part in VB Net:
Dim rightClickX As Integer = e.X
Dim rightClickY As Integer = e.Y
And these variables in C#:
int rightClickX = e.X;
int rightClickY = e.Y;
Our second form will be a Modal dialog box. A Modal dialog box is one where you can't click away until you dismiss it. So you won't be able to access the first form until our second form, the font one, has been dealt with. Turning a normal form into a Modal one is easy enough to do. Add this If Statement just below your new X and Y two lines (VB Net):
If FormText.ShowDialog() = DialogResult.OK Then
End If
In C#, you need to create a new object from your second form. Then you can show the dialog box. Here's your code:
FormText textForm = new FormText();
if (textForm.ShowDialog() == DialogResult.OK)
{
}
textForm.Dispose();
Notice that we're cleaning up by disposing of the form object we created.
Your code should look like this in VB Net:

And here's the C# version:

Test it out. Run your program. Now right-click on the picture box. It should launch your second form:

Now try to click on your first form, the one behind the dialog box. You'll find that it won't activate. You need to deal with the font form first.
Close down both forms and return to the coding window. We'll come back to the MouseDown Event a little later. Let's tackle the coding for the second form now.
Email us: enquiry at homeandlearn.co.uk