Visual Studio Shortcut to Auto-Add Event Delegate Methods - vb.net

In C# adding event handler methods is very easy. You just type "object.event +=" and then press tab twice.
Is there anything like this for VB projects?
Note: This is for dynamically created controls or controls that are not declared WithEvents.

You can select the object and the event in the top of the coding window and VB will automatically create the method for handling the event and hooking it up in the designer file. Besides from this there is no such feature available not with Visual Studio nor Resharper.
The C# development team added support for this, but for some reason the VB guys haven't gotten around to it yet, nor has I seen any third party addons doing anything like it.

Related

Call PopupMenu VB.NET Framework 2.0

I have a project that I have converted from VB6 to .NET, using Visual Studio 2008 with the framework 2.0. In VB6 you can call a PopupMenu like so:
Call Me.PopupMenu(mnuEstimating)
I tired to use the same code in .NET but I get the error:
'PopupMenu' is not a member of 'frmEstimatePriority'.
I have looked around on the web and have seen ContextMenuStrip being used in place of this. I would like not to have to add another/new object to the form designer if I don't have to. Is there another easier option besides ContextMenuStrip?
Can someone please tell me how to open a PopupMenu in VB.NET?
Assuming the form has a ContextMenuStrip control with menu items, try:
Me.ContextMenuStrip.Show(MousePosition)

Dispose unmanaged resources created in Windows Form

When an object is created in the .Designer.vb file from the Windows Form Designer, is a Dispose() call generated automatically for each object or must this be done manually?
Specifically, I have an object that uses unmanaged resources (by calling ShowDialog(), requiring a call to Dispose()) that is created in the Windows Form Designer. Do I still need to call Dispose() on that object?
Note: It is advised not to call Dispose() more than once on the same object.
While I dont know the answer, let me tell you how you can get the answer and learn a neat trick for designing components for winforms (might work for others but haven't tried).
Assuming you have a project that already references your component, create a new project and set the "Start External Program" to "C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe" (change this based on your version of visual studio, this is from 2005)
Run this project, which will then run another instance of VS. You have now opened VS in debug mode. Now open your project you are using to build/test your component (from the second instance of VS), set a break-point and test it. You should be able to see it hit your break-point in the dispose code.
This is great for testing VS Add-ins and designer components.
This was one of those questions where the answer is "because that is the way it works", but then you realize you don't actually know how it is implemented. So I spent a few moments to dig up the details.
Anything that is a Control (button, textbox, whatever) that is added to the Controls collection is disposed for you automatically. This implementation is inherited all the way back from the Control class itself.
Anything that is a non-graphical Component (like a Timer for example) that is added to the auto-generated components object in the MyForm.Designer.vb file is also disposed for you. In this case, components object, while implementing the System.ComponentModel.IContainer interface, will be an instance of System.ComponentModel.Container that handles the actual Dispose.

Issue with OpenFileDialog and threading

I just upgraded from VS 2005 to VS 2012. This is a new issue that I do not understand. I am using the default "Form1" class the VS automatically creates. I added a button to open a file open dialog and when I click the button I get this error:
Current thread must be set to single thread apartment (STA) mode before OLE calls can be >made. Ensure that your Main function has STAThreadAttribute marked on it. This exception >is only raised if a debugger is attached to the process.
I have added " to Public Class Form1:
<STAThread()> Public Class Form1
But I get this...
Attribute 'STAThreadAttribute' cannot be applied to 'Form1' because the attribute is not >valid on this declaration type.
I have searched but get some info telling me that I need to set the entry point (Form1 I believe) to be Single Thread Attribute but the above code does not work.
How?
The <STAThread()> attribute cannot be added to classes like your form. It only works when it is applied to the Main function, which is the entry point of your application.
But VB.NET hides this function from you because it is rare that one needs to mess with Main in a WinForms application. It is just needed to get the plumbing set up for your app, which the compiler can manage for you. This is controlled by the "Application Framework" checkbox in the project options. If this is checked, the compiler automatically generates the Main function and the required plumbing. You can disable this option, but it makes life quite a bit harder for the average WinForms developer because you'll have to write and maintain your own Main function.
The real question here is why this is a problem at all. The compiler-generated Main function for a WinForms application is always going to have the STAThread attribute applied to it. That is just how the WinForms framework is designed to run. If that is not happening, then there is something badly wrong with your project. I would recommend scrapping it and starting over letting Visual Studio create a new WinForms project from one of the built-in templates. Everything should Just Work™.
The other option, of course, is that you're trying to display the OpenFileDialog on a separate thread (other than your main UI thread). But from your description in the question (adding a button to the form to display the dialog), it doesn't sound like this is the case. Regardless, the solution is not to do that. For example, if you're using a BackgroundWorker to do work on a non-UI thread in order to keep the UI responsive, that's great, but you'll want to do all of the UI stuff like showing an OpenFileDialog on the main UI thread before invoking the BackgroundWorker. There is a way to set a particular thread's apartment state using the SetApartmentState function, but I really don't recommend showing an OpenFileDialog on a background thread.

What is the quickest way to add a COM visible event to VB.NET class?

What is the quickest way to add a COM visible event to VB.NET class?
Is there something in the Visual Studio toolbox or is there a macro that will do this?
I am using VS2005.
EDIT: In this article http://msdn.microsoft.com/en-us/library/dd8bf0x3(v=vs.80).aspx, a GuidAttribute is specified. Is this generated by the programmer at design time, or does it need it to to match a GUID elsewhere?

Loading a Vb.net control inside a VB 6 form

I have a user control in vb.net application.
and i have a form in vb 6.
so in my vb.net applciation to this user control i need to mention the vb 6 form as its parent.
Means when i run a vb6 application i should see this user control as a part of that form.
Ahy help appreciated.
Take a look at the COM interop Toolkit.
It allows you to easily create com-visible .NET usercontrols, which can be used in VB6 for instance.
You can try saving it as a dom object in .net
After this, you can open it in vb6 in the menu components.
I've done it with guid class of .net but i don´t know what you and to do, but i think you should try it.
It is actually fairly easy to add some plumbing to a VB.NET User control to expose it via COM as an ActiveX control that can be used by VB6 and other COM clients.
Here is an article describing how to do that:
"Writing an ActiveX Control in VB.NET"