Author Topic: Call form from controls  (Read 1560 times)

Phrog30

  • Guest
Call form from controls
« on: May 11, 2017, 09:28:15 AM »
Is there a way to call a form in the AHMI application from the AHMIcontrols application?  Will I have to create the form in the controls application to make this work?

James

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Call form from controls
« Reply #1 on: May 11, 2017, 09:48:00 AM »
Can you give an example of what you want to do? Are you trying to do something like open a form from clicking on a BasicLabel?

Phrog30

  • Guest
Re: Call form from controls
« Reply #2 on: May 11, 2017, 09:55:44 AM »
I've created a custom message box that I want to use, it's used to display security rights.  It will be called on a custom button, on click event if the user doesn't have rights it will call this form.  I took your basic button and added security to it.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Call form from controls
« Reply #3 on: May 11, 2017, 10:23:50 AM »
You can add a property to point to a form:
Code: [Select]
Private m_FormToOpen as Form
Public Property FormToOpen as Form
Get
   Return m_FormToOpen
End Get
Set (value as Form)
   m_FormToOpen=value
End Set
End Property

then in the Click event handler:
Code: [Select]
If m_FormToOpen isnot Nothing then
   m_FormToOpen.Show
End If

Phrog30

  • Guest
Re: Call form from controls
« Reply #4 on: May 11, 2017, 10:53:20 AM »
You can add a property to point to a form:
Code: [Select]
Private m_FormToOpen as Form
Public Property FormToOpen as Form
Get
   Return m_FormToOpen
End Get
Set (value as Form)
   m_FormToOpen=value
End Set
End Property

then in the Click event handler:
Code: [Select]
If m_FormToOpen isnot Nothing then
   m_FormToOpen.Show
End If

I tried this code and it only allows me to call the form the object is in.  I used your code for the list (code from form change button), it didn't return anything.  This form that I want to call will always be the same, it will not change.  Another part of this is that I need to pass a value to this form when called.  Basically just what I'm doing now with a messagebox:
Code: [Select]
MessageBox.Show("You must login as """ & securityText & """ to access...", "Insufficient Rights", MessageBoxButtons.OK, MessageBoxIcon.Stop)
But, with a custom form.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Call form from controls
« Reply #5 on: May 11, 2017, 01:07:17 PM »
The attached project shows how the custom button that exists in a separate class library can open a form that exists in the man application.

Phrog30

  • Guest
Re: Call form from controls
« Reply #6 on: May 11, 2017, 02:28:05 PM »
Yours is doing the same as mine, it only allows to select the form the button is on.  In your example only form1 is available.  If I create a form2 and place a button on it, then my only choice is form2.  Plus, like I said there is no reason to select a form, it's always the same.  Additionally, if this did work how would I pass a variable to the form I want to open?  If that's not possible then all of this really doesn't matter.


James


Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Call form from controls
« Reply #7 on: May 11, 2017, 03:04:48 PM »
You cannot select a form in the designer. You have to set the property in code as I had shown in the example. It is a limitation of the VS designer.

Phrog30

  • Guest
Re: Call form from controls
« Reply #8 on: May 11, 2017, 03:12:15 PM »
Ok, I guess I missed that.  Having to write code, even simple code, defeats the purpose of using the components.  Is there any way to pass this form as a global variable?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Call form from controls
« Reply #9 on: May 11, 2017, 03:15:12 PM »
If the form never changes, you can embed it into the control itself. Why not put the form in the same project of the control?

Phrog30

  • Guest
Re: Call form from controls
« Reply #10 on: May 11, 2017, 03:23:19 PM »
I can, but it uses resources from AHMI application.  I'm trying to eliminate doing things double.  I was able to create a variable form and load it, but when I tried to load again I got "Cannot access a disposed object in instance VB.net".  I assume I have to create a new instance each time?  This is not that important, just something on my todo list.  I will fumble through it.  Thanks for the help Archie.

James


Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Call form from controls
« Reply #11 on: May 11, 2017, 03:57:56 PM »
You could consolidate your resources in a shared class library as explained here:

http://geekswithblogs.net/MarkPearl/archive/2011/03/02/centralizing-a-resource-file-among-multiple-projects-in-one-solution.aspx

Then add a reference to that class library in both projects.

Phrog30

  • Guest
Re: Call form from controls
« Reply #12 on: May 11, 2017, 05:45:00 PM »
I will definitely look into that.  Thanks.