InvokeMember using GetField. Field not found in VB.NET

This is probably simple, but I cannot figure out how to do this.

I have a bunch of form elements created by a form constructor declared as (in frmAquRun.Designer.vb)

Public WithEvents btnAquRunEvent1 As VisibiltyButtonLib.VisibilityButton
Public WithEvents btnAquRunEvent2 As VisibiltyButtonLib.VisibilityButton

... etc.

And I basically want to be able to provide a function number to access each of these fields. Therefore, I wrote this function. (in frmAquRun.vb)

Const EVENT_BUTTON_PREFIX As String = "btnAquRunEvent"
Public Function getEventButton(ByVal id As Integer) As Windows.Forms.Button

    Dim returnButton As Windows.Forms.Button = Nothing
    Try
        returnButton = DirectCast(Me.GetType().InvokeMember(eventButtonName, Reflection.BindingFlags.GetField Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.Instance, Nothing, Me, Nothing), Windows.Forms.Button)
    Catch ex As Exception
    End Try
    Return returnButton
End Function

But it always seems that field generation is not detected. The message in the exception is "Field" ATSIS_ControlProgram.frmAquRun.btnAquRunEvent1 "not found".

The namespace and form name in the message are correct. Any idea what I'm doing wrong?

+3
source share
2 answers

, WithEvents VB , . . _ + . 1)

, , _ BindingFlag, getter ( GetField).

, Controls :

returnButton = DirectCast(Me.Controls(eventButtonName), Windows.Forms.Button)

, , , .. .


1) VB, ( Monos vbnc), WithEvents VB .

+3

, . , add_btnAquRunEventX, remove_btnAquRunEventX fire_btnAquRunEventX. , , , , . List < > , .

VB, :

Dim events = New List<EventHandler>()
events.Add( btnAquRunEvent1 )
events.Add( btnAquRunEvent2 )

....

events( 0 )( null, EventArgs.Empty )

, . , .

0

Source: https://habr.com/ru/post/1736226/


All Articles