I need to know how to determine the total number of lines in a multi line textbox.
I have looked thru the properties and I cannot come up with a solution.
Printable View
I need to know how to determine the total number of lines in a multi line textbox.
I have looked thru the properties and I cannot come up with a solution.
tempInteger is how many lines there is.Code:Dim s As String = TextBox1.Text
Using sw As New StreamWriter(My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData & "\someTempFileIMade.txt")
sw.Write(s)
End Using
Dim tempInteger As Integer = 0
Using sr As New StreamReader("Same as above")
Do Until sr.EndOfStream
sr.ReadLine()
tempInteger += 1
Loop
End Using
I will try it out.
if there are any other ways to do the same function I wold like to know the other ways also.
thanks.
If your worried about size, add the code to a function...
MsgBox(getTextBoxLines(TextBox1.Text))Code:Public Function getTextBoxLines(ByVal text As String) As Integer
Using sw As New StreamWriter(My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData & "\someTempFileIMade.txt")
sw.Write(text)
End Using
Dim tempInteger As Integer = 0
Using sr As New StreamReader("Same as above")
Do Until sr.EndOfStream
sr.ReadLine()
tempInteger += 1
Loop
End Using
Return tempInteger
End Function
I am getting the message that "streamreader" is not defined.
Are you guys serious?!
textBox1.Lines.Length
:ehh:
Textboxes has a Lines property that returns a string array, containing each line in the textbox. Using this array, you can easily find out how many lines there are in the textbox, considering all arrays have a Length property.
I didn't have VS open...Quote:
I have looked thru the properties and I cannot come up with a solution.
I have the code working that Icyculyr has posted, but, it does not count a line that is blank and I need to count the blank lines also.
any ideas?
textBox1.Lines.Length
*cough*
I just finished trying textBox1.Lines.Length.
thanks a lot! it works just like I need.
Iam going to keep the other code because it works in a different way that I may need sometime.
thanks again!!!
Remember to mark your thread as resolved.
Thread Tools -> Mark Thread as Resolved.
I have a problem, it is not counted as a line unless the user hits the enter key.
if the user types and allows the text box to wrap automatically then the lines are not counted properly.
any ideas?
Do you have the WordWrap property set to true?
yes.
What to you mean by, "allows the text box to wrap automatically?"
Certainly if WordWrap is set to true, you cannot allow it to wrap automatically, yes?
if the user continues to type when he\she reaches the far right side of the textbox the text will jump to the second line, but, the line count does not change unless the user hits the enter key before the cursor jumps to the second line.
That's because it is not a new line, it appears to be a new line since the textbox wraps it, but it's not...
can this be fixed?
I highly doubt that... even if it could, I would say it would take a long time to make those kind of modifications...
but if I was going to, I would think of TRYING to get the y position of each character (as it will be the same on the first line) but as soon as it moves to the second, it will be different... but I am not sure if that is possible.
ok. thanks
Edit: Agh, there's 3 new replies already?! These boards move too fast =(
Ohhhhhhhh alright. Yea, that definitely is a problem.
This is probably due to the fact that since the Length property is an array of integers. When the textbox is created when the program runs, the arrays index is set to 0 and everything the user types goes in index 0 of the array. Then, when the user presses enter, the index is increased by one, so everything the user types now is in the index 1.
How to fix this I haven't the foggiest. I suppose you could handle the TextChanged event and simulate the key press of the Enter key when the condi-actually, that doesn't sound like much of a good idea.
Maybe you should just use Icyculyr's code, if it does not produce the same error, that is.
his code produces the same effect.
Yea, I thought it would. It all makes sense if you think about it. It is just wrapping the current line (Icyculyr already said this). It's not actually a new line. What do you need this for anyways?
I have a database field that I want to limit to 300 charactors & spaces but also limit it to 6 lines. this allows a way to print the data to paper easily since I will always know how many lines to allow for.
here is the code for VB 6 that worked perfect.
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
dim lCount as integer
lCount = SendMessage(StatusNotes.hwnd, EM_GETLINECOUNT, 0, ByVal 0)
'lCount is the number of lines.
You can do call Win32 API functions in VB .Net, can you not? I know you can do it in C#.
Imports System.Runtime.InteropServices
Looks like you can o_O
Also looks like all hope is not lost!
I would think the code I used for VB 6 could be converted to VB 2008 XE.
I could write a small program using the function and then run it thru the VB 2008 VB XE converter.
vb.net Code:
Imports System.Runtime.InteropServices Public Class Form1 Private Const EM_GETLINECOUNT As Integer = &HBA Declare Auto Function SendMessage Lib "user32" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim lineCount As Integer = SendMessage(TextBox1.Handle, EM_GETLINECOUNT, 0, 0) End Sub End Class
We win!
thanks!!! it works just like in VB 6.
thanks again!!!
I'm new to VB and need help on this project? Could anyone of you help?
Programming 1 - Final
Create a multi-form project that on one form the user enters the following information:
Form 1: Customer Entry
Customer Number: should be 5 digits and unique
Customer Name:
Customer Street Address:
City:
State:
Zip:
Customer Telephone:
Discount Percentage: .85 represents the customer is charged 85% of total sale.
Form 2: Order Entry
Customer Number:
Item Number:
Item Description:
Price of Item:
Quantity Ordered
Delivery Date:
Shipping Cost:
Total Cost w/out tax:
Total Cost with Tax:
Form 3: Invoice:
This form should display one invoice per customer displaying all the Orders entered in the order entry and give the user the ability to specifically lookup one customer number and display all orders from form 2.
The invoice should display all info from form 1 and all info from form 2 per order, and a total amount or the overall orders.
New things you will need for this project:
List boxes
Multiple Forms
Arrays
Code Module
User Defined Types
Grading:
Form 1 = 15%
Form 2 = 15%
Form 3 = 15%
Interactivity = 55%
Errors – 5%
Efficiency – 5%
Use of Arrays + 5%
Code Module + 5%
Comments – 10%
List Boxes + 5%
You need to post this on a new thread.