rykk

This may well show my idiocy but here goes.

I decided to build a custom control using visual basic in Visual studio 2005. I created a control library and built a control by dragging a Text Control from the tool bar. I managed to add the properties I wanted and modified the key down and key press events to accomplish what I wanted. The idea was of course to have this custom control available in many areas in a project.

I then added the control library to the visual basic windows project. I am able to select the control from the toolbar in the project and add it to forms in the project. This all works (the control works as designed).

The problem I have is when I add code to say the got focus event of the control in the project the event never gets fired Note the control load event does fire. For example the double click, got focus, loss focus events all do not fire. The reason I need to do this is because when the control gets focus I need to set its properties related to other screen conditions.

The question is ¨C how do I get the events connected to control to work

Or better yet where does my stupidity lie



Re: Visual Basic General Custom control problem

Dick Donny

Hi

It's hard to tell exactly what the problem may be from your description but I will have a guess.

If your control only contains a text box, do I presume that that text box takes up the full area of the user control If so, you will not get the events because it is the inner text box that has the focus, receives the click etc.

If this is the root of the problem, you have a couple of options.

1. Your new class should inherit from TextBox as opposed to UserControl. This way the events are automatically exposed for you.

2. You new control should bubble up the event from the text box by handling its click, got focus whatever and raising the event out of it

Hopefully this will help you.

Richard





Re: Visual Basic General Custom control problem

rykk

I suspect you are correct.

However when I tried to inherit from TextBox I get the following error

"Base Class 'System.Windows.Forms.TextBox' specified for class 'TestIntegerField' cannot be different from the base class 'System.Windows.Forms.UserControl' of one of its other partial types"

I do not know to overcome this problem. Everything I tried made no difference.

Thanks for the response

rykk





Re: Visual Basic General Custom control problem

rykk

It got away from me early !

>You new control should bubble up the event from the text box by handling its click, got focus whatever and raising the event out of it

If the above is unsolveable How can I bubble up the event

Thanks again





Re: Visual Basic General Custom control problem

Tall Dude

I just played around with one of the few

usercontrols I've made and the 'lostfocus' event

acted funny.

The 'leave' event behaved well.

Might want to give that a try.






Re: Visual Basic General Custom control problem

Dick Donny

The easiest thing for you to do is add a new class to your project and enter the following ...

Public NotInheritable Class TestIntegerField

Inherits TextBox

..... add your own properties, methods, events etc

End Class

That should resolve any issues you have

Richard





Re: Visual Basic General Custom control problem

rykk

I am still tring to get the control library to work with no results

rykk





Re: Visual Basic General Custom control problem

Dick Donny

Hmmm .. afraid I can offer no more advice unless you can post your code.

Maybe look at bubbling the events as we discussed earlier. All this involves is handling the gotfocus/mouseXXX events of the text box and raising similar events from your control. Something along the following lines should get you started ....

public class MyControl

public event Click(byval sender as object .....

private sub TextBox1_XXX(byval sender as object, ......

raiseevent Click or Mouse or whatever

end sub

If this doesn't work, please give us some more information on what is not working and any exceptions messages/compile time messages that you are getting.

Good luck

Richard





Re: Visual Basic General Custom control problem

rykk

I have been trying all sorts of things with no success I will try your comments and get back tomorrow.

thank yuo very much.

rykk





Re: Visual Basic General Custom control problem

rykk

1) Start Visual Studio 2005
2) File->New Project->select Windows Control Library
3) It comes up with UserControl1.vb
4) Add a TextBox using the toolbar
5) Double click on the TextBox
6a) Add KeyDown event
6b) Add a KeyPress Event
7) Add some variables and some properties
8) Build the Library
9) Close the Library
10) Open my actual project
11) Add the library to the toolbar
12) Select the control UserControl1.vb in the toolbar and add it to a form
13) Add the following code to the control load and got focus events
Private Sub UserControl11_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UserControl11.Load
Debug.Print("Load")
End Sub
Private Sub UserControl11_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles UserControl11.GotFocus
Debug.Print("GotFocus")
End Sub
14) When the project is tested the control load event is executed but the gotfocus never gets executed

