Click button on another form

Posted on 2008-10-25
Medium Priority
Last Modified: 2012-05-05
I have added a button on another form to perform an action. I want to open that form and click the button. Here is the code to open the form but the last line gives me an error. "Reference to a non shared member requires an object reference"

The button on the other form is public.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm As New frmPersons(frmIncidentReport.TblPersonsBindingSource)
        Dim result As DialogResult = frm.ShowDialog
        If result = Windows.Forms.DialogResult.OK Then
        End If
        frmPersons.Button2_Click(Nothing, Nothing)     '<<<<< Problem>>>>>
    End Sub
Question by:wiswalld
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4

Expert Comment

ID: 22802862
you need to reference the object instead of the class name, that would basically solve your problem,
but you really should look into another solution for your problem overall

the code you posted is trying:

creates a new form
shows it blocking the execution and saving that result for later use
you then remove the object from memory
then you have an empty if statement
and now you're trying to click a button on a non existing object, since you disposed of it before hand...

        frm.Button2_Click(Nothing, Nothing) 

Open in new window

LVL 13

Author Comment

ID: 22802872
I had the if state ment commented out so I just removed it for this post. So the problem is I dispose of the second form before calling the button click?
LVL 13

Author Comment

ID: 22802883
So how to word this?
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 13

Author Comment

ID: 22802888
Is this because the binding source on frmpersons is set from another form to that binding source?
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 22802907

LVL 13

Author Comment

ID: 22802936
Same error. I tried this with a function with same result. I removed the dispose but no luck. Basically what I want to do is copy all the fields from a datagridview to the frmpersons form after I open creating a new record based on an existing record.
LVL 13

Author Comment

ID: 22802941
Don't think this matters but this is the button click

 Public Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.CaseNumberTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(1).Value
        Me.DivisionPrecinctComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(2).Value
        Me.PersonTypeComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(3).Value
        Me.LastNameTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(4).Value
        Me.FirstNameTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(5).Value
        Me.MiddleTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(6).Value
        Me.JrTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(7).Value
        Me.StreetTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(8).Value
        Me.BuildingNumberTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(9).Value
        Me.AptNumberTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(10).Value
        Me.CityComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(11).Value
        Me.StateComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(12).Value
        Me.ZipCodeTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(13).Value
        Me.HomeNumberTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(14).Value
        Me.HomeExtTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(15).Value
        Me.WorkNumberTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(16).Value
        Me.WorkExtTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(17).Value
        Me.SSNTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(18).Value
        Me.DOBComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(19).Value
        Me.AgeFromTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(20).Value
        Me.AgeToTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(21).Value
        Me.AgeTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(22).Value
        Me.SexComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(23).Value
        Me.RaceComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(24).Value
        Me.EthnicityComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(25).Value
        Me.AKALastTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(26).Value
        Me.AKAFirstTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(27).Value
        Me.AKAMiddleTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(28).Value
        Me.AKADOBComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(29).Value
        Me.AKASSNTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(30).Value
        Me.CautionComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(31).Value
        Me.ConditionComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(32).Value
        Me.SkinComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(33).Value
        Me.HeightTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(34).Value
        Me.WeightTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(35).Value
        Me.HairComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(36).Value
        Me.EyesComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(37).Value
        Me.GlassesComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(38).Value
        Me.BuildComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(39).Value
        Me.ReligionComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(40).Value
        Me.MaritalStatusComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(41).Value
        Me.EmployedComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(42).Value
        Me.BeardComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(43).Value
        Me.MoustacheComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(44).Value
        Me.OccupationTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(45).Value
        Me.POBTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(46).Value
        Me.USCitizenComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(47).Value
        Me.EducationTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(48).Value
        Me.ResidenceStatusComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(49).Value
        Me.EmployerNameTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(50).Value
        Me.EmployerStreetTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(51).Value
        Me.EmployerBuildingNoTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(52).Value
        Me.EmployerAptNoTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(53).Value
        Me.EmployerCityTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(54).Value
        Me.EmployerStateTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(55).Value
        Me.EmployerZipCodeTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(56).Value
        Me.PictureTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(57).Value
        Me.PictureDateComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(58).Value
        Me.PictureTimeTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(59).Value
        Me.ScarsComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(60).Value
        Me.MarksComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(61).Value
        Me.TattoosComboBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(62).Value
        Me.DescriptionTextBox.Text = frmAddPersonsSearchMatch.TblPersonsDataGridView.CurrentRow.Cells(63).Value
    End Sub

Accepted Solution

_Choggo_ earned 500 total points
ID: 22802945
if you dispose it, it means you won't be using it anymore, so you should dispose it after you are actually done with the object, after the button click

if you want o access the button's method by class name you should use Shared as well, no just public

if you do frm.Button2_Click since you made the method public, it should work...


Expert Comment

ID: 22802952
You could also use webtubbs's suggestion, make the button public then through the form's object access frm and not frmPersons
LVL 13

Author Comment

ID: 22802974
I see what the problem. I was calling frmpersons.button2_Click not frm.button2_Click

You got it Choggo. I did not see that.


Expert Comment

ID: 22802986
glad to be of assistance

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question