-
Aug 17th, 2010, 01:35 PM
#1
Thread Starter
Lively Member
DirectCast Help (Easy Question I hope!)
Hey guys...
I have this chunk of code that worked in vb 2008 on the 3.5 framework, but after upgrading to 4.0, i keep getting this error right when i say "For each ctl in Control"
"Control is a type and cannot be used as an expression"
Am i missing a reference to somewhere? I notice that there is a control class available to me, but i played around with it and didn't get too far.
Thanks guys,
Code Code:
Public Sub DefaultValueisZero()
Dim ctl As Control
Dim txt As TextBox
For Each ctl In Control
If TypeOf ctl Is TextBox Then
txt = DirectCast(ctl, TextBox)
If Len(txt.Text) = "0" Then
txt.Text = "0"
End If
End If
Next
End Sub
-
Aug 17th, 2010, 01:41 PM
#2
Re: DirectCast Help (Easy Question I hope!)
Control is just a control, so why are you trying to get each out of it? You need a collection in there, such as:
For each ctl As Control in Me.Controls
which is what it looks like you meant to write anyways.
My usual boring signature: Nothing
-
Aug 17th, 2010, 01:45 PM
#3
Thread Starter
Lively Member
Re: DirectCast Help (Easy Question I hope!)
what you said does make sense, but when I tried the Me.Controls (which is what I meant to write lol), I get a new error...
"Controls is not a member of Project1.MainWindow"
I am working in WPF instead of windows forms, but I'm not sure if that makes a difference.
-
Aug 17th, 2010, 03:10 PM
#4
Re: DirectCast Help (Easy Question I hope!)
I have never worked with WPF, so I really can't say, but it sure looks like it is different, as every form and control has a Controls collection in a normal windows program.
My usual boring signature: Nothing
-
Aug 17th, 2010, 03:12 PM
#5
Thread Starter
Lively Member
Re: DirectCast Help (Easy Question I hope!)
ok. thanks for trying to help though. I definitely appreciate it..
-
Aug 17th, 2010, 04:19 PM
#6
Re: DirectCast Help (Easy Question I hope!)
Yes, that does make a difference... WinForms and WPF are two different technologies.
I'll ask a mod to move this to the WPF section... you'll probably get a better response there.
-tg
-
Aug 17th, 2010, 05:26 PM
#7
Re: DirectCast Help (Easy Question I hope!)
-
Aug 18th, 2010, 02:30 AM
#8
Re: DirectCast Help (Easy Question I hope!)
Thread moved from 'VB.Net' forum to 'WPF, WCF, WF' forum.
(thanks for letting us know tg )
-
Aug 18th, 2010, 07:21 AM
#9
Thread Starter
Lively Member
Re: DirectCast Help (Easy Question I hope!)
ah! that would help. Sorry about that.
-
Aug 18th, 2010, 08:01 AM
#10
Thread Starter
Lively Member
Re: DirectCast Help (Easy Question I hope!)
ok guys, I've written this, and I think it's definitely closer to the solution, but I can't figure out how to cycle through my textboxes... see error below
vb Code:
Dim ctl As FrameworkElement Dim txt As TextBox For Each ctl In PositonProfileGrid.Children 'Get an error in this next line ' "ctl can never be a textbox, etc..." since ctl is a FrameworkElement 'and TextBox is part of Windows.Forms... If TypeOf ctl Is TextBox Then 'Do stuff here End If Next
-
Aug 18th, 2010, 08:06 AM
#11
Re: DirectCast Help (Easy Question I hope!)
-
Aug 18th, 2010, 08:36 AM
#12
Thread Starter
Lively Member
Re: DirectCast Help (Easy Question I hope!)
thanks tg, but i did look at all of that prior to posting, but it didn't quite solve my issue. I don't understand how WPF can't access the Textbox control, even if it is a form component. i mean, you can drop a textbox onto your WPF app just like you would do in a winform...
-
Aug 18th, 2010, 08:52 AM
#13
Thread Starter
Lively Member
Re: DirectCast Help (Easy Question I hope!)
ok, this is what I did, and it is working so far, although it isn't pretty.
vb Code:
Dim textboxcollection As New Collection textboxcollection.Add(Position1) textboxcollection.Add(Position2) textboxcollection.Add(Position3) textboxcollection.Add(Position4) textboxcollection.Add(Position5) textboxcollection.Add(Position6) textboxcollection.Add(Force1Value) textboxcollection.Add(Force2Value) textboxcollection.Add(Force3Value) textboxcollection.Add(Force4Value) textboxcollection.Add(Force5Value) textboxcollection.Add(Force6Value) Dim txt As Windows.Controls.TextBox For Each txt In textboxcollection If Len(txt.Text) = "0" Then txt.Text = "0" End If Next
-
Aug 18th, 2010, 09:00 AM
#14
Re: DirectCast Help (Easy Question I hope!)
You're thinking like a WinForms developer... you're taming a lion when what you have is really an elephant...
In the 30 seconds it took for me to click through some of the results... the answer seems to be that you have to go through the visualTreeHelper
http://social.msdn.microsoft.com/For...5-9168cb4bb8dd
Also your problem may be that it's not a text BOX but a text BLOCK... as noted here: http://www.vbforums.com/showpost.php...48&postcount=5
-tg
-
Aug 18th, 2010, 09:05 AM
#15
Thread Starter
Lively Member
Re: DirectCast Help (Easy Question I hope!)
I'll be honest, I don't know how to use the visual tree helper, and I got lazy and didn't feel like learning how...BUT...I did figure out how to clean up my original code...you have to use the windows.controls.textbox instead of windows.forms.textbox, which was pretty much my problem all along...
Is this a bad way to do it?
and i did see the textblock error in the posts by the way.
vb Code:
Dim ctl As FrameworkElement Dim ctl2 As FrameworkElement Dim txt As Windows.Controls.TextBox For Each ctl In ForceProfileGrid.Children If TypeOf ctl Is Windows.Controls.TextBox Then txt = DirectCast(ctl, Windows.Controls.TextBox) If Len(txt.Text) = "0" Then txt.Text = "0" End If End If Next For Each ctl2 In PositonProfileGrid.Children If TypeOf ctl2 Is Windows.Controls.TextBox Then txt = DirectCast(ctl2, Windows.Controls.TextBox) If Len(txt.Text) = "0" Then txt.Text = "0" End If End If Next
Last edited by Isaacx; Aug 18th, 2010 at 09:15 AM.
-
Aug 18th, 2010, 10:39 AM
#16
Re: DirectCast Help (Easy Question I hope!)
That is how I would do it, enumerating all of the children of the Grid (though there are a few small things that could be tidied up in your code)
-
Aug 18th, 2010, 10:42 AM
#17
Thread Starter
Lively Member
Re: DirectCast Help (Easy Question I hope!)
Yeah, I figured as much. is there a way to combine those two grids (position and force) into one grid without going into XAML? That way, I can just run the code once instead of repeating the same thing twice.
-
Aug 19th, 2010, 01:56 AM
#18
Re: DirectCast Help (Easy Question I hope!)
First up, this:
vb.net Code:
Dim ctl As FrameworkElement
Dim ctl2 As FrameworkElement
Dim txt As Windows.Controls.TextBox
For Each ctl In ForceProfileGrid.Children
If TypeOf ctl Is Windows.Controls.TextBox Then
txt = DirectCast(ctl, Windows.Controls.TextBox)
If Len(txt.Text) = "0" Then
txt.Text = "0"
End If
End If
Next
can be far more succinctly written like this:
vb.net Code:
For Each txt In Me.ForceProfileGrid.Children.OfType(Of TextBox)()
If txt.Text.Length = 0 Then
txt.Text = "0"
End If
Next
As for doing the same thing twice, you should never. In most cases you should write a method that does the work and then call it multiple times with different parameters. In this case, it's easier to concatenate two sequences:
vb.net Code:
For Each txt In Me.ForceProfileGrid.Children.OfType(Of TextBox)().Concat(Me.PositionProfileGrid.Children.OfType(Of TextBox)())
If txt.Text.Length = 0 Then
txt.Text = "0"
End If
Next
-
Aug 19th, 2010, 07:38 AM
#19
Thread Starter
Lively Member
Re: DirectCast Help (Easy Question I hope!)
wow. that looks much better! Thanks everyone!
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
|