|
-
Feb 8th, 2010, 01:16 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] How many"k" are in my textboxes
How many"k" are in my textboxes?
I want help for the code that will check a collection of 10 textboxes how many letters"k"are in my textboxes and to see the number in a label1
thank you
-
Feb 8th, 2010, 01:36 PM
#2
Re: How many"k" are in my textboxes
vb.net Code:
Dim CharCounter As Integer = 0 For Each txtbox As TextBox In Me.Controls.OfType(Of TextBox)() Dim c = Aggregate chars In txtbox.Text Where chars = "k"c Into Count() CharCounter += c Next MsgBox("Total number of character k in all textboxes is:" + CharCounter.ToString)
-
Feb 8th, 2010, 02:06 PM
#3
Re: How many"k" are in my textboxes
That code will only work on .NET 3.5 wont it though cicatrix?
An alternative that will work in .NET 2.0 would be something like:
vb.net Code:
Dim CharCounter As Integer = 0
For Each txtbox As TextBox In Me.Controls.OfType(Of TextBox)()
For Each letter As Char In txtbox.Text
If letter = "k"c Then
CharCounter += c
End If
Next
Next
MessageBox.Show("Total number of character k in all textboxes is:" + CharCounter.ToString)
-
Feb 8th, 2010, 02:21 PM
#4
Re: How many"k" are in my textboxes
Yes, my code is for .NET 3.5. I really don't see any reason to continue developing for .NET 2.0 after a newer version is available (and it's free).
LinQ helps enormously and I can't imagine how I managed without it. Still, it's a little more than 'syntactic sugar' but it allows to focus on the main things, not the peculiarities of implementation.
-
Feb 8th, 2010, 02:26 PM
#5
Re: How many"k" are in my textboxes
 Originally Posted by cicatrix
Yes, my code is for .NET 3.5. I really don't see any reason to continue developing for .NET 2.0 after a newer version is available (and it's free).
That is a little naive... a lot of people either have an existing program that is using .NET 2.0 that they have to support or they simply dont want to force their users to have to download and install the rather large 3.5 framework (plus SP1) package. A lot more people have .NET 2.0 installed (every Vista user for example as it comes pre-installed) than people have .NET 3.5 installed. I'm sure there are other reasons but those are the first things I think of - after all if there was no reason then every .NET developer would be using 3.5... and that clearly isn't the case.
-
Feb 8th, 2010, 02:28 PM
#6
Re: How many"k" are in my textboxes
With memas's here I'm sure he's got vs2008 or 2010 Your points are valid and well taken though.
-
Feb 8th, 2010, 02:30 PM
#7
Re: How many"k" are in my textboxes
Yeah I'm sure in this particular case using LINQ wont be a problem at all, I just thought it worth mentioning that it requires 3.5 in case the OP was not aware
-
Feb 8th, 2010, 03:32 PM
#8
Thread Starter
Fanatic Member
Re: How many"k" are in my textboxes
cicatrix
how can i will put the collection of mytextboxes? i dont want all the textboxes
-
Feb 8th, 2010, 03:36 PM
#9
Re: How many"k" are in my textboxes
Well, either you should have some criteria or use your custom collection:
vb.net Code:
Dim myCol As New List(Of TextBox) myCol.Add(Textbox1) myCol.Add(Textbox5)
...
etc
the rest is the same:
vb.net Code:
Dim CharCounter As Integer = 0 For Each txtbox As TextBox In myCol For Each letter As Char In txtbox.Text If letter = "k"c Then CharCounter += c End If Next Next MessageBox.Show("Total number of character k in all textboxes is:" + CharCounter.ToString)
P.S. If you use VS2003 or VS2005 you should use Chris's example.
-
Feb 8th, 2010, 03:41 PM
#10
Thread Starter
Fanatic Member
Re: How many"k" are in my textboxes
cicatrix
1..if i have labels to take the k how will be the code for a group of labels ?
-
Feb 8th, 2010, 03:45 PM
#11
Re: How many"k" are in my textboxes
 Originally Posted by memas
