|
A cheap way to display files and hex data
|
Total Hit (4489) |
The .NET framework includes a ByteViewer control, that you can use on your own forms to display data stored in a Byte array or in a file. The ByteViewer control can't be added to the control Toolbox, though, so you must instantiate it through code only.
«Code LangId=2»
' IMPORTANT: this code re
....Read More |
Rating
|
|
|
Adding an image as an embedded resource, and loading it from code
|
Total Hit (3749) |
To add an image as an embedded resource in a Windows Forms application, add the image file to the project with Project | Add Existing Item. Then select the file in the Solution Explorer, go to the Properties Window, and set its Build Action property to Embedded Resource. At this point the file will
....Read More |
Rating
|
|
|
Adding events dynamically in a Windows Form
|
Total Hit (2950) |
The new AddHandler statement makes it possible to attach event dynamically, that is without having to bind them via static code based on the Handles keyword. This keyword is also useful to have all the controls on a form share the same event procedure.
For example, say that you want to change th
....Read More |
Rating
|
|
|
Bind a DataGrid to a DataSet at design-time
|
Total Hit (3362) |
Binding a DataGrid - or any other data-aware control - to a DataSet or a DataTable at runtime isn't difficult: you just have to assign the DataTable to the DataGrid's DataSource property, as in:
«Code LangId=2»
' DS is the DataSet variable
DataGrid1.DataSource = ds.Tables("Titles")
«/Code»
....Read More |
Rating
|
|
|
Adding an image as an embedded resource, and loading it from code
|
Total Hit (2673) |
To add an image as an embedded resource in a Windows Forms application, add the image file to the project with Project | Add Existing Item. Then select the file in the Solution Explorer, go to the Properties Window, and set its Build Action property to Embedded Resource. At this point the file will
....Read More |
Rating
|
|
|
Copying data into the Clipboard
|
Total Hit (4673) |
Copying a piece of information into the clipboard is as easy as calling the Clipboard.SetDataObject: you can pass this method a string, an image, and so on. If you pass it a string that contains text in Rich Text Format (RTF), the Clipboard object detects this format automatically. For example, the
....Read More |
Rating
|
|
|
Creating multiline tooltips
|
Total Hit (3516) |
In VB6 there was no easy way to create multiline tooltips for form controls, you necessarily had to resort to API tricks. And it was probably just not worth the effort. The good news is that doing this is straightforward in VB.NET! First add a Tooltip Provider control on the form to add the Tooltip
....Read More |
Rating
|
|
|
Multi-column menus
|
Total Hit (3658) |
The new Break and BarBreak properties of the MenuItem object let you create menus with multiple columns. All you have to do is set either property to True for the MenuItem object that must become the first item of a new menu column. The only difference between these two properties is that BarBreak d
....Read More |
Rating
|
|
|
Pasting data from the Clipboard
|
Total Hit (6430) |
Pasting data from the clipboard requires more code because you must ascertain whether the clipboard contains data in one of the formats you're willing to process. First, use the Clipboard.GetDataObject to retrieve an IDataObject object. Next, use the GetDataPresent method of this IDataObject object
....Read More |
Rating
|
|
|
Showing and enumerating MDI child forms
|
Total Hit (3272) |
An MDI child form is a regular form whose MdiParent property points to its MDI container form, so the code that creates and displays a child window is very simple:
«Code LangId=2»
Sub ShowMdiChildWindow()
' Display the MenuForm form as a child of this form.
Dim frm As New MenuForm()
....Read More |
Rating
|
|
|
Unload setup projects before running the Inheritance Picker
|
Total Hit (2907) |
If your solution contains a setup project and this setup project has been built at least once, you get an Assembly Load error when you run the Inheritance Picker in order to inherit from an existing form in your main project.
This problem occurs because the setup project generates several execut
....Read More |
Rating
|
|
|
Use VB6 ActiveX controls with VB.NET
|
Total Hit (4106) |
It is possible to use VB6 ActiveX controls - such as ADO Data Control, Chart Control, Comm Control, and the Windowless controls - with the designer of Visual Studio .NET, but you may encounter problems when doing so because VS.NET doesn't come with the design-time license for these controls, therefo
....Read More |
Rating
|
|
|
Working with owned forms
|
Total Hit (3278) |
If a form owns another form, the owned form will be always displayed in front of its owner form, no matter which is the active form. This makes owned forms ideal for implementing tool and palette windows. Another feature of owned forms: when the user closes or minimizes the owner form, all the owned
....Read More |
Rating
|
|
|
Add multiple child controls to a Coolbar
|
Total Hit (2274) |
The Coolbar control consists of a collection of Band objects, and each Band object exposes a Child property. To move a control inside the Coolbar you just have to assign it to the Child property of a Band object.
A minor problem is that you can't directly assign multiple controls to one Band. The
....Read More |
Rating
|
|
|
Determine the number of visible items in a ListView control
|
Total Hit (4503) |
Microsoft "forgot" to provide the ListView control with the GetVisibleCount property, as it did, for example, with the TreeView control. However, getting this information is as easy as sending a message to the control:
«Code LangId=1»
Private Const LVM_FIRST = &H1000
Private Const LVM_GETCOUNT
....Read More |
Rating
|
|
|
Determine the optimal width for ListView columns
|
Total Hit (3055) |
Here's a simple but effective trick to ensure that the column of a ListView control is wide enough to display the entire string you're assigning to the column title or to an element. Just drop a Label on the form, set its Autosize property to True and Visible property to False. Also, ensure that the
....Read More |
Rating
|
|
|
Fill a TreeView control with random data
|
Total Hit (2219) |
Every now and then you need to fill a TreeView control with some random data, for example when you want to test a routine and you don't want to write a lot of code just for this secondary task. Here is a recursive routine that does the work for you:
«Code LangId=1»
' MaxChildren is the max num
....Read More |
Rating
|
|
|
Suppress Max and Min buttons in MDI forms
|
Total Hit (2156) |
Unlike regular forms, forms don't expose the MinButton and MaxButton properties and these buttons are always displayed. You can suppress these buttons using the following code:
|
Rating
|
|
|
The behavior of the LostFocus event depends on VB version
|
Total Hit (1743) |
The behavior of the LostFocus event has changed in the transition from VB4 to VB5. Under VB4, if a control on a form has the input focus and the user clicks on a toolbar button, the control fires a LostFocus event. Under VB5 and VB6, the same action doesn't fire any LostFocus event, so you must awar
....Read More |
Rating
|
|
|
The fastest way to append contents to a TextBox control
|
Total Hit (1959) |
The most intuitive way to append a string to the current contents of a TextBox is through the & string operator, as in:
«Code LangId=1»
Text1.Text = Text1.Text & "more text"
«/Code»
However, you can reach the same result about 4 times faster with the following method: «Code LangId=1»
Tex
....Read More |
Rating
|
|
|
The first visible line in a multiline TextBox control
|
Total Hit (4037) |
To determine the index of the first visible line in a multiline TextBox control you only need to send it the EM_GETFIRSTVISIBLELINE message:
«Code LangId=1»
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal _
hwnd As Long, ByVal wMsg As Long, ByVal wParam As Lon
....Read More |
Rating
|
|
|
The number of lines in a multiline TextBox control
|
Total Hit (2829) |
You can quickly determine how many lines of text are contained in a multiline TextBox control whose Scrollbars property is set to 3-Both - that is, when the TextBox behaves like a programmer's editor - by counting the number of CR-LF characters in the Text property. You can do that in just one line
....Read More |
Rating
|
|
|
Topmost windows
|
Total Hit (2127) |
This is an evergreen, but it's still popular. It is very easy to create a window that always stays on top of the others, thanks to the SetWindowPos API function. Here is a general procedure that can be called to make a form the topmost window and to revert to the normal status. This capability is ex
....Read More |
Rating
|
|
|
Undo changes in a TextBox control
|
Total Hit (3875) |
The TextBox control supports the capability to undo changes, but there is no property or method that exposes this feature. You can achieve the same goal with a a set of messages.
«Code LangId=1»
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal _
hwnd As Long,
....Read More |
Rating
|
|
|
Unload a form with shrinking effect
|
Total Hit (1641) |
A simple way to add some pizazz to your forms is by minimizing them and then closing them, instead of making them disapper istantaneously. Just try this code:
«Code LangId=1»
Private Sub Form_Unload(Cancel As Integer)
Me.WindowState = vbMinimized
End Sub
«/Code»
....Read More |
Rating
|
|
|
Use a ListBox as a poor man's grid
|
Total Hit (2242) |
You can easily create columns of data in a ListBox control by setting its tab stop at appropriate positions. This way, you can use a ListBox control as a sort of grid control with (very) limited functionality, but without using third-party controls.
You can set ListBox's tab stop by sending the
....Read More |
Rating
|
|
|
Use DataChanged with unbound controls
|
Total Hit (1985) |
When you need to learn if a field has been changed by the user, you have at least three options:
you can use a global boolean variable and set it in the control's Change event, or
you can save the original value in global variable to compare it against the current value
you can use the EM_GE
....Read More |
Rating
|
|
|
Use Refresh, not DoEvents
|
Total Hit (2053) |
The DoEvent statement should be used only to give other portions of your program to be reactive to the end user's actions. Instead, many VB developers use it to force a repaint of a form or some of its controls. This means adding a lot of overhead to your code, which you can avoid by using the Form'
....Read More |
Rating
|
|
|
UseMnemonics property for bound Label controls
|
Total Hit (2231) |
If your forms contain Label controls used to display data from a database, set their UseMnemonic property to False. In fact, if you leave this property to its default value (True), ampersand "&" characters won't be showed correctly, and the Label will mistakenly react to hotkeys.
....Read More |
Rating
|
|
|
Using Enter to move to next field
|
Total Hit (1653) |
Most MsDos programs used the Enter key to move from one field to the next one. You can easily simulate this behavior by setting the form's KeyPreview property to True and writing this code in the Form_KeyPress event:
«Code LangId=1»
Sub Form_KeyPress (KeyAscii As Integer)
If KeyAscii = 13 Th
....Read More |
Rating
|
|