Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 680663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:23:51+00:00 2026-05-14T01:23:51+00:00

This is probably a simple one but I can’t seem to figure it out.

  • 0

This is probably a simple one but I can’t seem to figure it out.

I have a bunch of form items created by the form designer 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 supply a number to a function access each of these fields. So 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 to be generating field not found exceptions.
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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:23:51+00:00Added an answer on May 14, 2026 at 1:23 am

    The problem is that for WithEvents fields, VB actually creates a property that does the necessary event handler attaching and detaching. The generated property has the name of the field. The actual backing field gets renamed to _ + original name.1)

    So in order for your code to work just prefix the button name by _ or use the BindingFlag that corresponds to the property getter (instead of GetField).

    Alternatively, you can do this a lot easier by using the Controls collection of the form:

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

    But beware that this only works if the button is top-level, i.e. not nested within a container control on the form.


    1) This is an implementation detail of the VB compiler but it’s portable (especially to Mono’s vbnc compiler) since the handling for WithEvents fields is described in great detail in the VB language specifications.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if this is probably a simple fix but I can't think of one.
This is probably a simple question that I am just missing but I have
This is probably something very simple, so forgive my blonde moment :) I have
I probably am missing something very simple here, but I cannot see it. MSAccess
the problem is probably simple, the post is longer than I wished, but I've
I haven't, but I don't say there isn't one. All of the C# developers
I am using Visual Basic 2010 and I am makeing a simple login app
I've got a simple class called object that I'm having a problem with. Theres
I have a set of custom user controls, of type ChartControl. I want to
Whiteboard Overview The images below are 1000 x 750 px, ~130 kB JPEGs hosted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.