Tag Archives: line

Microsoft has released a new (Well, released last Month) Power Pack for Visual Basic.NET 2005/2008. This 3.0 version actually includes a new control named: DataRepeater that allows you to use standard controls to display rows of data in scrollable based container. The existing controls appears to have had some minor updates as well. Below is the overview from Microsoft…

Microsoft Overview…

The new DataRepeater control included in this version of the Visual Basic Power Packs allows you use standard Windows Forms controls to display rows of your data in a scrollable container giving you more flexibility and customization than standard grid controls.

The Line and Shape controls are a set of three graphical controls that enable you to draw lines, ovals, and rectangles on forms and containers at design time making it much easier to enhance the look of your user interface. These shape controls also provide events such as click and double-click allowing developers to respond and interact with end users.

The Printer Compatibility Library allows projects that used the Printer and Printers Collection in Visual Basic 6.0 to be upgraded without having to re-write your printing logic. By simply adding a reference to the library, declaring a Printer and making a few minor syntax changes, your project will be able to print using the Printers collection and Printer object as it did in Visual Basic 6.0. This version adds a new Write method to the Printer object which allows you to print text without a forced carriage return similar to the semicolon syntax used by Print method in Visual Basic 6.0.

The PrintForm component is designed to bring back the ability to easily print a Windows Form. With this the new PrintForm component you can once again layout the Windows Form exactly as you want it and allow your users to print the form as a quick report.

There is unfortunately no so-called ‘built-in’ way to get the current line number in a Visual Basic.NET Textbox control. Microsoft added that support in Visual Basic 2005/2008/2010. But many still use the original .NET versions which do not have that feature in the textbox control.

What to do? Well, there is a somewhat easy way to add this feature by using the Windows API. The API to use is the SendMessage API call. Below is the declaration, constant value, and line# variable…

Getting the Current Line Number code

'
'Unfortunately, .NET before 2005 doesn’t have a built-in feature to get the current
'line. So I am using the SendMessage API to do it. You CAN do various extractions or
'whatever to do it, but this is easier and more elegant in my opinion.
'
'Used to Send Messages to the control and will be used with a request for the
'current line number in the textbox control.
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal winHandle As Int32, _
ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
'
'Constant found in the VB 6.0 API Viewer App IT will be passed to the SendMessage
'API to tell the control to give the current Line # from the textbox character position.
Public Const EM_LINEFROMCHAR = &HC9
'
'Will be used to contain the current line #.
Dim lineNum As Int32

You can put the codes below in the textbox_Click, textbox_Keydown, textbox_TextChange event you want to take place or a button control to get the line# results or whatever. In other words, put the code for where/when you want the current line results.

I put these codes in the Textbox_TextChanges event and used a label control to display the current line number. Its also worth nothing that Windows reports line # starting at “0”. So if the API returns a “0”, you can Add (+) one to the value which will then show the line# as 1. Another example is if the Windows API returns a 1, but in reality the current line is the second, then as before simple add (+) a One(1) to the value which will then be line #2.

'
'Call the API to get the Line # from the current char. Txt is your textbox control.
lineNum = SendMessage(txt.Handle.ToInt32, EM_LINEFROMCHAR, -1, 0)
'
'Windows sees the first line in a edit box as line 0. So just add a value of 1 to the
'result if you want the first line to be line #1.
lblCurrent.Text = "Current Line: #" & (lineNum + 1).ToString
'
'Check for line count first to keep a possible error from happening if the textbox has
'none.
If txt.Lines.Length > 0 Then
'
'Get the text at the current line position.
txtLineText.Text = txt.Lines.GetValue(lineNum)
End If

Thats all there is to it! When you run your project and type your characters or make a new line or whatever in the textbox, the label control should show the number that the caret or cursor is currently at. If you want the Line # code to be updated when the mouse clicks on a different line or position, simply put the code above in the textbox_Click event. Have Fun!