Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

hey folks,

 

i have a Windows Form and on it two user controls, the first one, UserControl1, has a TreeView control in it and the second one, UserControl2, has a TextBox in it, amongst other things.

 

So i want when i click on any of the items in TreeView from UserControl1 the textbox in UserControl2 to receive input focus. So i wrote this little routine that's supposed to be doing just that, but it doesn't. Here is the code:

 

Private Sub FocusItem()

For Each workItem As UserControl2 In Me.Panel1.Controls

If workItem.TextBox1.Text = "" Then

workItem.TextBox1.Focus()

Exit Sub

End If

Next 'End For

End Sub 'End FocusItem

 

[on the main form i place a bunch of controls of type UserControl2 in panel control Panel1]

 

To ensure that this function is called when the TreeView in UserControl1 is clicked i have this routine:

 

Private Sub myUserControl1_AfterSelect(ByVal sender As Object, ByVal e As System.EventArgs) Handles myUserControl1.AfterSelect

 

................................

................................

 

Call Me.FocusItem()

End Sub

 

The problem is that it doesn't work. Well it doesn't when i click with the mouse on an item in the TreeView, but it does work when i switch between items using the arrow keys on the keyboard, i.e. if I click on the TreeView the TextBox does not receive input focus, but if I press a key it does. I've established through testing that the routine myUserControl1_AfterSelect is being called in both occasions, but for some reason the TreeView won't give up input focus when clicked on. Any ideas?

Posted

ListView? There is no ListView, perhaps you mean TreeView.

In my UserControl UserControl1 i have a TreeView control amongst other things. So in UserControl1 I have something like this:

 

Public Class UserControl1

Inherits System.Windows.Forms.UserControl

 

Friend WithEvents TreeView1 As System.Windows.Forms.TreeView

TreeView1 = New System.Windows.Forms.TreeView

 

............................

 

Public Event AfterSelect(ByVal sender As Object, ByVal e As EventArgs)

 

Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

..........................

..........................

 

RaiseEvent AfterSelect(sender, e)

End Sub 'End TreeView1_AfterSelect

 

End Class

 

In other words when TreeView1 is clicked its event AfterSelect fires my custom UserControl event AfterSelect. I don't think the fact that they have the same name matters.

Is that what you were asking?

 

What's the ListView event you're using?

 

Alex :p

Posted

Sorry about the TreeList mistake...

 

Can you verify if the textbox doesn't get focus at all or gets it and looses it right after?

 

Alex :p

Software bugs are impossible to detect by anybody except the end user.
Posted

actually yeah, i already did this, it does get the focus for a split second but then loses it.

any ideas why?

what's the difference between clicking on a node in the TreeView and using the arrows on the keyboard to switch nodes?

 

Sorry about the TreeList mistake...

 

Can you verify if the textbox doesn't get focus at all or gets it and looses it right after?

 

Alex :p

Posted

Are you doing anything else on the caller component after setting the focus on the textbox?

 

Because if you are it takes the focus to it self again...

 

Alex :p

Software bugs are impossible to detect by anybody except the end user.
Posted

nope, not doing anything else after setting focus to the textbox, unless the AfterSelect TreeView event exhibits some kind of behavior i am not aware of.

look at my first post, the last statement in routine myUserControl1_AfterSelect is a call to the function doing the focusing. nothing is supposed to be happening after that, although i may be wrong.

 

Are you doing anything else on the caller component after setting the focus on the textbox?

 

Because if you are it takes the focus to it self again...

 

Alex :p

  • Leaders
Posted

it seems you are calling an AfterSelect event which is nothing to do with a treeview :rolleyes:

Public Event AfterSelect(ByVal sender As Object, ByVal e As EventArgs)

 

Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

..........................

..........................

 

RaiseEvent AfterSelect(sender, e) ' < Here you are calling the wrong afterselect i guess

End Sub 'End TreeView1_AfterSelect

you need to be calling TreeView1_AfterSelect not AfterSelect , eg:

[color=Green]'/// in a sub or whatever you use to call afterselect of the treeview...[/color]
TreeView1_AfterSelect(sender , e) [color=Green]'/// calls the TreeView's AfterSelect event.[/color]
[color=Green]'/// not RaiseEvent AfterSelect(sender, e)[/color]

Posted

i could be mistaken but i don't think you are understanding what i was saying.

 

please look at my code carefully. i have a UserControl called UserControl1.vb. It has a TreeView control in it, amongst other controls. Unless I explicitly expose it to the rest of my application somehow, the TreeView's AfterSelect event will only be available inside UserControl1.vb. So the way to expose it is to declare a public event for my user control and perform a call to it when TreeView's AfterSelect event occurs.

 

it seems you are calling an AfterSelect event which is nothing to do with a treeview :rolleyes:

 

you need to be calling TreeView1_AfterSelect not AfterSelect , eg:

[color=Green]'/// in a sub or whatever you use to call afterselect of the treeview...[/color]
TreeView1_AfterSelect(sender , e) [color=Green]'/// calls the TreeView's AfterSelect event.[/color]
[color=Green]'/// not RaiseEvent AfterSelect(sender, e)[/color]

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...