jjspierx's picture

Windows.Forms + GL Control based application

Hello everybody, I am brand new to OpenTK and trying to get myself started. I am wanting to create a GLControl window within a Windows.Form. I have been reading through the tutorials and have gotten a game window demonstration to work, but I am stuck on getting a GLControl window working. I am programming in C# using Visual Studio 2012. I tried using the following tutorial.


I am able to understand the tutorial, and can even compile the tutorial without any problems. However, the GLControl is just a white window within my Windows form. I have verified that the glControl1_Load event is firing, but nothing appears in the GLControl window. Even trying to change the background color of the window has no effect.

Does anybody have any ideas for me on what I may be doing wrong? The tutorial was written using Visual 2005, maybe things have changed with VS, C# or OpenTK since the tutorial was written?

Thanks in advance for any help.


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jjspierx's picture

Just a little update. I can control the size property of the GLControl window and see that it changes size when the run the program, but the control is always blank (white). I was able to follow another tutorial that used a panel control as a host for GL Control, but I was only able to get it to work using VB.net. Once I got it working in VB.net, I rewrote it to run in C#, but again the same thing happens, it is like the screen never refreshes, or invalidates. The tutorial I used that worked with VB.net, but not with C# is here:


There has to be something simple that I missing, something different about the way vb.net and C# need to be instructed to refresh the GLControl. I assumed .invalidate() would do the trick as shown in the examples, but alas, it does not seem to work.

Thanks in advance for any advice or suggestions.

jjspierx's picture

Another update...

I notice the vb.net code uses "Import OpenTK.GLControl", however I cannot use what I assume to be the equivalent in C# "Using OpenTK.GLControl". First off, VS2012 does not show GLControl as namespace to use, and if I type it in anyway, I get a compile time error same OpenTK.GLControl is a type and not namespace. I am a mechanical engineer and not an experienced programmer, so I am unclear what this means, and wonder if this is the root of my problem? When I remove the "using OpenTK.GLControl;" the program will still compile just fine, but the GLControl window is always blank.

I have tried the GLControl examples do work, I verified that, I just can't compile my own programs and get them to work.

Aestivae's picture

Hi jjspierx,

I can't really tell very much from your description... all the tutorials you posted compile and run fine for me.

Since you say it compiles fine, I'm guessing it has to do with the Paint call. Double check that all your code matches the tutorials.

OpenGL (and OpenTK, by extension) render independently of the rest of the window. When you draw in OpenGL, you usually draw to a buffer in memory, which is then drawn on screen all at once to reduce flickering.

glControl1.SwapBuffers(); is the line of code in this case that draws everything in the OpenGL buffer to the screen. You'll need to put this line after all your drawing calls. In the C# tutorial, this is placed in your glControl1_Paint method.

A call to


forces a call to the glControl1_Paint method, which in this case calls SwapBuffers(), but this isn't always the case, so don't get them confused.

As for your "using" problem, C# only allows you to "import" namespaces, unlike VB.NET. If you need access to OpenTK.GLControl, just add using OpenTK;. Since you compile fine without it though, it's not strictly necessary.

jjspierx's picture

Thanks for the response Aestivae. I never actually figured out what the problem was, but I know the code matched what was in the tutorials. I suspect what was happening is since I was pasting in the "events" their was no code being generated in the designer. Either way, I moved on was able to get the 3D cube glControl sample to work and that was more in line with what I was needing anyway. After getting the 3D cube example to work, I modified it to take in data from a gyro to move the cube with the gyro's pitch/roll and yaw. Pretty cool stuff. Thanks again for responding. I am enjoying learning OpenTK and appreciate such a helpful community.

Aestivae's picture

Glad to hear it worked out.

For future reference, in C# (I don't know about VB.NET) you usually need to manually attach the event handlers using code like:

glControl1.Paint += new EventHandler(glControl1_Paint);

Visual Studio's Intellisense generally helps a lot to figure out which events to attach. If you double click a control in the designer, it adds a line like this (usually the Click event) to the designer-side code, but I personally prefer to add handlers manually, since it gives more control.

Good luck :)