Complete Code of control
************************
Imports System
Imports System.Windows.Forms
Imports System.Windows.Forms.UserControl
Imports System.Drawing
Imports System.ComponentModel
Imports System.Collections
Imports Microsoft.VisualBasic
Public Class UserControl1
' Inherits TextBox
Private invalidKeystroke As Boolean = False
Private minIntValue As Integer = -999999
Private maxIntValue As Integer = 999999
<BrowsableAttribute(True)> Public Property MinimumValue() As Integer
Get
Return Integer.Parse(Me.minIntValue, Globalization.NumberStyles.AllowLeadingSign + Globalization.NumberStyles.AllowThousands)
End Get
Set(ByVal value As Integer)
Me.minIntValue = value
End Set

End Property
<BrowsableAttribute(True)> Public Property MaximumValue() As Integer
Get
Return Integer.Parse(Me.maxIntValue, Globalization.NumberStyles.AllowLeadingSign + Globalization.NumberStyles.AllowThousands)
End Get
Set(ByVal value As Integer)
Me.maxIntValue = value
End Set

End Property
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
invalidKeystroke = False
If Not Char.IsDigit(Chr(e.KeyCode)) Then
' Test keystroke entered
invalidKeystroke = True
End If
End Sub

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
' Check for the flag set in the KeyDown event
If invalidKeystroke = True Then
' Dump invalid character
e.Handled = True
End If
End Sub

End Class

15) The suggestion (to get all the events to work) was to add "Inherits TextBox"
16) If you return to the control library and uncomment the inherits line the following error is displayed
Error 1 Base class 'System.Windows.Forms.TextBox' specified for class 'UserControl1' cannot be different from the base class 'System.Windows.Forms.UserControl' of one of its other partial types. C:\ca\wim\WindowsControlLibrary1\WindowsControlLibrary1\UserControl1.vb 9 14 WindowsControlLibrary1

17) I can not find a way around this





Re: Visual Basic General Custom control problem

Dick Donny

Hi Rykk

Looks like you are trying to code a text box that only accepts numeric digits .. try the following ...

1. Start Visual Studio

2. Create new windows application (forget the user control library, it's not what you need)

3. Add a new class ... eg MyControl.vb

4. Delete all the code in the class and enter the following .....

Public Class ControlXXX

Inherits TextBox

Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)

If Not Char.IsDigit(Chr(e.KeyCode)) Then

e.SuppressKeyPress = True

Else : MyBase.OnKeyDown(e)

End If

End Sub

End Class

To hook into the new controls events, simply attach a handler as you would any other control

Good luck

Richard





Re: Visual Basic General Custom control problem

rykk

Dick Donny what you suggested worked.

However the example I posted was just a dummy to show that I could not get the inherits clause to work in the control library.

Why is the control library not what I want

The reason I want to use the control library is so the controls I develop can be added to many different projects without adding the code for all the controls to each project. If I add the code to each project each time I want to modify one of the controls I will have to make the changes in all the projects.

Thank you very much for a soultion that at least works.

rykk





Re: Visual Basic General Custom control problem

Dick Donny

Hi

Good to hear :)

I meant that you do not need to create a user control. By all means, add your new class to a shared assembly and reference that assembly from many projects. Your new class is still a control ... think of it as a 'super' text box :) ... and can be exposed publically.

Richard





Re: Visual Basic General Custom control problem

rykk

Again thank you for the help.

rykk





Re: Visual Basic General Custom control problem

S Wolfe

I have a question also regarding the need for a Form versus a UserControl. Let me tell you what I want to do:

I'm unhappy with some of the oddities within the TableLayoutPanel control, and have extensively modified it for my own use within a particular project, but need the same modified functionality in other areas as well. I would like to create a derivative of the TableLayoutPanel that I can easily slap in wherever I need it.

I presumed that the first steps would be to open a new UserControl project and have the class inherit the TableLayoutPanel.

It gives me:
base 'System.Windows.Forms.TableLayoutPanel' class specified for class 'UserControl1' cannot be different from the base class 'System.Windows.Forms.UserControl' of one of its other partial types

Could you explain for me the reason why a UserControl is the incorrect project-type

Thanks!