PDA

View Full Version : Dotnet controls fighting for focus


lo
11-04-2010, 01:23 PM
I'm trying to have a dotnet combobox+listbox control, where when you press the combobox, it doesn't drop down, but instead a listbox opens (which would allow mutliple selections).
Up to here everything works great, the problem is that I can't get the listbox to maintain its focus.
When I click inside the listbox to select items, other controls BEHIND the listbox get focus, even if they are not visible!

Does anyone know what I'm doing wrong?

See attached test code.
try(destroydialog FocusTest)catch()

rollout FocusTest "test" width:240 height:150
(
dotNetControl cb1 "System.Windows.Forms.Combobox" width:200 height:20 pos:[20,20]
dotNetControl cb2 "System.Windows.Forms.Combobox" width:200 height:20 pos:[20,60]
dotNetControl lb "System.Windows.Forms.ListBox" width:200 height:100 pos:[-200,20]

on focusTest open do
(
lb.items.addRange #("1","2","3","4","5","6","7","8")
)

on cb1 MouseDown do
(
cb1.droppedDown=false
lb.pos=[20,40]
lb.BringToFront()
)

on FocusTest lbuttondown pos do
(
lb.pos=[-200,20]
)
)
createDialog FocusTest

Insanto
11-04-2010, 01:40 PM
hmm you could try your luck with setFocus or temporarily move the hidden ones

lo
11-04-2010, 01:42 PM
I'm reluctant to move the other controls, since it's about 20 of them, and it would make the interface look weird every time you click a combobox.

what do you mean with setfocus? where would I be using it?

Kameleon
11-04-2010, 01:52 PM
Easily solved by changing the order on how the controls are created:

try(destroydialog FocusTest)catch()

rollout FocusTest "test" width:240 height:150
(
dotNetControl cb1 "System.Windows.Forms.Combobox" width:200 height:20 pos:[20,20]
dotNetControl lb "System.Windows.Forms.ListBox" width:200 height:100 pos:[-200,20]
dotNetControl cb2 "System.Windows.Forms.Combobox" width:200 height:20 pos:[20,60]


on focusTest open do
(
lb.items.addRange #("1","2","3","4","5","6","7","8")
)

on cb1 MouseDown do
(
cb1.droppedDown=false
lb.pos=[20,40]
cb2.visible=false

)

on FocusTest lbuttondown pos do
(
lb.pos=[-200,20]
cb2.visible = true
)
)
createDialog FocusTest

Is this it? Cheers.

lo
11-04-2010, 02:50 PM
Thanks for your responses.

Your solution works in my test example, but not really for my intended use, since I was hoping to use the same listbox for many different comboboxes, moving it to a different place each time, and not have to create a listbox for each one of them.

Do you know if there is a way to change the 'creation order' (or really, the z-ordering), after the controls are already created? .BringToFront() does not seem to work.

Insanto
11-04-2010, 03:33 PM
not afaik

heres a little example of the setFocus fn


rollout ro ""
(
editText et1 ""
editText et2 ""
editText et3 ""
editText et4 ""

timer click active:true interval:500

on click tick do
(
local wich = mod click.ticks 5
if wich == 0 then wich += 1
local item = ro.controls[wich]

if classOf item == editTextControl do setFocus Item
)

)

createDialog RO

tryn write something :p

lo
11-04-2010, 03:47 PM
Thanks

I'm familiar with the setFocus() method, I was wondering how I could use it in this situation.

if I give the ListBox focus every time it loses focus, I could never focus any other control. :shrug:

Kameleon
11-04-2010, 03:53 PM
Have you tried the BringToFront method in a windows form instead of a rollout? just to check if it works as expected in a form.

Insanto
11-04-2010, 03:59 PM
well yous aid you only want it to not loose focus when you select an item and loose it, so put it in there, should work ... theoretically :D

Kameleon
11-04-2010, 04:34 PM
It's a rollout thing, if you can use a Form great, I think your problems will be solved, if not you can add a panel to the dialog and create the controls there, this way you can either use the .Visible property or by moving in and out the listbox (I assume the that using the visible property is better).

Here's the code:

try(destroydialog FocusTest)catch()

rollout FocusTest "test" width:240 height:150
(
dotnetcontrol panel "System.Windows.Forms.Panel" pos:[0,0] width:240 height:150
local cb1,cb2,lb


fn cb1MouseDown =
(
cb1.droppedDown=false
--lb.Location = dotnetobject "System.Drawing.Point" 10 30
lb.Visible=True
lb.BringToFront()
)

on FocusTest open do
(
cb1 = dotnetobject "System.Windows.Forms.Combobox"
cb1.Location = dotnetobject "System.Drawing.Point" 10 10
cb2 = dotnetobject "System.Windows.Forms.Combobox"
cb2.Location = dotnetobject "System.Drawing.Point" 10 40
lb = dotnetobject "System.Windows.Forms.Listbox"
lb.Location = dotnetobject "System.Drawing.Point" 10 30
lb.Visible = False
--lb.Location = dotnetobject "System.Drawing.Point" -1000 0
lb.items.addRange #("1","2","3","4","5","6","7","8")

dotnet.addEventHandler cb1 "MouseDown" cb1MouseDown

panel.controls.add cb1
panel.controls.add cb2
panel.controls.add lb
)

on panel Click do
(
lb.Visible=False
--lb.Location = dotnetobject "System.Drawing.Point" -1000 0
)
)

createDialog FocusTest

lo
11-04-2010, 05:26 PM
Thanks, this will prove very helpful. Looks like I have to rewrite a lot of stuff :argh:

lo
11-07-2010, 03:49 PM
Just noticed something... in your panel example... I can't seem to write any text in the comboboxes, the max viewport seems to have focus no matter what. Is there any way around this? :hmm:

edit: Got it... enableAccelerators = false :thumbsup:

edit2: Do you know if there is an event of the Panel class which can catch if any of the children controls of the panel get focus? 'enter' and 'gotfocus' don't seem to catch this, and it's tedious to add a 'gotfocus' event for each of the panels controls just to disable the accelerators.

Kameleon
11-08-2010, 11:11 AM
Have you tried the ContainFocus event?

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.containsfocus.aspx

lo
11-08-2010, 11:36 AM
I'm afraid I don't understand. The link you posted describes a .ContainsFocus property, not an event. How can I call the changing of a property as an event?

Or did you mean I should set up a timer to periodically check whether or not the panel contains focus?

Kameleon
11-08-2010, 12:19 PM
Sorry, my bad, I thought it was an event! I'll try to check the code to see if I can do what you need.

Kameleon
11-08-2010, 12:31 PM
I just noticed one thing... why do you want to disable the accelarators inside of each panel? I've just added a enableAccelerators = false in the FocusTest open event and it seems to work fine, at least for one panel... dunno how it works with more panels.

lo
11-08-2010, 01:36 PM
I works the first time, but try this:

1) run the script
2) type something in the combobox (works fine so far...)
3) click somewhere in the viewport to defocus the rollout
4) try typing something again...

No worries though, I added an event for each combobox.

CGTalk Moderation
11-08-2010, 01:36 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.