using the sender and e parameters (1 Viewer)

buratti

Registered User.
Local time
Today, 12:11
Joined
Jul 8, 2009
Messages
234
First off forgive me because I am totally new to VB.net. I know a little VBA for Access and if I mess up my terminology, bear with me.

What I am trying to do is open form2 from form 1, but in the load event of form2, I want to figure out what button on form1 was pressed to open form2, then load the appropriate code from there.

For example... Form1 has 2 context menu buttons that open the same form (form2). When form2 opens, I want to know if it was button1 or button2 that was clicked to open form2.

I was looking into the sender and e parameters, but I could only figure out the form name of the form that opened it using sender.name
Was thinking something like
Code:
[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]If sender.name = "Name of button1 on form 1" then[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]Do this code[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]Else[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]Do this code[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]End If[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]
But like I said sender.name only returns the name of the previous form that opened this form.

The end result I am trying to get is slightly more complicated, but this method "technically speaking" should work. What I want to do is what would be the equivalent to the "Open in add mode" in MS Access. I figured how to load a dataset and populate fields and be able to navigate through records on open of the form, but for button2 I want to be able to open the form to a blank form, ready to add a new record, WITHOUT navigation buttons.
Even if someone has a better method top open the form in "add mode" please describe the first part in detail just for future reference.
Thank you
 

MarkK

bit cruncher
Local time
Today, 09:11
Joined
Mar 17, 2004
Messages
8,179
Since Form2 has a couple of modes why not directly pass it a parameter to tell it what mode you want it to open in.
On Form2 write your own Public Sub New() that takes a mode parameter ...
Code:
Public Sub New(OpenMode as String)
[COLOR="Green"]  'Form2 is inherited from Form and base class requires this call[/COLOR]
  Me.InitializeComponent
  
  Select case OpenMode.Mode
    Case "Add"
[COLOR="Green"]      'execute add mode initialization[/COLOR]
    Case "Edit"
[COLOR="Green"]      'execute Edit mode initialization[/COLOR]
  End Select
End Sub
... and from Form1 you might have code like ...
Code:
Private Sub AddButton_Click(s as object, e as eventargs) handles AddButton.Click
[COLOR="Green"]  'call Form2 constructor with mode[/COLOR]
  dim frm as new Form2("Add")
  frm.OpenDialog
End Sub

Private Sub EditButton_Click(s as object, e as eventargs) handles EditButton.Click
[COLOR="Green"]  'call Form2 constructor with mode
[/COLOR]  dim frm as new Form2("Edit")
  frm.OpenDialog
End Sub
... and now Form2 will require a parameter when it opens. This ...
Code:
dim frm as new Form2
... will fail because Form2 does not expose a parameterless constructor anymore.
Cheers,
Mark
 

buratti

Registered User.
Local time
Today, 12:11
Joined
Jul 8, 2009
Messages
234
Thanks for the quick response. I inserted the code and had some problems off the bat. Well to back up just a bit, I dont "fully" understand the code you suggested either. We'll get back to that in a minute though. The problems I am having is: (BTW I used "Form1" and "Form2" as easy explinations, the real name of form1 is "Main" and form2 is "Customer_Detail") (For whatever reason the spaces did not copy when i copied the code from my project so dont think that may be some problems)
Code:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] NewCustomerToolStripMenuItem_Click([/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] sender [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.Object, [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] e [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] System.EventArgs) [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#000000] NewCustomerToolStripMenuItem.[/COLOR][/SIZE][SIZE=2][COLOR=#008000]
[/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'call Form2 constructor with mode[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] frm [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Customer_Details([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Add"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]frm.OpenDialog()[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE]
frm.openDiag()--->gives the error "OpenDiag() is not a member of windowsApplication1.Customer_Details". Which is weird because WindowsApplication1 was the default name for my project but
I saved it as RGB_Tools. Same thing with the Edit button click.

Code:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] OpenMode [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Form2 is inherited from Form and base class requires this call[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].InitializeComponent()[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Select[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] OpenMode.Mode[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Add"[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'execute add mode initialization[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Edit"[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'execute Edit mode initialization[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Select[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[/COLOR][/SIZE]
SelectCase OpenMode.Mode---> gives the error "'Mode' is not a member of 'string'"
Code:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Customer_Details_Load([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Load[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] frm [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Customer_Details[/SIZE]
Under frm it says "Argument not specified for parameter 'OpenMode' of 'Public Sub New(OpenMode as String)'"

As for not understanding the code... I know enough about VBA but not everything. Everything I did learn was from online tutorials and forums like this so its quite possible that i missed a few key important procedures/methods when coding. And VB.net is not exactly the same as VBA so that hurts me too.

Me.InitializeComponent... i just dont know what this does
dim frm as new Customer_Detail("Add")... i get the "dim frm as new Customer_Detail" but not the "("Add")" part
 

MarkK

bit cruncher
Local time
Today, 09:11
Joined
Mar 17, 2004
Messages
8,179
This is a typo in what I posted ...
Code:
SelectCase OpenMode[COLOR="DarkRed"].Mode[/COLOR]
It should be ...
Code:
SelectCase OpenMode
Fix that and see what happens. We can't really discuss the other stuff yet, since I expect the typo would have caused a compile error then it's no suprise the form wouldn't open.
Let me know,
Mark
 

buratti

Registered User.
Local time
Today, 12:11
Joined
Jul 8, 2009
Messages
234
OK SelectCase OpenMode now has no errors, but I still have the other 2 errors :
"OpenDiag() is not a member of windowsApplication1.Customer_Details" and
Dim frm AsNew Customer_Details... Under frm it says "Argument not specified for parameter 'OpenMode' of 'Public Sub New(OpenMode as String)'"

Those 2 are not compile errors but rather, I guess you would call design time errors. Meaning that they have blue squiglly lines under them similar to a misspelled word in a Word document. I hover the mouse cursor over it and I get a popup message stating what I mentioned above. hey are also listed in the Error List window on the bottom of the screen.
 

Users who are viewing this thread

Top Bottom