How to find what caused an event? VB.Net - vb.net

I have a Handler for a ComboBox.SelectedIndexChanged event. The 'sender' tells me that it is the ComboBox that caused the event, and what its text and value currently is, but what I need to find out is what caused the ComboBox to change its selection in the first place?
How do I find out which line in my program is causing the ComboBox to change, please?

I added the following code to my Handler, and found some useful clues:
Dim st As New StackTrace(True)
For i As Integer = 0 To st.FrameCount - 1
Dim sf As StackFrame = st.GetFrame(i)
Debug.WriteLine(vbCrLf & "Line: " & _
sf.GetFileLineNumber().ToString & _
" Method: " & sf.GetMethod().ToString)
Next

You will need to use the SelectionChangeCommitted event and not the SelectedIndexChanged event.
The SelectionChangeCommitted event works similar to SelectedIndexChanged event, the only difference is that the SelectedIndexChanged event gets fired when the ComboBox selection is changed from code i.e. during DataBinding or when the form is loaded.
So, if you populate your ComboBox in any place in your code, that should be the place where the event is triggered.

Related

Dynamically add members to action - VB.NET

I'm trying to perform a loop action to edit same property with its specific value on multiple controls.
Something like:
Dim action as action
action.add(control1)
action.add(control2)
action.add(control3)
action.run
Then the action will perform something like this:
For each ctrl as control in action.controls
ctrl.text = ctrl.text & "another value"
Next
I ended up creating new tasks of the same action by calling it with different controls. Even better than I wanted to.

vb.net deleting a label and textbox at runtime

So i have a class that creates a new label and textbox when it is called. The names of each label and textbox are in a continues order counting upward using a variable called i:
"Label" & i creates the name Label1 and "Textbox" & i creates the name Textbox1
i += 1
And so on. Now i want to add a procedure that deletes the last label and textbox using i. I have tried to make this procedure using the lines of code below but this doesnt work as the string cannot be converted to system.windows.forms.control :
Form1.Controls.Remove("Label" & i)
Form1.Controls.Remove("Textbox" & i)
i -= 1
Controls is a list of controls using the line of code:
Public controls As List(Of Control)
Basically i need a way to delete a label and textbox using the variable i in the class. Any ideas? Thanks.
Try...
Form1.Controls.RemoveByKey("Label" & i)
Form1.Controls.RemoveByKey("Textbox" & i)
This does not work if you have these controls tucked in GroupBox, Panel, or some other container on your form.
I think you should try adding your controls into a panel, and then delete it as this link suggests (it's quite like what you tried, but inside a panel):
Removing Dynamically created controls in Panel

How to Set Focus in Access Continuous Form (VBA)

Right now it's being ignored. Here's the code at it's most basic, it causes an inescapable scenario currently, but I plan to condition these out. Just need to get the basic setFocus working.
Private Sub fieldbox_LostFocus()
Me.fieldbox.SetFocus
End Sub
In a regular form, it works as expected. When fieldbox loses focus, the focus is set back onto fieldbox.
In a continuous form, it does not work, and the set focus does not happen. This is possibly due to multiple instances of fieldbox existing for each record.
What I'd like is if fieldbox1, 2 and 3 exist on the continuous form, and you click out of fieldbox2, how do I set focus back to fieldbox2?
You need to use the form's BeforeUpdate() event instead of focus:
Private Sub Form_BeforeUpdate(Cancel as Integer)
If RecordOK = False Then
Dim response, strMsg as String
strMsg = "There is data missing from the record. " _
& vbCrLf & "Press Yes to continue, editing. " _
& vbCrLf & "Press No to discard all changes."
Cancel = True
response = MsgBox(strMsg, vbYesNo)
If response <> vbYes
Me.Undo
End If
End If
Code above is not tested, may need some tweaks, paste and see. My recent update provides cleaner handling of the user's response to the dialog box. (You do have Option Explicit set, don't you?!)
A good explanation of this method at the control level is found here. But you may want to use the form event, as above. Sometimes I just temporarily insert Msgbox cues in the events so I can see what is firing when.
Note the dialog box should provide clues on how to avoid wiping the work done so far by a user.

Word VBA events only fire once for documents based on a template

