User Control woes

ALX

Well-known member
Joined
Nov 16, 2005
Messages
253
Location
Columbia, SC
Programming Experience
10+
In several of my forms I have a user control that is a panel containing 6 textboxes. In the "Validating" event handler for the textboxes, I want to return control, briefly, to the parent form to update some charts, etc. In the user control, I get a reference to the parent form by using
VB.NET:
Expand Collapse Copy
 Dim Frm As Form = Me.FindForm
So far, I've only been able to use
VB.NET:
Expand Collapse Copy
 Frm.Invalidate
to briefly return control to the parent form. I'd like to avoid that since it causes a complete repainting of the parent form (flickering with each entry in the textboxes), and I really only need to update a few items on the parent form.
I have tried "Activecontrol = Frm" (which plays havoc with the rest of my textbox code);
"Frm.Activate" (which does nothing);
"Frm.Focus" (which might be working ??? until the OS switches focus to the next textbox);
...Can anyone steer me in the right direction... Thanx !
 
Add an event to the usercontrol and let the parent form manage itself and its child objects, for example:
VB.NET:
Expand Collapse Copy
Public Event UpdatesReady
When the criteria is met in usercontrol code do:
VB.NET:
Expand Collapse Copy
RaiseEvent UpdatesReady
Subscribe to this event in the parent form and in the event handler add what you think should be done inside parent form when this event happen, like:
VB.NET:
Expand Collapse Copy
Sub usercontrol1_UpdatesReady() Handles usercontrol1.UpdatesReady
  'update some charts, etc.
End Sub
 
Is there a simple way to have the <ENTER> key get processed as if it were the tab key in these textboxes, so the user can press either key to shift the focus for the textboxes? I have come up with some ridiculously convoluted and verbose methods that don't work very well.

VB.NET:
Expand Collapse Copy
[SIZE=2][COLOR=#0000ff]Protected [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Overrides [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] ProcessKeyPreview([/SIZE][SIZE=2][COLOR=#0000ff]ByRef[/COLOR][/SIZE][SIZE=2] m [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.Message) [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean
[/COLOR][/SIZE]
 
    [SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] m.WParam.ToInt32 = 13 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]       Dim[/COLOR][/SIZE][SIZE=2] ctrl [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Control
[/SIZE]
 
[SIZE=2][COLOR=#0000ff]       For [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] ctrl [/SIZE][SIZE=2][COLOR=#0000ff]In [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].NumPanel.Controls
[/SIZE][SIZE=2][COLOR=#0000ff]           If[/COLOR][/SIZE][SIZE=2] ctrl.GetType.Name = [/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2](TextBox).Name [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]               If[/COLOR][/SIZE][SIZE=2] ctrl.Focused = [/SIZE][SIZE=2][COLOR=#0000ff]True [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]                   ctrl = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].NumPanel.GetNextControl(ctrl, [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]                   If[/COLOR][/SIZE][SIZE=2] ctrl.TabIndex > 6 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2] ctrl = NumBox1
                    NumBox_Focus(ctrl.TabIndex) [/SIZE][SIZE=2][COLOR=#008000]'This sub just shifts focus to the textbox (# tabindex)
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]               End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]               Return [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]           End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]       Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]   Else
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]       Return [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]   End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]

Using this code, the focus shifts three textboxes per <ENTER> key stroke. It seems that this function gets called three consecutive times by the OS. There has got to be a better way !
 
Last edited:
OK... by observing the different values in "m.msg" I found that the value in the "m.msg" field changes with each consecutive call to this overridding function. I can make this code work by changing the second line of code to filter out all but one of those values...
VB.NET:
Expand Collapse Copy
If m.WParam.ToInt32 = 13 And m.Msg.ToString = 258 Then
The problem is that I don't know what I'm doing here and can't find any documentation about it. Will this method work on all .NET systems? More importantly; What's the recomended way to do this?
 
Back
Top