A Ten-Thousand Foot View of SimConnect - Part 2
At this point, our program does very little. It just connects to SimConnect when you open it. When we are done with this tutorial, it will do the exact same thing, but it will do it with style.
Hopefully (since I didn't mention it last time) you saved your project when you when you were done. If so, when you open up Visual C#, it should be listed right there in the "Recent Projects" page. Open it, and double click on the file "Form1.cs" in the Solution Explorer. You will be presented with our blank form.
The Design screen in VC# allows us to layout our elements to a Windows Form and adjust the properties of those items. The easiest way of placing elements is opening the toolbox. It's an icon in the toolbar at top that looks like a pair of crossed hammers. Press it and you should get the Toolbox window on the left side of your screen. Expand the section titled "Common Controls". Under this you should see Pointer, Button, Checkbox, etc. All of the things you'd expect to see in a window.
First things first. click on button and drag over to your form. A button should appear. Place it near the top of the form.
In the properties at the bottom right, find "Name" and "Text". Both of these should be set to "button1". We're going to change name to "button_Connect" and text to "Connect to FSX". You'll notice that the text doesn't fit on the button, so grab one of the square handles surrounding the button and drag it right until it's large enough.
Now, find "TextBox" in the toolbox and drag that next to the button. Grab a handle and drag to the right, so that the textbox goes almost to the edge of the window. In the properties, change the Name to "textbox_Connect", border style to "None", background color to "Control" and text to "Not Connected". If you click on your form now to take away the box around the textbox, you should just see the text.
To wrap up the Design view, make sure the form is selected (you can tell because it will have the control handles around it) and on properties, change Text to whatever you want your application to be named. For me, it's "SimConnect Tutorial". Press Enter and it should appear in the title bar of your form.
Now we have some pretty elements, but we have to link them into the code to make them do fun stuff.
Accomplishing this could be very difficult, drawn out and painful. Instead, MS has made it so that we just double click on our button in the designer. The program takes us back to the code view, and we see that this has been added to Form1.cs
private void button_Connect_Click(object sender, EventArgs e)
{
}
Remember last time when I was talking about the event system? Here's a good example. The program isn't going to be constantly checking to see if you've pressed the button. Instead, the button click gets linked to a function that fires off when pressed. That's what we have here.
It could be easy enough just to move our SimConnect statements down into this function, but we want to do a little bit more with it. For starters, we have no error checking. If you have tried to run your program while FSX isn't running, you'll notice that it throws an exception, meaning that the program has run into an error it doesn't know how to handle. There's a way around that, of course, and that's with the try-catch statement. It looks like this:
try
{
// Run your code here
}
catch
{
// Handle any exceptions here
}
So in our case, we want it to look like this:
try
{
simconnect = new SimConnect("SimConnect Tutorial", this.Handle, WM_USER_SIMCONNECT, null, 0);
}
catch (COMException ex)
{
// Handle any exceptions here
}
the "COMException ex" part is the exception that gets sent back to our program. There's some information we could get from it if we wanted to, but we don't really need to worry about that now. Instead, we are going to use that text box that we created in the Design view.
A little bit of quick background on Properties and Methods. Pretty much everything in C# has properties and methods. Properties, as expected, contain information about the object, methods are actions or functions that it can do. For example, our text box (textbox_Connect) has a method called Clear, which simply clears the text from the textbox. To call that method for textbox_Connect, you would write the following:
textbox_Connect.Clear( );
This is called "Dot Syntax" and is used and many forms of programming. You access a property in the same way:
textBox_Connect.Text = "Your Text Here";
In this case, we are setting the property "Text" (the actual text that is displayed in the textbox) to whatever we have after the equals sign. That's what we are going to do in our button click function.
try
{
simconnect = new SimConnect("SimConnect Tutorial", this.Handle, WM_USER_SIMCONNECT, null, 0);
textbox_Connect.Text = "Connected to FSX";
}
catch (COMException ex)
{
textbox_Connect.Text = "Unable to Connect";
}
As you can see, we have text for both possibilities. If it connects, we get a message saying so, and if it throws an exception (if FSX isn't running) we say so as well. By setting the text property of the box, we will replace any text that is already in there. Before giving it a try, make sure you go up to the Form1 constructor and take out the SimConnect line you put up there earlier.
Form1.cs should now look like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.FlightSimulator.SimConnect;
using System.Runtime.InteropServices;
namespace SimConnect_Tutorial
{
public partial class Form1 : Form
{
SimConnect simconnect;
const int WM_USER_SIMCONNECT = 0x0402;
public Form1()
{
InitializeComponent();
}
private void button_Connect_Click(object sender, EventArgs e)
{
try
{
simconnect = new SimConnect("SimConnect Tutorial", this.Handle, WM_USER_SIMCONNECT, null, 0);
textBox_Connect.Text = "Connected to FSX";
}
catch (COMException ex)
{
textBox_Connect.Text = "Unable to Connect";
}
}
}
}
This one was a little bit shorter, but the goal is to establish a good framework to go off of. The next post in this series will add the ability to disconnect using the same button and an overview of SimConnect Event Handlers and detecting in-game events.
Once again, I appreciate any comments or critiques.


4 Comments:
Thank you so so so MUCH for your blog. I searched the entire internet for a tutorial on doing this step by step as I am not a programmer but have a huge interest in creating an addon for fsx. This tutorial is exactly what I was looking for. Can't wait for the next part. Thanks.
Thanks, anonymous.
Any particular direction you'd like me to go in? I'm going to build this particular application up to be sort of a "co-pilot helper" program to do things like raising gear, setting flaps, nav radio, things like that. A good mix of getting data, setting data and responding to in-game events.
I'm glad it helps!
I am anonymous.... Mike :) What you have planned sounds good to me. The only direction at the moment which I would like is for the application to exist within fsx. To be accessible on the toolbar at the top. Maybe i'm skipping ahead a little but thats the goal for me :) I can't offer much to you only that I will try the things you suggest and give you lots of feedback. Thanks..Mike
Hey there, great tutorial, I like where its going. I'm not really a programmer, but ive done a little bit of C++ and VB the last year. Im with Anonymous Mike here, really interested in add-ons for FSX. Would be great to see some decent add ons.
Post a Comment
<< Home