Any way to auto collapse to definitions whenever you close a document? - visual-studio

I know this might not sound very useful to most people, but i really like having all my code collapsed in VS and it's getting kinda annoying having to ctrl+m ctrl+o everytime i'm closing a document.
Is there some add-in that does this, or can someone give me general tips to create the add-in? thanks

You can achieve the functionality you desire by creating a macro in visual studio that executes the CollapsetoDefinitions command when ever the DocumentClosing event is raised.
Simply go: Tools -> Macros -> Macros IDE.
Then add the following code to the EnvironmentEvents module.
Private Sub DocumentEvents_DocumentClosing(ByVal Document As EnvDTE.Document) Handles DocumentEvents.DocumentClosing
Dim thread As New System.Threading.Thread(AddressOf CollapsToDefinition)
thread.Start()
End Sub
Public Sub CollapsToDefinition()
Try
If DTE.ActiveDocument Is Nothing Then Exit Sub
DTE.ExecuteCommand("Edit.CollapsetoDefinitions")
Catch
'Ignore any error
End Try
End Sub

Related

Visual Studio 2015 :is there any "post-debug" event?

I have an application that interacts with another one, suspending it while not used. If I end the debugging abruptly the suspended process will stay alive in the background and needs to be terminated.
I did some research and looks like none of the people found a solution that is applicable to Visual Studio 2015, in particular the macro solution will not work since looks like a dropped feature since VS2010...
My current solution is to run the proper taskkill command as a post-build action and within the startup code of the program.
I'm looking for a more elegant way of killing it, so a "post-debug" event like the one portrayed by the macro below would be perfect:
Private Sub DebuggerEvents_OnEnterBreakMode(
ByVal Reason As EnvDTE.dbgEventReason,
ByRef ExecutionAction As EnvDTE.dbgExecutionAction) Handles DebuggerEvents.OnEnterBreakMode
If (Reason = dbgEventReason.dbgEventReasonStopDebugging) Then
KillLeftoverProgram()
End If
End Sub
So is there any way to execute a cleanup shell command after using the stop button?

Expected end of line error in middle of codeline

I have this simple vbscript, but windows gives me an error when I'm trying to run it. I've never worked with vbscript before and i tried to google, but could figure out why. I'm getting expected end of line error at line 1, character 16. Does anyone see anything?
Also, I'm coding this in notepad and running it by double clicking the file. Is there any other way i should do it?
Dim WithEvents Button As CommandButton
Private Sub Application_StartUp(Cancel As Boolean)
Set Button = CommandButtons.Add("Click me")
End SubPrivate Sub Button_Click()
Msgbox "Clicked", vbInformation
End Sub
Since it was mentioned that notepad is being used for this and looking at the code this certainly appears to be code to add a button to a Form Based application using VBScript in Visual Studio.
VBScript via notepad is not really good at being used for creating GUI dialog boxes and such. A sample code of what would work using Notepad is as such:
toRepeat = InputBox("What should I repeat?", "Echo", "Hello World")
MsgBox toRepeat
Whatever is typed in to the input box will be repeated in this example.
If Visual Studio is not available and there is an interest in building a form based application then it may be possible to create User Forms in VBA via Word or Excel. The following site gives a good tutorial for creating a User Form application in Excel: Excel VBA Userform - Easy Excel Macros
In case there is interest, I recently wrote in to my blog an explanation of the different flavors of VB that are available: VBA vs VBSCRIPT vs Visual Basic
Thanks,
Sean W.

Visual studio 2010 ouput window maximize on build

Is there way to automatically maximize the output window on hitting build and then to automatically restore to previous state when the build completes?
You could create a macro that builds the solution then activates the output window. For example:
DTE.ExecuteCommand("Build.BuildSolution")
DTE.Windows.Item(Constants.vsWindowKindOutput).Activate()
You could then replace the Build button or the build keyboard shortcut to execute that macro.
I could implement a solution using a combination of macros.
Part of the solution is in this SO question:
OnBuildBegin does not fire in Visual Studio Macro until I run it from Macro Explorer
And the other part is to use 2 exported window settings and to toggle them on build events.
Something like:
Public Sub BuildEvents_OnBuildBegin() Handles BuildEvents.OnBuildBegin
DTE.ExecuteCommand("Tools.ImportandExportSettings", "-import:C:\Development\VsSettings\build_inprocess.vssettings")
End Sub
Private Sub BuildEvents_OnBuildDone(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) _
Handles BuildEvents.OnBuildDone
DTE.ExecuteCommand("Tools.ImportandExportSettings", "-import:C:\Development\VsSettings\Two_Screen.vssettings")
End Sub

