Visual Basic .NET (Easy way to test Form) - visual-studio

I have been using Java for most of my programming projects, but I got Visual Studio yesterday and want to start learning how to use Visual Basic.
I normally in Java, and can just copy and paste code into the class and rename the class name and it will work.
However, in Visual Basic this doesn't seem to be as simple as just a copy and paste and rename. All of Visual Basic .NET code is in a solution and from what I can tell there is only a main method in one form. In Java you can have a main class in each class and run the class simple like that.
I thought that you could do it the same way in Visual Basic .NET but so far have not been able to find the way to do it. I have tried making a form and then pasting in the example code into the load form and that seemed to generate no errors but didn't work neither.
Public Class GridsAndData
Private Sub GridsAndData_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
Dim formGraphics As System.Drawing.Graphics
formGraphics = Me.CreateGraphics()
formGraphics.DrawLine(myPen, 200, 400, 200, 200)
myPen.Dispose()
formGraphics.Dispose()
End Sub
End Class
This was some sample code that I tried to draw a single line on a form.

You're not supposed to draw anywhere except in the Paint handler in Windows. Here's an example that works with the code you posted (placed in the correct method):
Start a new WinForms solution (File->New Project from the main menu).
Once the project is created, click on the Properties window (by default in the lower-right corner of Visual Studio). Click on the Events button (the little lightning bolt) to switch to that tab. Find the Paint item (in the Appearance section), and double-click it.
Paste your code into the generated method (which, by default, is Form1_Paint):
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
' Paste your code here - the part below "Private Sub" and above "End Sub"
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
Dim formGraphics As System.Drawing.Graphics
formGraphics = Me.CreateGraphics()
formGraphics.DrawLine(myPen, 200, 400, 200, 200)
myPen.Dispose()
formGraphics.Dispose()
End Sub

Related

How do I sort build output of Visual Studio by Build Order by default?

I know that I could sort the build output of my multicore builds in Visual Studio using the Build Order item in the Output window (as described here).
But once I've done that and hit F7 again, the option switches back to Build and I have to switch back to Build Order again.
Is there a way to set Build Order as default setting in the Output window?
Searching a bit shows me that this question was asked several times but never answered:
http://ntcoder.com/bab/2009/06/02/ordering-output-of-out-of-order-builds-in-visual-studio/#comment-484
http://blogs.msdn.com/b/zainnab/archive/2010/07/03/show-the-output-window-during-build-vstiptool0045.aspx#comments
http://weblogs.asp.net/scottgu/archive/2005/10/21/428094.aspx#1451451
Edit:
The answer given by Simon works for me (or at least it points me to the right direction), but I could not simply copy his code and insert it in my MyMacros project. Instead, I have to create the handler for the build events exactly as described here:
On the Class View explorer pane, in the Macros IDE, double-click the EnvironmentEvents node to display it as an EnvironmentEvents tab
and a drop-down menu on the macro editor pane.
From the EnvironmentEvents drop-down menu, select an events type, such as TaskListEvents. The Declarations combo box is now populated
with the available Task List events.
On the Declarations drop-down menu, select an event, such as TaskAdded, to add its event procedure to the module.
The event is inserted into your macro and you can now add code to the event procedure.
Otherwise, the event handler isn't called at all.
You could write a Visual Studio macro, something like this:
Dim WithEvents MyBuildEvents as BuildEvents
Private Sub BuildEvents_OnBuildBegin(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles MyBuildEvents.OnBuildBegin
OpenBuildOrderOutputPane()
End Sub
Private Sub OpenBuildOrderOutputPane()
Dim window As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput) ' Get Output Window
Dim output As OutputWindow = CType(window.Object, OutputWindow)
For Each pane As OutputWindowPane In output.OutputWindowPanes ' Browse panes
If (pane.Guid = "{2032B126-7C8D-48AD-8026-0E0348004FC0}") Then ' Build Order guid
pane.Activate()
End If
Next
window.Activate()
End Sub
You need to paste this code in MyMacros, EnvironmentEvents module.

How to save an Excel file via VB 2010 without any dialogs (such as "save as")

