I have a little problem calling the button1_click event of form1 from my form2.
When I call form1.button1_click() it gives me an error saying:
Argument not specified for parameter 'e'.
How can I fix this?
Assuming WinForms, try using this:
form1.button1_click(Nothing, Nothing)
or
form1.button1_click(form1.button1, EventArgs.Empty)
The error means the procedure you are trying to run has parameters, but you left them out in your call. The click event is looking for two parameters, sender As Object and e As EventArgs.
You may also wish to try this:
form1.button1.PerformClick()
Related
I'm working on a POS program where I have a POS keyboard COM control within the application. When I double click the icon, an Event is created:
Private Sub PosKeyboard_DataEvent(sender As Object, e As AxOposPOSKeyboard_CCO._IOPOSPOSKeyboardEvents_DataEventEvent) Handles PosKeyboard.DataEvent
If PosKeyboard.POSKeyData = 1 Then Exit_Button.PerformClick()
End Sub**
How do I create an event on a different form within the app for the same device?
Based on the comments so far, I will provide an answer that I think applies.
When it comes to handling events, you have a variable to which you assign an object and then you handle an event of that variable. When the assigned object raises its event, the handling method is executed. So, in your case, you need to start by declaring a variable in the form in which you want to handle the event, e.g.
Private WithEvents posKeyboard As SomeType
The WithEvents keyword is required for that variable to be able to be used in a Handles clause. I use SomeType because I don't know the actual type of your object, although I suspect that it's something like _IOPOSPOSKeyboard.
You can then write a method to handle the desired event, copying the signature from your other form:
Private Sub posKeyboard _DataEvent(sender As Object, e As AxOposPOSKeyboard_CCO._IOPOSPOSKeyboardEvents_DataEventEvent) Handles posKeyboard .DataEvent
'...
End Sub
You then need to pass in the object that will be raising the event from the other form, which you can do via a parameter in a constructor or some other method or a property setter.
I want to call a sub from within another sub. The problem is there is a variable in the event handler that appears nowhere else in the code. I think it is an array of data fed by an API. the variable in the event handler parenthesis is called 'positions'. the syntax inside the event handler parenthesis is:
ByVal positions as X.API.UpdateList
I haven't code vb.net but if you are using ByVal you are actually referencing the value
of the variable positions to another variable in you main sub that holds the same data.
So try calling the Private Sub like this:
Call update(positions)
Take note that positions must be declared with the same data type in your main sub like this.
Dim positions as X.API.UpdateList
And of course it holds your required value for use in your private sub.
Hope this helps.
I'm looking to call a pre-existing event handler subroutine from the form_Load event handler.
But the below doesn't work because control doesn't come back and I want to do more.
UPDATE:
I'm new to this so I don't know what the proper protocol is but...
The reason for the non-return was that a statement like the below ended the subroutines execution.
If aLabel.Tag = 1...
the fix was adding New to the declaration to create an instance of it, ie..
changing....
Dim aLabel As Label
... to ...
Dim aLabel As New Label
I'm surprised I didn't get a warning but instead they just abruptly stopped execution of the sub. That wasn't very helpful :)
Thanks again for your time guys...
(Maybe this question should be deleted now that it has served its purpose)
#konrad #karl
END OF UPDATE
What doesn't work is....
Private Sub Form1_Load...
button1_Click(sender, e) 'But Control doesn't come back.
end sub
Do I change the sender to something?
Thanks in advance
Dave
Invoking event handlers like this is a bad idea, because you are trying to simulate the event context by making sender and/or EventArgs be something else.
Instead, put the logic that you want to invoke into a Subroutine or Function and have your Form1_Load method call that; likewise if you really do have a real click event handler, then that handler code can call the method too, like this:
Private Sub Form1_Load()
DoSomeWork()
End Sub
Protected Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
DoSomeWork()
End Sub
Private Sub DoSomeWork()
' Put logic here that you want to do from form load and a button click
End Sub
This has the benefit of making the code cleaner, clearer and easier to maintain as you only need to change the logic in one place should you need to change the logic.
Note: Obviously, you can pass parameters to the DoSomeWork method, if need be, and change it to a Function if you need it to return something.
I have a form which has a search feature - a single text field and command button; when the text field is filled-in a database query is executed and the result (if one result returned) is shown on the form via dynamic control fields.
When the search feature is used for the first time, the fields are created and the data is returned from the database, however when the search feature is re-ran I am getting the error "Object Reference not set to an instance of an object", the error occurs at:
initSearch(txtSearchInput.Text)
I am guessing that I am not handling the textfield properly for this type of use, can anyone please advise how else I should be doing this?
The txtSearchInput is not a dynamic field, it has been created through the design mode, the same for the command button. The above code is located in the command button On Click event:
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
Try
initSearch(txtSearchInput.Text)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error Encountered")
End Try
End Sub
Any help would be greatly appreciated.
Thanks,
Matt
Any help would be greatly appreciated.
The error is not in the code you posted. The Text property of a TextBox, and the reference to a Form Textbox don't become null all of a sudden.
You probably have to debug into initSearch
Did you test in the debugger if txtSearchInput is null?
Exception could be bubbling up from initSearch function, best way is to debug your code.
I want some event handling functions to only be able to be accessed via raised events and not called from the program.
Such as:
Public Event Event1(Byval TheText as string)
private sub Event1Handler(Byval TheText as string) handles me.Event1
msgbox("Hi")
end sub
I want this code to execute the function:RaiseEvent Event1("Hi")
But I do not want this code to execute the function:Event1Handler("Hi")
Is there a declaration or some other way to accomplish this?
There's no other way than embed this logic into a lambda expression.
Right click the function and click "find all references". Do this periodically to make sure nothing references it directly