Inheritance/user control problem

I have the following event in a VB2005 User Control:

Public Event ButtonClicked(ByVal Sender As Object, ByVal E As System.EventArgs)

This code in the User Control is the only code that calls the event:

Private Sub HandleButtons(ByVal Sender As Object, ByVal E As System.EventArgs) Handles btnFirst.Click, btnLast.Click, btnNext.Click, btnPrev.Click

Dim BTN As Button = DirectCast(Sender, Button)

Select Case BTN.Name

Case "btnFirst"

CurrentRecord = 1

Case "btnLast"

CurrentRecord = RecordCount

Case "btnNext"

CurrentRecord += 1

Case "btnPrev"

CurrentRecord -= 1

End Select

RaiseEvent ButtonClicked(Sender, E)

End Sub

I added the User control to a form (FORM1) and add the following code:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

With NavControl1

.RecordCount = 10

.CurrentRecord = 1

End With

End Sub

Private Sub HandleButtons(ByVal Sender As Object, ByVal E As EventArgs) Handles NavControl1.ButtonClicked

Dim CTRL As Control = DirectCast(Sender, Control)

Dim strBTN As String = String.Empty

Select Case CTRL.Name

Case "btnFirst"

strBTN = "FIRST"

Case "btnLast"

strBTN = "LAST"

Case "btnNext"

strBTN = "NEXT"

Case "btnPrev"

strBTN = "PREVIOUS"

End Select

MessageBox.Show(String.Format("The {0} button was clicked", strBTN))

End Sub

FORM1 runs without any problems. If I remove the code from FORM1 and inherit a new form (FORM2) from FORM1 and add the same code to FORM2 I get an error “Button Click event is read only and can not be changed” when I attempt to open FORM2 in the designer.

Can anyone see what’s causing the problem What can I do to correct it



Answer this question

Inheritance/user control problem

  • Shailendra18

    Nope.



  • Bassel Samir Banbouk

    try using the overridable attribute especially if you are going to want to change the code in the inherited form:

    Private Overridable Sub HandleButtons....



  • bhoffmancsi

    You're on to something. I tried to reproduce what you did and got major problems. At one point, I got both Form1 and Form2 unable to display their designers with "Catastrophic failure" error messages (0x8000FFFF).

    I have to admit that I don't know enough about event inheritance to really give a good answer. Seeing an error message from trying to implement the ButtonClicked event handler in Form2 kinda made sense: you can only inherit the events the the base form raises.

    So I tried to rework it by having the UserControl raise an event in Form1:

    Public Class Form1
    Public Event ButtonClicked2(ByVal Sender As Object, ByVal E As System.EventArgs)

    Private Sub UserControl11_ButtonClicked(ByVal Sender As Object, ByVal E As System.EventArgs) Handles UserControl11.ButtonClicked
    RaiseEvent ButtonClicked2(Sender, E)
    End Sub
    End Class

    Public Class Form2
    Inherits Form1

    Private Sub Form2_ButtonClicked(ByVal Sender As Object, ByVal E As System.EventArgs) Handles MyBase.ButtonClicked2
    Debug.Print("Click")
    End Sub
    End Class

    No "Click"....



  • donall

    I don't want to change or overwrite any of the code in the User Control and FORM1 has no code. Changing the sub in FORM2 from "ButtonClicked" to "ButtonTest" did not help.
  • Dan.B

    Make sure that the accessibility for NavControl1 is Public, Friend, or Protected.

    Hope that helps,



  • Mario Lucin

    Try changing the way you declare the event. Since this is a standard format event handler, declare it as such:

    Instead of:
    Public Event ButtonClicked(ByVal Sender As Object, ByVal E As System.EventArgs)

    Try:
    Public Event ButtonClicked As EventHandler

    HTH



  • Inheritance/user control problem