LabWindows / CVI Step–by–Step Tutorials For the Absolute Beginner
Handling Multiple Panels Shailesh Appukuttan E-Mail:
[email protected] [email protected] A few points to keep in mind before we start… This tutorial tutorial is aimed at easing the learning curve curve for LabWindows. LabWindows. They might quickly get you up and running but I stress that you need to practice, experiment and develop to really get to grips with these topics. I haven’t focused on being technically correct in these tutorials. So possibly there might be some factual errors.
LabWindows / CVI Step–by–Step Tutorials For the Absolute Beginner
Handling Multiple Panels Shailesh Appukuttan E-Mail:
[email protected] [email protected] A few points to keep in mind before we start… This tutorial tutorial is aimed at easing the learning curve curve for LabWindows. LabWindows. They might quickly get you up and running but I stress that you need to practice, experiment and develop to really get to grips with these topics. I haven’t focused on being technically correct in these tutorials. So possibly there might be some factual errors.
This being the first version is likely to contain some errors and ambiguities. You can help improve these tutorials by informing me of these errors, suggest additions and improvements. My email address has been given on the cover page. Ok let’s getdown to business….
Happy Programming! Programming!
In this tutorial we will be learning how to create menu bars. Before we begin, lets become familiar with the lingo specific to panels:
The User Interface (UIR) for a program consists of panels, menu bars, pop-ups and other elements. We have whole tutorials devoted to menu bars and pop-ups. Here we will discuss about panels.
Whenever we start a new program and we create a UIR, we by default get a panel, as shown below, on the screen:
Then we go about adding buttons, LEDs and other controls to this panel and create our program. So remember a panel is part of a UIR. We can also have more panels added to our UIR. This is what we will explore in this tutorial. We will see how we can create and manage multiple panels.
Now let us start off….
The software would open as shown:
Possibly, your earlier projects might be open. First we need to close all existing project files and open a new work area. Click on File -> New -> Workspace (*.cws)…
If you get a popup:
Click Yes.
Now, click on File -> New -> User Interface (*.uir)…
A panel would appear as shown:
Now click on Code -> Generate -> All Code…
Click ‘Yes’ on the pop-up:
Now another pop-up like would appear asking where you would like to save the files:
Select an appropriate location (create a new folder for every program/assignment that you create), give a suitable name (here I have named it as “MenuDemo.uir”) and click on ‘Save’.
A new pop-up appears as shown:
Click OK. Next Pop-up:
Click OK. Always ensure to keep the ‘Create Project in New Workspace’ option selected as shown above (which is the default option). This ensures that different program files don’t get wrongly associated.
So now we have a screen as below:
This is the place where we will ultimately write all our C Language code and hence observe that this file has a ‘.c’ extension. Observe the top-left of the window:
This shows that there you have a project named “MenuDemo”. This project currently contains two files: A UIR file: A C file: program
Panels.uir Panels.c
-> User interface that the user will get to see -> Where we create the functionality of our
Let’s first go and make our user interface. Double-click on MenuDemo.uir (or whatever is your file name) on the left top panel as shown above. We come back to the earlier screen:
This is where we will construct our user interface (UIR) for our program. We, by default, have been provided with one panel.
Run the program right now and see how it looks. We get:
Try clicking the screen:
and closing the program. It doesn’t work. Switch to your Labwindows
And click the
button. You might have to press it again until it becomes dimmed:
Let us customize our panel. Double-Click on the panel. We get a pop-up as shown:
Let us change some of the attributes (others you should experiment with on your own): Callback Function: Panel_Code Panel Title: Main Panel
Our pop-up now looks as:
Note that the Constant Name for this panel is ‘PANEL’. Click OK. Our panel now has a new title and looks like:
Now let us generate the code for this panel. Click Code -> Generate -> Panel Callback
Double-Click and open your C file now. We get:
You will see that a new function (by the name we gave in the Callback Function field for the panel) has now been created. Let us add code to activate on our panel. Add the following line between ‘case EVENT_CLOSE:’ and the ‘break;’ just beneath it: QuitUserInterface(0); So our C file now looks like:
Observe the top of the file where the ‘main’ function is defined. There is a line: if ((panelHandle = LoadPanel (0, “Panels.uir”, PANEL)) < 0) return -1;
Ignore the ‘if’ structure and just focus on the content inside. What we are doing is loading the panel ‘PANEL’ (remember the Constant Name of our panel) into memory and assigning it to a variable called ‘panelHandle’. LoadPanel takes 3 parameters. Leave the first as 0 (see help file for details). The second parameter refers to the name of our UIR file. Avoid giving full paths (e.g. C:\CVI\Panels.uir) as it would give errors when you run the program from a different PC with different folders. So just add the name of the UIR file as we have done. The third parameter is the Constant Name of your panel that we had seen earlier. If you go down, you will see: DisplayPanel (panelHandle); RunUserInterface (); DiscardPanel (panelHandle);
So once the panel is loaded into memory, you can then display it to the user.
RunUserInterface() is where your entire program gets executed.
And finally once we are done we discard the panel from memory. This is generally done towards the end of the main() function.
Both these functions, DisplayPanel and Discard Panel take one parameter: variable associated with the panel during LoadPanel.
Important: A panel can be displayed on the screen only once it is loaded into memory by LoadPanel().
If you go to the top of the file, you will see that: static int panelHandle;
Thus, panelHandle is of data type static int. You don’t need to bother much about its meaning. It’s just the way CVI LabWindows works. Just remember that when you want to create more panels, similar arrangements will have to be made. We will be doing that later on this tutorial. Try running the program now and see if
works. It does
Ok now come back to your UIR by double-clicking on your UIR file on the top-left window. Lets add a LED and two buttons to our panel.
Right-Click anywhere on the UIR and select LED -> Round LED (1st Option):
This gives us a new LED on our panel:
Double Click on this new LED and we get:
By now, we should be knowing how to fill up these fields. So let us give it the following values: Constant Name: Label: So we have:
Click OK. We get:
LED_MainPanel LED Main Panel
Now, Right-Click anywhere on the UIR and select Command Button -> Square Command Button (1st Option):
This gives us a new button on our UIR:
Double Click on this new button and we get:
Let us give it the following values: Constant Name: Callback Function: Label:
CMD_On CMD_On_Code ON
So we have:
Click OK. We get:
Right-Click on the button and select Generate Control Callback:
Now again do a Right-Click on the button and select View Control Callback:
This brings us to our C file with the function for our ON button ready to be edited:
Write code for turning ON the LED inside this new function. Add the following line between ‘case EVENT_COMMIT:’ and the ‘break;’ just beneath that: SetCtrlVal (panelHandle, PANEL_LED_MainPanel, 1);
Our C file now looks as:
Like before, create another button this time and fill it up with following values for the fields: Constant Name: Callback Function: Label:
So we have:
CMD_Off CMD_Off_Code OFF
Click OK. We get:
Like before do: 1. Right-Click on the button and select Generate Control Callback 2. Right-Click on the button and select View Control Callback
This brings us to our C file with the function for our OFF button ready to be edited:
Write code for turning OFF the LED inside this new function. Add the following line between ‘case EVENT_COMMIT:’ and the ‘break;’ just beneath that: SetCtrlVal (panelHandle, PANEL_LED_MainPanel, 0);
Our C file now looks as:
Resize and position the controls on the UIR as you would like and we get something like this:
Run the program and see that the buttons work in turning On and Off the LED.It does Ok now come back to your UIR by double-clicking on your UIR file on the top-left window. Click Create -> Panel
You get:
Now we have one more panel. Resize and position the new panel as you would like and we get something like this:
Double-Click on the new panel and fill in these values: Panel Title: Panel 2 Our pop-up now looks as:
Note that the Constant Name for this panel is ‘PANEL_2’. Click OK. Our panel now has a new title and looks like:
Now we need to add some code to our C file to be able to use this new panel in our program. Open your C file by double-clicking on your C file in top-left window.
We need to add statements to load and discard this new panel. We will do the load immediately after our main panel is loaded and discard it just before the main panel is discarded. The statements will almost be identical to those present for our main panel. For loading the panel into memory we will do the following: Create one more static int variable like panelHandle: static int panelHandle, secondPanelHandle; This variable ‘secondPanelHandle’ (or whatever you name it) is what we will use whenever we deal with anything on the second Panel. It is identical to ‘panelHandle’ that we use for the main panel. So as a rule we will use ‘secondPanelHandle’ in all places for the second panel wherever we use ‘panelHandle’ for the main panel.
Now write a simple form for the LoadPanel: secondPanelHandle = LoadPanel (0, “Panels.uir”, PANEL_2);
For discarding the panel from memory we have: DiscardPanel (secondPanelHandle);
Now, lets add one more button to our first panel from which we can open this new panel. Like before, create another button this time and fill it up with following values for the fields: Constant Name: Callback Function: Label: So we have:
Click OK. We get:
CMD_OpenPanel2 CMD_ OpenPanel2_Code Open Panel 2
Resize and position the button as you would like and we get something like this:
Like before do: 1. Right-Click on the button and select Generate Control Callback 2. Right-Click on the button and select View Control Callback
This brings us to our C file with the function for our Open Panel 2 button ready to be edited:
Write code for opening our new panel inside this new function. Add the following line between ‘case EVENT_COMMIT:’ and the ‘break;’ just beneath that: DisplayPanel (secondPanelHandle); So our C file now looks as:
Now let us add an LED and a button to our new panel. First add an LED: Right-Click anywhere on the new panel and select LED -> Round LED (1st Option):
This gives us a new LED on our new panel:
Double Click on this new LED and fill in the following values: Constant Name: Label: So we have:
LED_SecondPanel LED Second Panel
Click OK. We get:
Like before, create another button this time on the second panel and fill it up with following values for the fields: Constant Name: Callback Function: Label:
CMD_ClosePanel2 CMD_ ClosePanel2_Code Close
So we have:
Click OK. We get:
Resize and position the LED and the button on the second panel as you would like and we get something like this:
Like before do: 1. Right-Click on the button and select Generate Control Callback 2. Right-Click on the button and select View Control Callback
This brings us to our C file with the function for our Close button ready to be edited:
Write code for closing the second panel inside this new function. Add the following line between ‘case EVENT_COMMIT:’ and the ‘break;’ just beneath that: HidePanel(secondPanelHandle); HidePanel makes the panel not visible on the screen. Thus it would seem as if it has closed. But the panel will still be loaded in memory (until you call DiscardPanel) and you can do DisplayPanel on it again. Our C file now looks as:
Now let us update the ON and OFF buttons on our main panel to have effect on the LED on second panel as well. Right-Click on the ON button and select View Control Callback. We get:
Add one more line beneath the existing line in that function: SetCtrlVal (secondPanelHandle, PANEL_2_LED_SecondPanel, 1); This is almost identical to the line that we had entered to turn ON the first LED. The differences are the names of the panel variables (first parameter) and the names of the
LEDs themselves (second parameter). Our C file now looks like:
Similarly we now do for the OFF button. Right-Click on the OFF button and select View Control Callback. We get:
Add one more line beneath the existing line in that function: SetCtrlVal (secondPanelHandle, PANEL_2_LED_SecondPanel, 0); Again, this is almost identical to the line that we had entered to turn ON the first LED. The differences are the names of the panel variables (first parameter) and the names of the LEDs themselves (second parameter).
Our C file now looks like:
Now run the program and experiment with the various buttons. You will see that initially we have our main panel displayed:
Then when we click ‘Open Panel 2’, the second panel is displayed:
The ON/OFF buttons affect both the panels now. So this way we can control objects on one panel from another panel. These effects of ON/OFF take place even when Panel2 is not in display.
Also observe that you can still operate (as in press buttons etc) on the first panel even after the second panel is displayed. Now let us look at another method of showing a panel. It is by way of a function called InstallPopup(). Exit your program. Go back to your UIR. Let us modify the code for ‘Open Panel 2’ button that is on our main panel. Right-Click on the button and select ‘View Control Callback’. This brings us to our C file with the function for our Open Panel 2 button ready to be edited:
Let us change the code inside this function. Replace the existing line: DisplayPanel (secondPanelHandle); With InstallPopup (secondPanelHandle); So our C file now looks as:
Now run the program and experiment with the various buttons. You will see that initially we have our main panel displayed:
Then when we click ‘Open Panel 2’, the second panel is displayed:
What is to be noted here is that once the second panel is displayed, we can no longer operate on the first panel, i.e. we are unable to press the ON and OFF buttons once second panel is visible. This is because we have now opened the second panel as a pop-up and LabWindows has a rule that pop-ups have to be closed to get back to earlier window/panel. This is the basic difference between DisplayPanel() and InstallPopup(). Press the ‘Close’ button on the second panel. Now you can ON/OFF the LEDs. Even though second panel is hidden, pressing the ON button does turn on its LED as well. To test this out, press the ON button. This turns ON the LED on your main panel:
Now press ‘Open Panel 2’. The second panel will appear and its LED will also be ON:
With that we have finished this tutorial on handling multiple panels. You can extend this same method to handle more than 2 panels as well.
A Quick Summary: