Adding WithEvents to SerialPort object place on form in VB.NET - vb.net

I have added a SerialPort object onto a form I created in a VB.NET solution. How do I add "WithEvents" to it, since I dragged it onto the form instead of creating it in code. Is it even necessary to do so?

Dragging it onto the form automatically adds it to the designer generated code as WithEvents - it has to or you wouldn't be able to hook up events in the Events pane. If you hit the "View All Files" toggle in the Solution Explorer you should be able to see the designer files. Have a look in there and you will find where the IDE adds this code.

Related

How can a ContextMenuToolStripItem be displayed, without doing it programmatically?

I've taken over a project, which contains some UltraWinGrid controls on some of the forms.
On one of the original forms which has been there since before I took on the project, there is a ContextMenuStrip, which has just one ContextMenuToolstripItem on it, captioned 'Default for Column'.
This ContextMenuStrip is hidden by default, however, when the user right-clicks on the UltraGrid, the ContextMenuStripItem is displayed on the grid (rather than at the top of them form where the ContextMenuStrip is).
I'm trying to replicate this behaviour in a new form that I've added myself, however, after adding the ContextMenuToolstripItem in, I cannot get it to display when right-clicking my grid.
I've put a breakpoint on every subroutine in the in the older forms class, and none of them are triggered when right-clicking it.
Additionally I have also searched the class for DefaultForColumnToolstripMenuItem, and the only place it appears is in the Click event of the ContextMenuToolStripItem itself.
So, how else is it possible to show it when right-clicking the grid? I've copy and pasted the ContextMenuStrip from the old form onto the new form and still nothing occurred.
I'm assuming it's in a property somewhere that I'm missing?
Check for a ContextMenuStrip property on the UltraWinGrid. You can set it in the designer and your context menu will appear on right click without having to deal with the MouseClick handler, checking the mousebutton used, etc.

How to set the class of a vb.NET control after it's been created already

I have a Windows Form with 7 Picture Boxes on it that are called PropButton1 through to PropButton7. I know they aren't buttons but I'm using them as buttons anyway (normal buttons aren't suitable for this purpose).
I want to add a custom "File Path" property to the Picture Boxes. To do this I've created a separate class that inherits the PictureBox class:
Public Class PropButton
Inherits PictureBox
Private SoundFilePath As String
Public Property SoundFile() As String
Get
Return SoundFilePath
End Get
Set(value As String)
SoundFilePath = value
End Set
End Property
End Class
I want to convert the original Picture Boxes from PictureBox to PropButton so I can read and write to things like PropButton1.SoundFilePath and I preferably want to do this without having to delete all of my Picture Boxes and start again. Is there a way to do this?
In Visual Studio look to the right in the Solution Explorer. It has a toolbar button to Show all files. Click it and you will see that you can expand the tree nodes for the forms and they contain three files. One that contains your source code, and another one called a Designer. The Designer file is automatically generated by Visual Studio and in most cases it should not be touched.
When you open the designer file you see all the initializations of the controls on your form and their declarations. Here you can easily change the declarations of your pictureboxes so that they are created as PropButtons instead.
Just be careful what you change here, because it can mess up the Visual Studio designer. But it is good to know what happens behind the scenes.
Look here first:
Change this...
...to this.
Yes, that is possible with the text editor. The Visual Basic IDE hides too much information, first thing you want to do is click the "Show All Files" icon in the Solution Explorer window. That adds a node next to your form in the same window, open it and double-click the Designer.vb file. Note the InitializeComponent() method and the declarations at the bottom of the file, you see the PictureBoxes being declared and initialized.
You can now simply Edit+Replace "System.Windows.Forms.PictureBox" with "PropButton".
Ensure you have a good backup before you do this.

can i make a window form(common controls) using code in vb.net?

instead of dragging and dropping a common control in vb.net, is there a way to hard code it?
or is there a way for me to be able to view the codes where a dragged and dropped object has
been created? thank you so much!
The auto-generated code is intentionally hidden in the VB.NET IDE. But you can easily reveal it. Click the "Show All Files" toolbar button in the Solution Explorer window. You'll now see the Form nodes in your project displayed with a triangle. Click on it to reveal the Designer.vb file. And double-click that to see the code.
Observe the changes in the InitializeComponent() method as you use the designer to add/remove/edit controls. It isn't perfect code, the machine generated it, but it gives you a major leg-up on what kind of code you need to write to "hard-code".

main.vb, main.designer.vb and missing Form Designer

I have been working with "visual basic.net" on a "windows forms" application. While manipulating controls and adding event handlers I noticed the resultant code was being generated within a file named 'main.designer.vb'. However, if I look in the solution explorer for my project there is no 'main.designer.vb' file, just 'main.vb'.
This is not a colossal problem as it runs properly. However, having closed the 'form designer' window I now cannot reopen it! 'main.vb' has no option to 'view in form designer'.
Any advice on this?
Would it be possible to copy the contents of 'main.designer.vb' in to 'main.vb' and delete 'main.designer.vb' entirely? If I did this, the next time I manipulated the form would the code be added to 'main.vb' or would a new 'main.designer.vb' be created?
I seem to have sorted out the problem.
'main.vb' was completely empty. All the code I had generated and written directly was inside 'main.designer.vb'. However, once I made a class definition within 'main.vb':
Public Class main
End Class
and then cut/pasted all my custom event handler code and subroutines from 'main.designer.vb' to THAT class - all was well. 'main.vb' now shows the correct form icon and FINALLY offers the correct 'view designer' context menu option.
I am not sure why it happened in the first place though.
Look in the Solution Explorer in Visual Studio. In the toolbar in this window is a button called "Show all files". Click it.
Then every file in the projects folder is actually shown in the solution explorer. Expand the treenodes for the form and you will see the designer.vb.
There are also buttons for switching between code-view and designer view. Just remember to select the form in the solution explorer for the buttons to show the correct form in the designer.

Edit form in desginer that is generated at load time

Hi
Just inherited a VB forms application that must be modified. My problem is that the controls are placed at the form at the load event. There is no controls on the form when I open the form1.vb in Solution explorer.
How can I achieve changing the design for the form?
/Andy.l
You'll have to locate the code that creates and adds controls (I guess you found it in the Load event) and modify that code. Or else comment that code out and add all the same controls in the designer.
If the controls are not dynamic (i.e. if the Load event always adds the same controls in the same positions), then your best long-term solution is my second suggestion (add all the controls "properly" in the designer).