LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Is there a way to set focus to a field within code?

When a particular control is selected, I would like to change the focus to another control. Is there a way to do this? The result I'm looking for is having a Listbox become active such that when the cursor passes over items in the list box the item under the cursor will highlight. This normally happens in a list box when you click within the box. I need to be able to have the same visual effect when another control is selected. I've tried SetActiveCtrl and it will make keyboard input active in the listbox, but the cursor position does not highlight items.
0 Kudos
Message 1 of 7
(3,678 Views)
Are you trying to give focus back to the listbox and just highlight the previously selected item, or to change the highlighted item by just passing the mouse cursor over the items (similar to what happens on a menubar)?
The menubar-style highlighting (highlighting the item the cursor is pointing to without clicking) is not the normal operation of a listbox. You have to click with the mouse or use the keyboard cursor keys to change the highlighted item. If you want to impliment menubar-style highlighting, you could use mouse events in toolbox.fp in the callback for the listbox. EVENT_MOUSE_MOVE gives y in eventData1 and x in eventData2. But then you'd have to calculate the position of each item in the listbox.
If you just want to give focus back to the listbox (a
nd have it behave like a normal listbox), you need to call SetActiveCtrl(). If you tried that, but don't see the focus (and highlighting) going back to the listbox, I'd suspect that you haven't yet returned from the callback where you called SetActiveCtrl(). The user interface is not normally updated while you're in a callback. If you want the user interface to be updated before you return from the callback, you need to call ProcessDrawEvents(). But CVI still won't see events generated by the listbox until the callback returns unless you call ProcessSystemEvents().
See the CVI help for ProcessDrawEvents() and ProcessSystemEvents(). Carefully read the cautionary notes on ProcessSystemEvents().
Message 2 of 7
(3,678 Views)
Here's a sample project showing how to use Extended Mouse Events to create an auto-highlight listbox and how to programmatically set focus.
0 Kudos
Message 3 of 7
(3,678 Views)
Thanks Al. This example is a big help. There was one change that I needed to make which I wanted to pass on to you. The list box has numeric item entries 1 to 4. I wasn't able to highlight the 4. It had to do with how the value for the SetCtrlVal was calculated at the end of the ListBoxCallback routine. I made the following changes to the last couple of lines of the routine:

// your existing code
if (nPointedToItem < 1)
nPointedToItem = 1;
if (nPointedToItem > nListLines)
nPointedToItem = nListLines;

// I added this line (lineNum is new int variable)
// to find out which item is displayed on the first
// line of the list box
GetCtrlAttribute(panel, PANEL_LISTBOX, ATTR_FIRST_VISIBLE_LINE, &lineNum);

// the value returned fro
m previous call is added to
// value calculated by the mouse position, hence the
// proper item in the listbox is highlighted.
SetCtrlVal (panel, PANEL_LISTBOX, (int) floor(nPointedToItem)+lineNum);

I realize that you may never need this example again, but I thought I'd pass along my modification in case you were interested. I really appreciate your taking your time to come up with this example for me. Thanks again.
0 Kudos
Message 4 of 7
(3,678 Views)
On my machine, I have no problem with item 4. It may have something to do with fonts or screen resolution. Do you have to scroll the list at all to see all of the 4?
I knew that my calculation would have a problem if not all the items in the list were displayed. Thanks for the feedback.
0 Kudos
Message 5 of 7
(3,678 Views)
On my machine the number of display lines in the list box was 3, so a scroll bar was displayed in the listbox with 4 only visible when scrolled to the bottom. My application requires a scroll bar displayed once a certain number of items are in the list, therefore I would have had to modify the software to handle my requirements. Fortunately, I had to find the solution to the problem, before it became a problem in my application. Thanks again for your help.
0 Kudos
Message 6 of 7
(3,678 Views)
Hey Al,
I just found a problem with the code. It will only work if the values of the list items are 1-whatever in consecutive order. If the values are not as specified the highlighting will not work properly. I found that by replacing the following line of code near the bottom of the fnScrollList routine,

SetCtrlVal(scrollPanel, SCROLL_LIST, lineNum+(int) floor(nPointedToItem));

with

SetCtrlIndex (scrollPanel, SCROLL_LIST, lineNum+(int) floor(nPointedToItem)-1);

the highlighting will work properly regardless of the values of the items in the list. Just thought I'd pass it on in you. Thanks again for your help.
0 Kudos
Message 7 of 7
(3,678 Views)