I want to call a click in event from a button in VB6, I can't seem to figure it out. I have tried this code here but it doesn't work.
Call cmdLoads_Click(Sender, e)
I have also set the sub to public as well, still no luck.
The button sub has to have an index:
Private Sub cmdLoads_Click(index As Integer)
The name of the command button is cmdLoads, it is really cmdLoads(0). So all I had to do is change the code to this to make it work.
Call cmdLoads_Click(0)
A command button (or any other object raising events) can have multiple sinks (event subscribers) so calling "xxx_Click" sub will not notify any of the other listeners.
In short: use cmdLoads(0).Value = True as it's more portable.
Related
Word has several events to hook into, to control application behavior etc.
Some of these events are: Document.BeforeSave and Application.BeforeSave
As far as I know, they do the same thing - they allow you to perform actions before the new document is saved, or even cancel the save event entirely.
But in later versions of Word, there seems to be an event happening BEFORE the BeforeSave events.
It shows a "Save this file" dialog:
Once the user has chosen where to save, and click [Save], THEN is the BeforeSave event executed.
I need a way to intercept this dialog, to prevent the user from saving in certain situations.
While I can do that with the current BeforeSave event, it results in a very bad user experience, since the user first chooses where to save, only to then be told that the time spent doing that was for nothing since the document is not allowed to be saved at this time.
In earlier versions of Word this was not an issue, Word just showed the regular (simple) Save As dialog whenever the user wanted to save the new document.
Coding in VB, I've tried these two ways to handle the BeforeSave Events at Document-level and Application-level. Both fire too late.
(C# code is also welcome, it's easily translated to VB in this context.)
' --- Handle BeforeSave at Document-level
Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
AddHandler vstoDoc.BeforeSave, AddressOf clsWord.ThisDocument_BeforeSave
Public Shared Sub ThisDocument_BeforeSave(sender As Object, e As SaveEventArgs)
' Do stuff...
End Sub
' --- Handle BeforeSave at Application-level
Private Sub HandleDocumentBeforeSaveEvent(document As Word.Document, ByRef SaveAsUI As Boolean, ByRef Cancel As Boolean) Handles _wordApplication.DocumentBeforeSave
' Do stuff...
End Sub
How do I intercept the new dialog?
Alternatively, is there another way to prevent the user from saving the document (like setting a document property or something)?
I found a way.
First part is to expand the ribbon markup to include a <commands> node above the <ribbon> element (if any).
<commands>
<command idMso="FileSave" getEnabled="FileSave_GetEnabled"/>
<command idMso="FileSaveAs" getEnabled="FileSaveAs_GetEnabled"/>
</commands>
Then add callback methods to the ribbon code file:
Public Function FileSave_GetEnabled(ribControl As Office.IRibbonControl) As Boolean
' Simple test to toggle enabled/disabled easily
' - requires ribbon invalidation for changes to take effect though
Return My.Computer.Keyboard.CapsLock
End Function
Public Function FileSaveAs_GetEnabled(ribControl As Office.IRibbonControl) As Boolean
Return FileSave_GetEnabled(ribControl)
End Function
Finally you need to handle the BeforeSave event:
Private Sub HandleDocumentBeforeSaveEvent(Doc As Word.Document, ByRef SaveAsUI As Boolean, ByRef Cancel As Boolean) Handles _wdApp.DocumentBeforeSave
If MessageBox.Show("Do you REALLY want to save the document?", "BeforeSave", MessageBoxButtons.YesNo) = DialogResult.No Then
Cancel = True
Exit Sub
End If
End Sub
This allows you to disable the "Save"-icon in the top of the ribbon, Ctrl+S and the "Save" option in the File menu.
It does not disable the "Save as" option in the file menu, but whenever a user tries to save from there, the BeforeSave event is executed, allowing you to take action there.
All in all, a better solution than previously achieved!
I want to end program in visual basic using keypress procedure. I use this code but every time i press a key it doesn't end the program.
Private Sub Form_KeyPress(KeyAscii As Integer)
End
End Sub
I write this and it works :
Private Sub Form_DblClick()
End
End Sub
but pressing doesnt work
You've put your code in the Form KeyPress event. If the form has any controls on it, the active control gets the event. The form only sees the KeyPress event when you set KeyPreview = True. In the Form properties try setting the KeyPreview to True, then test it again.
To intercept keypresses from anywhere on the form, set the Form property KeyPreview to True.
You can set this at runtime with Me.KeyPreview = True in the Form's Initialize or Main method.
Once you are capturing all keystrokes, use System.Windows.Forms.Application.Exit() within the method to end the program. Check thisenter link description here out for more info on exiting the application.
I have the following problem. I wrote simple macro which shows MsgBox before print dialog. Here is source:
Public WithEvents App As Application
Private Sub App_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
MsgBox "aaaaa"
End Sub
Private Sub Document_New()
Set App = Application
End Sub
When I open one document from template with this macro, everything is OK. But I have a problem when I open two documents from this template at same time. When I click to print button, MsgBox shown up twice. Any ideas?
Edit:
When I create document from this template and create another new document, which isnt't based on this template (both of this documents are opened at the same time) and I print from that new empty document, MsgBox shown up. This is also wrong right?
You have created application-level events that fire every time any document is being printed. They are triggered once for every document that has this code in it, so every time you print a document you will get the msgbox once for every open document that has the code in it, whether or not the document that's printing has the code in it.
So, the behaviors aren't wrong, although clearly they are not what you want.
You should put the Before_Print event in the ThisDocument module of your template. That way the event will only happen once, and only when the document being printed has the code in it.
You could put a check in your App_DocumentBeforePrint sub to check whether the instance of the application object which is firing the event is the instance that contains the active document:
If Me <> ActiveDocument Then Exit Sub
I am trying to call a click event from within another a method from the same Window Form file. It just won't work form me.
For instance:
theClass = partial class(System.Windows.Forms.Form)
method AlarmListBox_Click(sender: System.Object; e: System.EventArgs);
private
protected
public
method DoSomething;
end;
method theClass.DoSomething;
begin
self.AlarmListBox_Click; <<<this is where i want to call the click event
end;
No matter what I do, it keeps raising compiler errors. I tried AlarmListBox.Click, AlarmListBox.performClick, etc.
Some of the errors I got:
There is no overloaded method "AlarmListBox_Click" with 0
parameters.
Cannot access underlying event field
So, how do you fire an event within the same window Form?
It's best to call the Event handler with the default parameters:
AlarmListBox_Click(self, EventArgs.Empty);
By passing self into the method you define that the source of the call was not the AlarmListBox but your form. You could also can pass in custom EventArgs that state that the Event was not raised because of a click on the AlarmListBox but from your code.
You are not passing the parameters of the method AlarmListBox_Click
Try this
AlarmListBox_Click(nil, nil);
I have the following code:
Private Sub Tabstrip1_Click()
Form2.Show vbModal, Me
end sub
Form2 has just a Close button. The first mouseclick on Form2 has no visible effect unless form2 lies over the calling form, then an error is raised that a modal form cannot be shown twice! So what happens is that the first mouseclick is registered on the parent form. I tried all kind of workarounds (mostly by going through Tabstrip1_GotFocus), but I could not get it to work without doing some very convoluted things.
Is there a simple solution?
Use this code to fix mouse capture issues TabStrip control exhibits in your particular case
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Sub TabStrip1_Click()
Call ReleaseCapture
Form2.Show vbModal, Me
End Sub
The problem is described here:
https://support.microsoft.com/en-us/kb/262441 .
As said there, TabStrip (supposedly only the version included in Mscomctl.ocx SP3) does not release the mouse capture, so you have to call ReleaseCapture before displaying a modal form.
aside:
I'd have just edited the previous correct answer but the edit was refused.
"Introduces an external dependency" ??? Authoritative references are not a bad thing in my experience, I think the link is going to be useful to someone.