Results 1 to 19 of 19

Thread: DirectCast Help (Easy Question I hope!)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    69

    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:
    1. Public Sub DefaultValueisZero()
    2.  
    3.         Dim ctl As Control
    4.         Dim txt As TextBox
    5.  
    6.         For Each ctl In Control
    7.             If TypeOf ctl Is TextBox Then
    8.  
    9.                 txt = DirectCast(ctl, TextBox)
    10.  
    11.                 If Len(txt.Text) = "0" Then
    12.  
    13.                     txt.Text = "0"
    14.  
    15.                 End If
    16.  
    17.             End If
    18.  
    19.         Next
    20.  
    21.     End Sub

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    69

    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.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    69

    Re: DirectCast Help (Easy Question I hope!)

    ok. thanks for trying to help though. I definitely appreciate it..

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: DirectCast Help (Easy Question I hope!)

    For future reference

    WPF
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: DirectCast Help (Easy Question I hope!)

    Thread moved from 'VB.Net' forum to 'WPF, WCF, WF' forum.

    (thanks for letting us know tg )

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    69

    Re: DirectCast Help (Easy Question I hope!)

    ah! that would help. Sorry about that.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    69

    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:
    1. Dim ctl As FrameworkElement
    2.         Dim txt As TextBox
    3.  
    4.         For Each ctl In PositonProfileGrid.Children
    5.             'Get an error in this next line
    6.             ' "ctl can never be a textbox, etc..." since ctl is a FrameworkElement
    7.             'and TextBox is part of Windows.Forms...
    8.             If TypeOf ctl Is TextBox Then
    9.  
    10. 'Do stuff here
    11.  
    12.             End If
    13.  
    14.  
    15.         Next

  11. #11
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: DirectCast Help (Easy Question I hope!)

    Do any of these help?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    69

    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...

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    69

    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:
    1. Dim textboxcollection As New Collection
    2.  
    3.         textboxcollection.Add(Position1)
    4.         textboxcollection.Add(Position2)
    5.         textboxcollection.Add(Position3)
    6.         textboxcollection.Add(Position4)
    7.         textboxcollection.Add(Position5)
    8.         textboxcollection.Add(Position6)
    9.         textboxcollection.Add(Force1Value)
    10.         textboxcollection.Add(Force2Value)
    11.         textboxcollection.Add(Force3Value)
    12.         textboxcollection.Add(Force4Value)
    13.         textboxcollection.Add(Force5Value)
    14.         textboxcollection.Add(Force6Value)
    15.  
    16.         Dim txt As Windows.Controls.TextBox
    17.         For Each txt In textboxcollection
    18.  
    19.             If Len(txt.Text) = "0" Then
    20.                 txt.Text = "0"
    21.             End If
    22.         Next

  14. #14
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    69

    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:
    1. Dim ctl As FrameworkElement
    2.         Dim ctl2 As FrameworkElement
    3.         Dim txt As Windows.Controls.TextBox
    4.  
    5.         For Each ctl In ForceProfileGrid.Children
    6.          
    7.             If TypeOf ctl Is Windows.Controls.TextBox Then
    8.  
    9.                 txt = DirectCast(ctl, Windows.Controls.TextBox)
    10.  
    11.                 If Len(txt.Text) = "0" Then
    12.  
    13.                     txt.Text = "0"
    14.  
    15.                 End If
    16.  
    17.             End If
    18.  
    19.         Next
    20.  
    21.         For Each ctl2 In PositonProfileGrid.Children
    22.          
    23.             If TypeOf ctl2 Is Windows.Controls.TextBox Then
    24.  
    25.                 txt = DirectCast(ctl2, Windows.Controls.TextBox)
    26.  
    27.                 If Len(txt.Text) = "0" Then
    28.  
    29.                     txt.Text = "0"
    30.  
    31.                 End If
    32.  
    33.             End If
    34.  
    35.         Next
    Last edited by Isaacx; Aug 18th, 2010 at 09:15 AM.

  16. #16
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  17. #17

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    69

    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.

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: DirectCast Help (Easy Question I hope!)

    First up, this:
    vb.net Code:
    1. Dim ctl As FrameworkElement
    2.         Dim ctl2 As FrameworkElement
    3.         Dim txt As Windows.Controls.TextBox
    4.  
    5.         For Each ctl In ForceProfileGrid.Children
    6.          
    7.             If TypeOf ctl Is Windows.Controls.TextBox Then
    8.  
    9.                 txt = DirectCast(ctl, Windows.Controls.TextBox)
    10.  
    11.                 If Len(txt.Text) = "0" Then
    12.  
    13.                     txt.Text = "0"
    14.  
    15.                 End If
    16.  
    17.             End If
    18.  
    19.         Next
    can be far more succinctly written like this:
    vb.net Code:
    1. For Each txt In Me.ForceProfileGrid.Children.OfType(Of TextBox)()
    2.     If txt.Text.Length = 0 Then
    3.         txt.Text = "0"
    4.     End If
    5. 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:
    1. For Each txt In Me.ForceProfileGrid.Children.OfType(Of TextBox)().Concat(Me.PositionProfileGrid.Children.OfType(Of TextBox)())
    2.     If txt.Text.Length = 0 Then
    3.         txt.Text = "0"
    4.     End If
    5. Next

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    69

    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
  •  



Click Here to Expand Forum to Full Width