I have a macro enabled template with an event listener that listens for content control exit events. The idea is that when the "title" or "date" content controls on the title page of the document are edited, we automatically update the "title" and "date" content controls in the header so that the author doesn't have to enter the same content twice.
My problem is that, when I open a new document based on my template (right click template => new, or just double click it), these events only fire for the very first instance of a content control being exited. I click inside the CC, click outside the CC, get a MsgBox indicating that my event has fired. I then try that a second time: click inside the CC, click outside the CC and do not get a MsgBox.
Code from my event handler class:
Public WithEvents doc As Word.Document
Private Sub doc_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
MsgBox ContentControl.Range.Text
End Sub
I've checked and found that my event handler object is still defined (not "Nothing") in my NewMacros, it's just that it isn't getting ContentControlOnExit events, or is ignoring them.
If I change the above code such that I'm not actually doing anything with the content control inside the event body, the problem is fixed - my theory is that touching any sort of content control while inside the ContentControlOnExit event is triggering recursive ContentControlOnExit events and somehow causing a problem. Obviously a ContentControlOnExit event is pretty useless if I'm not allowed to do anything with content controls while inside it.
i.e. receiving a ContentControlOnExit event doesn't "break" future ContentControlOnExit events if I change my code to:
Public WithEvents doc As Word.Document
Private Sub doc_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
MsgBox "Content Control exit event"
End Sub
I've tried using an eventsEnabled boolean to try and guard against doc_ContentControlOnExit being called recursively in case that's the problem, but it didn't help. The code I used for that was like:
Sub Class_Initialize()
pEventsEnabled = True
End Sub
...
' in the doc_ContentControlOnExit sub:
If pEventsEnabled Then
' obvious race condition...
pEventsEnabled = False
' Fiddle around with some content controls
pEventsEnabled = True
End If
Excel has an Application.EnableEvents property, but this doesn't seem to be present in Word.
The interesting thing is that this all works fine when editing the template itself, just not for documents based on that template. When editing the template, I get a ContentControlOnExit event every time I exit a content control, and all of my code works fine.
If it helps, I'm using Word 2010 (Office Professional Plus) on Windows 7 Professional 64 bit. I've also confirmed that the same problem occurs under Word 2007.
edit:
I just tried setting the event handler object to call "ReincarnateEventHandler" in NewMacros, which in turn set the original event handler object to Nothing and then instantiated a new event handler. This resulted in an infinite loop of event handlers handling the first event and then setting up a new event handler which then handled the same (original) event. Using Application.OnTime to delay the reincarnation by 1 sec fixed the infinite loop, but didn't fix the original problem — i.e. dropping my first event handler and then instantiating a new event handler doesn't let me catch subsequent events after the first event.
It's a quick fix. Change ThisDocument to ActiveDocument in your SetUp routine. As it is, ThisDocument refers to the template itself. ActiveDocument is for the one created from AutoNew. So it should read Set eventHandler.doc = ActiveDocument.
It won't interfere with other documents not created with "so_template.dotm" as the ones created from that template will have an .AttachedTemplate of "so_template.dotm" and the AutoNew, if present, will control them.

how to get the object associated with a context menu in an office add-in

I have a simple COM add-in for office that I am developing (for access specifically).
I have added a custom commandbarbutton item to the context menu that pops up when you right click on an object in the navigation pane.
This works fine. The debug code I added runs (currently just a msgbox command). The one thing I cannot figure out how to do though is get an object for the object bound to the context menu.
I would like this to happen; I right click on a module in the navigation pane, select my new menu option, and then a message box appears with the name of the module that is currently highlighted. How would I go about this?
This is how I am currently handling the event:
Public Sub myEventHandler(ByVal ctrl As CommandBarButton, ByRef CancelDefault As Boolean) Handles contextMenu_navPaneObject.Click, contextMenu_navPaneList.Click
MsgBox(Microsoft.VisualBasic.Information.TypeName(ctrl) & vbCrLf & _
Microsoft.VisualBasic.Information.TypeName(ctrl.Parent) & vbCrLf & _
Microsoft.VisualBasic.Information.TypeName(ctrl.Parent.Parent))
End Sub
contextMenu_navPaneObject and contextMenu_navPaneList are private objects declared using "withevents" and having an object type of commandbarbutton.
Is this the correct way to do what I want, or is there an alternative method I should be using?
Turns out to do what I wanted, I needed to run the following method:
Access.Application.CurrentObjectName()
This returned the name of the item I currently had highlighted.
I hope this helps others!