cicatrix
1..if i have labels to take the k how will be the code for a group of labels ?
Guess what?
vb.net Code:
Dim myCol As New List(Of Label)
In all examples of this thread all it really takes is change TextBox with Label
-
Feb 8th, 2010, 03:46 PM
#12
Re: How many"k" are in my textboxes
There is a really hideous pun that can be made about this thread, but it may be American only, so I shall spare you all.
My usual boring signature: Nothing
 
-
Feb 8th, 2010, 03:46 PM
#13
Re: How many"k" are in my textboxes
 Originally Posted by shaggy hiker
there is a really hideous pun that can be made about this thread, but it may be american only, so i shall spare you all.
do it
-
Feb 8th, 2010, 03:49 PM
#14
Re: How many"k" are in my textboxes
Excuse me, kind sirs, but what is 'a pun'? (I know I'm good with my English, but not that good, apparently).
-
Feb 8th, 2010, 04:00 PM
#15
Thread Starter
Fanatic Member
Re: How many"k" are in my textboxes
cicatrix
is correct this code because the textbox says 2 and the k are 5
Private Sub Button72_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'HOW MANY K
Dim myCol As New List(Of Label)
myCol.Add(Label201)
myCol.Add(Label200)
myCol.Add(Label199)
myCol.Add(Label198)
myCol.Add(Label197)
myCol.Add(Label196)
myCol.Add(Label195)
myCol.Add(Label193)
myCol.Add(Label190)
myCol.Add(Label189)
myCol.Add(Label188)
myCol.Add(Label185)
myCol.Add(Label181)
myCol.Add(Label178)
Dim CharCounter As Integer = 0
For Each label As Label In Me.Controls.OfType(Of Label)()
Dim c = Aggregate chars In label.Text Where chars = "k"c Into Count()
CharCounter += c
Next
num34TextBox.Text = CharCounter.ToString
End Sub
-
Feb 8th, 2010, 04:03 PM
#16
Re: How many"k" are in my textboxes
 Originally Posted by memas
cicatrix
is correct this code because the textbox says 2 and the k are 5
This is because you're not paying attention to what people tell you.
If you use a custom collection then obviously you should iterate through it.
Here you iterate through ALL texboxes, you need to iterate only throught myCol.
Code:
Private Sub Button72_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'HOW MANY K
Dim myCol As New List(Of Label)
myCol.Add(Label201)
myCol.Add(Label200)
myCol.Add(Label199)
myCol.Add(Label198)
myCol.Add(Label197)
myCol.Add(Label196)
myCol.Add(Label195)
myCol.Add(Label193)
myCol.Add(Label190)
myCol.Add(Label189)
myCol.Add(Label188)
myCol.Add(Label185)
myCol.Add(Label181)
myCol.Add(Label178)
Dim CharCounter As Integer = 0
For Each label As Label In MyCol
Dim c = Aggregate chars In label.Text Where chars = "k"c Into Count()
CharCounter += c
Next
num34TextBox.Text = CharCounter.ToString
End Sub
-
Feb 8th, 2010, 04:11 PM
#17
Thread Starter
Fanatic Member
Re: How many"k" are in my textboxes
cicatrix the textbox always says 0 zero why?
-
Feb 8th, 2010, 04:15 PM
#18
Re: How many"k" are in my textboxes
Hmm, this should work.
Try renaming the label variable:
Code:
...
For Each lbl As Label In MyCol
Dim c = Aggregate chars In lbl.Text Where chars = "k"c Into Count()
CharCounter += c
Next
...
And it won't probably find 'K' (CAPITAL) - only 'k' (small)
-
Feb 8th, 2010, 04:18 PM
#19
Thread Starter
Fanatic Member
Re: How many"k" are in my textboxes
-
Feb 8th, 2010, 04:23 PM
#20
Re: How many"k" are in my textboxes
 Originally Posted by cicatrix
Excuse me, kind sirs, but what is 'a pun'? (I know I'm good with my English, but not that good, apparently).
Some people would call a pun the lowest form of humor, but it is actually just a play on words. For example, I should note that your code will find ALL the ks in a string, including the very rare case where two Ks come one after the other. This is very rare, and could be considered a special K because it refers to serial ks.
Now, in case it is not sold everywhere, Special K is a type of breakfast cereal.
My usual boring signature: Nothing
 
-
Feb 8th, 2010, 04:25 PM
#21
Thread Starter
Fanatic Member
Re: How many"k" are in my textboxes
shaggy hiker
why you dont try to help?
-
Feb 8th, 2010, 04:26 PM
#22
Re: How many"k" are in my textboxes
its sold here in the UK... and congrats on the terrible pun haha
-
Feb 8th, 2010, 04:26 PM
#23
Re: How many"k" are in my textboxes
 Originally Posted by memas
the same thing zero
Do these labels actually hold text containing small k English characters?
What is the text in your labels? Can you post some example?
P.S.
@Shaggy hiker: I may be not in a position to judge, but I too think it's a terrible pun
Last edited by cicatrix; Feb 8th, 2010 at 04:30 PM.
-
Feb 8th, 2010, 04:27 PM
#24
Re: How many"k" are in my textboxes
 Originally Posted by memas
shaggy hiker
why you dont try to help?
yeahhhh shaggy hiker, you're so unhelpful. Sometimes it makes me want to cry.
-
Feb 8th, 2010, 04:34 PM
#25
Thread Starter
Fanatic Member
Re: How many"k" are in my textboxes
cicatrix is working the mistake is mine thank you
-
Feb 8th, 2010, 04:40 PM
#26
Re: How many"k" are in my textboxes
 Originally Posted by memas
shaggy hiker
why you dont try to help?
Sorry, I don't deal in LINQ, so as long as Cicatrix is going down that road and is approaching a solution to the problem, I have nothing to add.
However, I would suggest that doing something like this:
Code:
For Each label As Label In MyCol
CharCounter += System.Text.RegularExpressions.Regex.Matches(label.Text,"[Kk]").Count
Next
would probably be faster. Haven't tested it, though, but it should match all K or k in the string and return the count of all such matches in the string.
My usual boring signature: Nothing
 
-
Feb 8th, 2010, 04:42 PM
#27
Re: How many"k" are in my textboxes
Simpler (and without regex):
Code:
c = Aggregate chars In txtbox.Text Where (chars = "k"c Or chars = "K"c) Into Count()
-
Feb 8th, 2010, 04:44 PM
#28
Re: How many"k" are in my textboxes
Perhaps, but LINQ isn't fast, so I would question whether or not Regex isn't the faster solution.
My usual boring signature: Nothing
 
-
Feb 8th, 2010, 04:47 PM
#29
Re: How many"k" are in my textboxes
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myCol As New List(Of Label)
For Each l As Label In myCol
num34TextBox.Text = countK(l.Text, False).ToString
Next
End Sub
Dim theCount As Integer = 0
Private Function countK(ByVal inWhat As String, _
Optional ByVal CaseMatters As Boolean = True) As Integer
theCount += inWhat.Count(Function(c) c = "k"c)
If Not CaseMatters Then theCount += inWhat.Count(Function(c) c = "K"c)
Return theCount
End Function
-
Feb 8th, 2010, 04:48 PM
#30
Thread Starter
Fanatic Member
Re: How many"k" are in my textboxes
CICATRIX
my mistake was that the k was with capital letter thats why i have problem now that i change the k in code with capital K is working perfect
thank you again
-
Feb 8th, 2010, 04:55 PM
#31
Thread Starter
Fanatic Member
Re: How many"k" are in my textboxes
dbasnett
with this code i have errors
-
Feb 8th, 2010, 05:03 PM
#32
Re: How many"k" are in my textboxes
 Originally Posted by memas
dbasnett
with this code i have errors
My internet telepathy is down today, so if you could be more explicit in the details of your difficulty, perhaps I could help. Post the code.
Last edited by dbasnett; Feb 8th, 2010 at 06:20 PM.
-
Feb 8th, 2010, 05:06 PM
#33
Thread Starter
Fanatic Member
Re: How many"k" are in my textboxes
relax for today tomorow i hope you are ok and i will ask you a difficult question
memas
-
Feb 8th, 2010, 06:24 PM
#34
Re: How many"k" are in my textboxes
 Originally Posted by Shaggy Hiker
Perhaps, but LINQ isn't fast, so I would question whether or not Regex isn't the faster solution.
Test it for yourself then. Regex was slower
Here's my results:

vb.net Code:
Imports System.Text.RegularExpressions Public Class Form1 Const MAX_ELEMENTS As Integer = 1000000 Dim rng As New Random Dim btnLINQ, btnREGEX As Button Dim RefCount As Integer = 0 ' For reference purposes Dim TestArray() As String Dim TestList As List(Of String) ' To test Arrays vs Collections issue Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load btnLINQ = New Button With {.Text = "Test LINQ", .Width = 200, .Height = 30, .Left = 10, .Top = 10} btnREGEX = New Button With {.Text = "Test REGEX", .Width = 200, .Height = 30, .Left = 10, .Top = btnLINQ.Bottom + 20} Me.ClientSize = New Size(btnLINQ.Right + 10, btnREGEX.Bottom + 10) Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog Me.Controls.AddRange(New Control() {btnLINQ, btnREGEX}) Me.Text = "LINQ vs REGEX" PrepareTestObjects() AddHandler btnLINQ.Click, AddressOf TestLINQ AddHandler btnREGEX.Click, AddressOf TestREGEX End Sub Private Sub PrepareTestObjects() btnLINQ.Enabled = False btnREGEX.Enabled = False RefCount = 0 TestList = New List(Of String) ReDim TestArray(MAX_ELEMENTS - 1) Dim x, c As Integer Randomize(Now.Ticks) For x = 0 To MAX_ELEMENTS - 1 Dim l As Integer = rng.Next(10, 21) ' make a random length Dim element As String = "" For c = 0 To l Dim randomChar As Char = Chr(rng.Next(33, 123)) ' random character from "!" to "z" element += randomChar If randomChar = "k"c Or randomChar = "K"c Then RefCount += 1 ' We'll know for sure how many k's here Next TestArray(x) = element TestList.Add(element) Next btnLINQ.Enabled = True btnREGEX.Enabled = True End Sub Private Sub TestLINQ(ByVal sender As Object, ByVal e As EventArgs) Dim KCount As Integer = 0 Dim t As DateTime = Now For Each st In TestArray Dim Cnt = Aggregate chars In st Where (chars = "k"c Or chars = "K"c) Into Count() KCount += Cnt Next Dim ArrayTestDuration As TimeSpan = Now - t KCount = 0 t = Now For Each st In TestList Dim Cnt = Aggregate chars In st Where (chars = "k"c Or chars = "K"c) Into Count() KCount += Cnt Next Dim ListTestDuration As TimeSpan = Now - t Dim ResultsStr As String = String.Format( _ "Test on {4} elements of random strings using LINQ:{1}Test on array:{3}{0:mm:ss.FFF} ms {1}Test on list:{3}{2:mm:ss.FFF} ms{1}Found elements:{3}{5}{1}Reference elements:{3}{6}", _ New Object() {ArrayTestDuration, Environment.NewLine, ListTestDuration, ControlChars.Tab, MAX_ELEMENTS, KCount, RefCount}) MsgBox(ResultsStr, MsgBoxStyle.Information, "Test results for LINQ") End Sub Private Sub TestREGEX(ByVal sender As Object, ByVal e As EventArgs) Dim KCount As Integer = 0 Dim t As DateTime = Now For Each st In TestArray KCount += Regex.Matches(st, "[Kk]").Count Next Dim ArrayTestDuration As TimeSpan = Now - t KCount = 0 t = Now For Each st In TestList KCount += Regex.Matches(st, "[Kk]").Count Next Dim ListTestDuration As TimeSpan = Now - t Dim ResultsStr As String = String.Format( _ "Test on {4} elements of random strings using REGEX:{1}Test on array:{3}{0:mm:ss.FFF} ms {1}Test on list:{3}{2:mm:ss.FFF} ms{1}Found elements:{3}{5}{1}Reference elements:{3}{6}", _ New Object() {ArrayTestDuration, Environment.NewLine, ListTestDuration, ControlChars.Tab, MAX_ELEMENTS, KCount, RefCount}) MsgBox(ResultsStr, MsgBoxStyle.Information, "Test results for REGEX") End Sub End Class
Last edited by cicatrix; Feb 8th, 2010 at 06:29 PM.
-
Feb 8th, 2010, 06:55 PM
#35
Re: How many"k" are in my textboxes
So there I was, going to test the results, and my machine locked up. Numerous times I have tried to test regex and had this happen. Basically my code timer calls the init code once, and then each of the tests hundreds of thousands of times. I must be missing something.
Code:
Dim testSTR As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim myCount As Integer = 0
Private Function TestCase1() As Boolean 'One
'Test One Code Follows
myCount = Aggregate chars In testSTR Where (chars = "k"c Or chars = "K"c) Into Count()
'End Test One Code
Return True
End Function
'
Private Function TestCase2() As Boolean 'Two
'Test Two Code Follows
myCount = System.Text.RegularExpressions.Regex.Matches(testSTR, "[Kk]").Count
'End Test Two Code
Return True
End Function
'
Private Sub _init()
'Code needed to get the Test to run. NOT part of the timing.
testSTR &= testSTR.ToLower
testSTR &= testSTR
'Only called once!!!
End Sub
-
Feb 8th, 2010, 07:17 PM
#36
Re: How many"k" are in my textboxes
Yup, LINQ turns out to beat both Regex and the old fashioned looping through the characters. I have seen cases where LINQ was slower, but it is not in this case.
However, I haven't used LINQ much, nor have I used the type inference that is present in the LINQ example. When I copied that into my testapp, I found that type inference simply wasn't working in that app, despite using VS2008 targeting the 3.5 framework. Is there something else that I need to set?
My usual boring signature: Nothing
 
-
Feb 8th, 2010, 07:39 PM
#37
Re: How many"k" are in my textboxes
Are you absolutely sure that the most optimized form of looping possible couldn't beat it? (Normally I would test it, but I have VS 2005.)
vb Code:
Dim i As Integer = 0, count As Integer = 0, kL As Char = "k", kU As Char = "K" While i < str.Length If str.Chars(i) = kL OrElse str.Chars(i) = kU Then count += 1 i += 1 End While
sort of thing? Or call ToCharArray() first? There are just too many different ways to use the simple loop.
-
Feb 8th, 2010, 07:44 PM
#38
Re: How many"k" are in my textboxes
Yeah, I tested that one, too. LINQ beat it handily. I didn't compare it directly to Regex, but they were in the same vicinity.
My usual boring signature: Nothing
 
-
Feb 9th, 2010, 01:41 AM
#39
Re: How many"k" are in my textboxes
haven't read all the posts but:
vb Code:
MsgBox((From xItem In asd Where LCase(xItem) = "k").Count)
-
Feb 9th, 2010, 01:47 AM
#40
Re: How many"k" are in my textboxes
and actually linq is always faster ... except in cases where you need to evaluate the same thing multiple times... such as:
From xItem in asd where xItem.Somefunction() = "a" or xItem.Somefunction() = "b"
as Somefunction will be evaluated 2x where in a loop you could store this in a variable... of course u could do it like this:
From xItem In (From xItem In asd Select xItem, xItem.Somefunction) Where xItem.Somefunction = "a" Or xItem.Somefunction = "b" Select xItem.xItem
Thus only evaluating it in the inner linq query 1 time.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|