Referencing functions within VB6 User Controls

I'm having an issue referencing public procedures of User Controls that I've created within a VB6 project.
A simple example (exe), I have a form with a button and a user control:
Option Explicit
Private Sub Command1_Click()
UserControl1.updateMessage ("TIME NOW: " & DateTime.Time)
End Sub
The User Control code is as follows:
Option Explicit
Public Sub updateMessage(ByVal newMessage As String)
Label1.Caption = newMessage
End Sub
This exe compiles and works fine, and when I'm typing updateMessage in the Form, it appears in the intellisense list with the appropriate requirements. The issue I have is when I'm wanting to "go to the definition" of updateMessage, instead of going to the appropriate section of the code within the User Control, the message always returns with:
"Cannot jump to 'updateMessage' because it is in the library 'Unknown1' which is not currently referenced."
where the numbered suffix of "Unkown1" changes from time to time.
It seems that if there were no reference to this procedure, then it would not appear in the intellisense and the project shouldn't compile. When running this with MZTools (though the error appears regardless of this plug-in being installed), I can go into the updateMessage procedure, and use it to find all procedures calling this function, so the link between the two should exist (although I'm not sure if MZTools just finds using a text-matching pattern).
If anyone out there could shed some light on this matter, it would be very much appreciated, and save this poor VB6 developer a lot of hassle!
I have SP6 installed (build 9782) of VB6 and am running XP SP3 on an HP dx2400.
Yes, this is extremely annoying and I'm convinced it's a bug in VB6. I say this because, if you locate the updateMessage method in the object browser and double-click on it, you are taken to the definition. So, the compiler actually does know where the definition is, it just refuses to take you there with Shift+F2.

Is there something like Emacs' toggle-read-only in Visual Studio?

The subject says it all. Is there an easy way to toggle the editability of a buffer in Visual Studio? This would be similar to the toggle-read-only command in Emacs.
I am not looking to change the file attribute ... just whether I can edit the file while it is open in Visual Studio. I am using 2008 and 2005.
Why would I want to do this? I tend to have several files open at the same time .... for days at a time sometimes (perhaps a bad habit) and I have +/- a char or few here and there without meaning to or noticing ... also worried about "the cat walking across the keyboard"
Besides ... an "ancient" code editor like emacs has it :) and I grew to expect the feature.
TIA!
There is an extension for Visual Studio called CodeMaid that will give you a Read-Only Toggle per file.
http://www.codemaid.net/documentation/#andmore
You can download it at http://visualstudiogallery.msdn.microsoft.com/76293c4d-8c16-4f4a-aee6-21f83a571496
You can use this piece of vba
Public Sub ToggleReadOnly()
Dim doc As Document
doc = DTE.ActiveDocument
If (doc.ReadOnly) Then
doc.ReadOnly = False
Else
doc.ReadOnly = True
End If
End Sub
Note: the msdn documentation specifically mentions that the property ReadOnly shouldn't' be used explicit but I verified that this works for me on vs.net 2005.
I also verified that the actual file attribute isn't changed.
I'm not aware of anything that will quickly achieve what you're looking for. Furthermore, I'm not really sure why you would need such a thing. Typically I use subversion to tell me which files have been changed and where they have been modified that way I can revert anything that doesn't belong.
Can you expand on your question a little to let us know what your usecase is?
If you really need to toggle readonly....perhaps you can:
Right click on the file
Select Open Containing Folder
Right click on the file and choose properties
Check the readonly checkbox
Start Tools->Macros->Macro IDE. Add new module (described here in details) and define there procedure as follows:
Imports EnvDTE
Public Sub SwitchReadOnly()
DTE.ActiveDocument.ReadOnly = Not DTE.ActiveDocument.ReadOnly
End Sub
Assign macro to keyboard key(described here in details). That's all.

Resources