2

The question title may not be correct in this case, but I can't seem to find the answer to my problem any where else.

I have created a custom text box that is essentially a textbox within a panel within a user control. When I add this textbox to a form and want to use the eventhandlers from the textbox specifically, that is where things start to enter a grey area for me.

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
Fluffy Sebbert
  • 347
  • 2
  • 12
  • A UserControil is a little like a mini form - you would access the events from the UC Control window(s) – Ňɏssa Pøngjǣrdenlarp Apr 04 '16 at 12:28
  • so I would define the event in the UC and then override it in the form that I have it defined in? – Fluffy Sebbert Apr 04 '16 at 12:30
  • Typically a UC uses several controls for some task like Define New Product or a Search function, then raises custom events related to that logical operation (`SearchComplete`). If you want the control events to be caught by the form, you have to ask why there is a UC, why not put the panel on the form? Otherwise, you can "bubble up" or forward the events you need to pass to the form. More than 2 and you have to question again what value the UC adds. – Ňɏssa Pøngjǣrdenlarp Apr 04 '16 at 12:35
  • the efficiency of time and amount of code written. I'm using this UC as a re-styled textbox on multiple forms. I feel dumb, but I don't know how to forward an event to another form. I think the answer might be here http://stackoverflow.com/questions/7880850/how-do-i-make-an-event-in-the-usercontrol-and-have-it-handeled-in-the-main-form but i'm having trouble making sense of it. – Fluffy Sebbert Apr 04 '16 at 12:48
  • A custom control (not quite the same as a UC) would be more appropriate. All the standard events would automatically and still work the same. – Ňɏssa Pøngjǣrdenlarp Apr 04 '16 at 12:51

1 Answers1

2

You need to define the events you want to use inside your user control

For example if you want to fire the TextChanged event, when the TextChanged event of the textbox fires:

Public Class MyUserControl : Inherits Control

    Public Shadows Event TextChanged(sender As Object, e As EventArgs)

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        RaiseEvent TextChanged(sender, e)
    End Sub
End Class

Note that you need to declare it Shadows to avoid a conflict with the base class

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • Thanks. I figured it out on my own, but don't forget to add that when defining the event for the UC in the form outside is simply... `Private Sub UC_TextChanged(sender As Object, e As EventArgs) Handles UC.TextChanged` – Fluffy Sebbert Apr 04 '16 at 13:24