Jump to content
John Lokanis

Setting the key focus on an array control programatically

Recommended Posts

I am trying (and failing) to set the key focus to the first element of an array control.  I want my dialog window to appear and have the cursor placed in the first element of a specific array control so the user can just start typing without first having to click on the element in the array.

This seems like it would be a simple things to do, so I am hoping that I am just making some bonehead mistake.

I have tried setting key focus, array element key focus, selection start and selection size.  None have worked.

 

Anyone know if this is possible?

 

I have attached an example VI with my attempts.

array key focus.vi

 

thanks for any wisdom...

 

-John

Share this post


Link to post
Share on other sites

First you need to activate the panel. Once you do that, you can set key focus of the array element and it will work.

 

post-4441-0-30050800-1422050782.png

Share this post


Link to post
Share on other sites

Thanks Darren.  Didn't know you have to get a property node for the string in the array.  I figured that the Array Element.Key Focus would be the same thing.

BTW: the panel activation is not needed, apparently.  I got it to work without it.

One issue: it seems to put the focus on the last element you clicked in before running the VI.  So, if you click in element 3 while in edit mode, then end text entry without typing anything, when you run the VI, the third element will have the cursor in it.

Is there any way to control which element in the array gets the key focus?

 

And is any of this documented anywhere?  (the bit about getting the ref to the element control)

Edited by John Lokanis

Share this post


Link to post
Share on other sites

Ok, tried this in my actual application.  Didn't work.  Might be because my application has this VI as a floating window.  Or might be some other effect.

I tried making the example VI floating as well and found the sometimes the key focus did not work.  Seems to be flaky.

Does it matter if I set this from a sub-vi by passing in the ref to the string element of the array?

Also, does it matter if I initialize the array with empty strings after I set the key focus?

Share this post


Link to post
Share on other sites

I am not seeing a way to get the key focus to always go to the first element (as opposed to going to the last one that was clicked).

 

I'm also not aware of any documentation on these particular array idiosyncrasies.

 

You need the panel activation if you're running the VI from somewhere other than the panel. For example, if you're on the diagram and you press Ctrl-R, the key focus doesn't take.

Share this post


Link to post
Share on other sites

I have tried setting the panel activation in my actual application. I also set the FP to Frontmost.  Then I set the key focus on the string to true.  Not working.  Something strange must be going on.

Share this post


Link to post
Share on other sites

Yeah, I can't think of anything else to try. There very well may be a bug with setting key focus on an array element in a floating VI window.

Share this post


Link to post
Share on other sites

Feel free to send some sample code to the AEs so they can file the necessary CARs.

Share this post


Link to post
Share on other sites

Hi there,

The only way I found out is to set the value of the index and then the key focus. The problem that remain is that the first element show is the index value... For a GUI that you fix the size of the array, it could be confusing for the user.

 

Benoit

Share this post


Link to post
Share on other sites

There is a PITA way of getting what you want.

Set the Array index to the element you want to set focus to.  Set the visible size of the Array to a single element (The one you want to set).  Set the focus. Set the size of the array back to the original settings.  Re-set the Index of the Array (if required).

Like I said, PITA.  By forcing the array to show only a single element, the control reference to the array element will be forced to the one you want.  I haven't tried doing this in advance and then using several array element references, but I think they will all change in unison (as the array element is a property of the Array).

Share this post


Link to post
Share on other sites

That doesn't sound too terrible if it can be wrapped into a single function, with a defer and undefer front panel updates at the beginning and end (so the user doesn't see weird resizing of array controls)

Share this post


Link to post
Share on other sites

I really don't like defer front panel updates (unless working with tree controls), so for me that makes it a PITA.

Share this post


Link to post
Share on other sites

Dear Hooovahh,

Here is what i tried and it seems to not work. If i do not set back the Array parameter, it works. But the goal is to make that transparent to the user. You have an idea on how to fix that?

used LabVIEW 2017

Benoit

key focus array elelement.vi

Share this post


Link to post
Share on other sites

I couldn't make that work that way either...  It is possible something changed in LabVIEW since I used this trick.  It seems that setting the Index Value on the array makes focus go away.  And setting the KeyFocus any other time just sets the first element...darn.

But one method that does still work (and sucks even more) is you can navigate using various key combinations and you can simulate key presses to get the desired result.  So set KeyFocus to the string control.  This sets the focus on string control in the first index of the array.  Now if you press CTRL+Shift+Up Arrow, and your focus will be on the cluster element at index 0.  Now press Tab and it will be on the second cluster element.  Now press CTRL+Shift+Down Arrow and your focus will be on the second string control.  If you pressed tab twice it would be the third.  So you can programatically set the string control focus you want, but there is several complications with this method.  If you aren't looking at index 0 you need to do additional math to figure out what the index value should be, and how many times a tab is needed.  If I get some time today I'll try to post an example VI.  Mondays after a trip are always crazy

Share this post


Link to post
Share on other sites

Dang so I can't even get this to work at the moment.  Using a physical keyboard CTRL+Shift+Up and Down does what I'd expect.  But the Keyboard Event code I have for some reason isn't doing what it should.  CTRL, and Shift seem to be working, or at least my on screen keyboard shoes them being activated when they should.  And the Up and Down appear to work as it goes up and down a line of text.  But when I put it in my application, it just doesn't do what I'd expect.  This is also with a whole bunch of waits added in case windows doesn't like fast key presses all the time.  I know I used this technique before but I can't seem to find the code that I used.

Share this post


Link to post
Share on other sites

Well thanks to you for all this time spend on trying something.

I also found a bug in LabVIEW.

Try the VI i joined.

If we start with any element that is not the first one, it goes at current index + index specified. ***BUG***

How to report a bug to NI?

 

Benoit

key focus array elelement LabVIEW BUG.vi

Share this post


Link to post
Share on other sites

Benoit: I just saw this post while hunting around for a bit of help myself. I looked at your VI. It's not a bug. You aren't ever setting the active element of the array -- the same physical position remains active unless you click with the mouse or use TAB key to move to the next element. To the best of my knowledge, there is no programmatic way to set the active physical slot of the array control other than simulating a mouse click or something exotic like that.

The array control is not a particularly good control for UI, in case you hadn't noticed. It was never designed for a interactive front panel. The one in NXG is better.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.