I am trying to Save an Excel file Via VB 2010, and I have these questions
How can I disable the "save as" dialog? I tried things such as only "save" instead of "save as", but it doesen't work...
After I saved the file (using the save as) I can't Delete it... (I tried closing the excel file, Visual basic etc...) all i get is an error saying it is allready open in excel...
Is there a way to make VB show me the tips for writing the excel stuff (Ie - when I write messagebox. - it pops up "Show" for help. how can I enable this for excel code [worksheets.cells. ect.])
the connection:
Sub Connect()
' Connect to the excel file
oExcel = CreateObject("Excel.Application")
'Devine the workbook
oBook = oExcel.workbooks.open("e:\Words\Heb.xls")
End Sub
the saveas:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
oExcel.SaveAs(oExcel.Path & ".xls")
End Sub
Thanks a lot
I think Inafiziger has solved your main issue, it should be a vanilla Save.
As it was unclear to me exactly what your are doind (ie Visual Studio/VB/BA) then
On (1)
I thought it worth clarifying that you can use code inside the ThisWorkbook module to detect and handle a SaveAs if you are providing users with a choice. This Event detects the SaveAs and cancels it
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI Then
MsgBox "You cannot use SaveAs to save this file", , "Save Cancelled!"
Cancel = True
End If
End Sub
This code can be programmatically added to your target workbook but I doubt you would need to resort to this given you should be able to run the simple Save.
On (3)
You need to use Early Binding to get the benefit of intellisense. You are currently use late binding with oExcel = CreateObject("Excel.Application"). A commonly used approach is to write the code and get it working with early binding, then converting it to late binding for final code publication.
Conditional Compilation (see comment at bottom) can be used to switch between the two binding methods within the same code.
You should be saving the workbook. e.g. oBook.Save.
If you create a new file, you will need to use SaveAs with a valid filename in order to save it the first time.

Combobox from Visual Studio Macros

When you need to debug a Website that hosts on IIS Express, you usually don't restart it all over again, every time when you need to rebuilt your code. You just attach VS to the process. And the macros script helps a lot:
Public Module AttachToProcess
Public Sub AttachToWebServer()
Dim attached As Boolean = False
Dim proc As EnvDTE.Process
For Each proc In DTE.Debugger.LocalProcesses
If (Right(proc.Name, 14) = "iisexpress.exe") Then
proc.Attach()
attached = True
Exit For
End If
Next
If attached = False Then
MsgBox("iisexpress.exe is not running")
End If
End Sub
End Module
You can assign a keystroke and voila. The only problem is if your solution contains more than one webapp, there would be more than one iisexpress.exe processes with different PIDs, and VS would sometimes choose the wrong one.
The question: Is it possible to popup a dialog if there is more than one iisexpress.exe running to choose the right one?
Of course you can always use default 'Attach To Proccess' dialog, but it's not gonna be as quick as using that script and keyboard shortcut.
You can open a dialog, but it's not the most straightforward thing. You will need to put all your UI code into the macro, EG layout, size of controls, etc...
It's about 200ish lines of code, and rather than put it all here I will defer you to my blog at http://www.brianschmitt.com/2010/09/save-and-change-tool-layout-in-visual.html
You should be able to reuse the View Switcher dialog box and list out all instances of IISExpress. It shouldn't take much to do what you need it to.

visualstudio 2010 VB.NET customizing the code automatically displayed when I write New

When in a code editor I write
Sub New (enter)
The editor automatically inserts:
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
How can I customize the code it writes? Can't find it in the code snippets manager.
This string is baked into Common7\IDE\1033\msvb7ui.dll. In other words, it is hard-coded in the binary code that implements the VB.NET IDE.
This is also the case for the auto-generated code when you implement IDisposable. Much worse actually since that code is inappropriate 99.9% of the time.
If it really annoys you, you could use File + Open + File and navigate to the DLL. Open the string table and double-click "String Table". It is string #1059. Note that you cannot change the line spacing or the comment. Do make sure you make a backup of the file, I didn't try it myself.

How do I locate a Word application window?

I have a VB.net test application that clicks a link that opens the Microsoft Word application window and displays the document. How do I locate the Word application window so that I can grab some text from it?
You can use the Word COM object to open the work document and then you manipulate it. Make sure to add a reference for Microsoft Word first.
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Word
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strFileName As String
Dim wordapp As New Microsoft.Office.Interop.Word.Application
Dim doc As Microsoft.Office.Interop.Word.Document
Try
doc = wordapp.Documents.Open("c:\testdoc.doc")
doc.Activate()
Catch ex As COMException
MessageBox.Show("Error accessing Word document.")
End Try
End Sub
End Class
The doc object is a handle for the instance of Word you have created and you can use all the normal options (save, print etc). You can do likewise with the wordapp. A trick is to use the macro editor in Word to record what you want to do. You can then view this in the Macro Editor. This give you a great starting point for your VB code.
Also, be sure to dispose of the Word COM objects at the end.
I've done something similar with a SourceSafe dialog, which I posted on my blog. Basically, I used either Spy++ or Winspector to find out the window class name, and make Win32 calls to do stuff with the window. I've put the source on my blog: http://harriyott.com/2006/07/sourcesafe-cant-leave-well-alone.aspx
Are you trying to activate the word app? If you want full control, you need to automate word from your vb.net app. Check here for some samples: 1, 2

Resources