VB Crew: problem setting Label Text

Discussion in 'OT Technology' started by Javi, Mar 3, 2008.

  1. Javi

    Javi New Member

    Joined:
    Sep 13, 2004
    Messages:
    37,785
    Likes Received:
    0
    Location:
    Houston, TX
    I hope to explain this correctly.

    Let's say on a form I have a label called "lblFederalHours".

    In the code I have a loop going. I want to do something like this:
    (AgencyName is determined from another loop)


    Code:
    While ATReader.Read()
       ("lbl"&AgencyName&"Students").Text = "something"
    End While
    
    Basically I want the code to do this:
    Code:
      lblFederalHours.Text = "something"
    
    I know I can't do with what I wrote above - can someone point me in the right direction?
     
  2. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    vb6? vb.net?
     
  3. Javi

    Javi New Member

    Joined:
    Sep 13, 2004
    Messages:
    37,785
    Likes Received:
    0
    Location:
    Houston, TX
    .NET/vb2005
     
    Last edited: Mar 3, 2008
  4. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    there is a way to do that.... but it's probably not the best solution. looping like that is a no-no. Windows is event-driven. Wouldn't you best to put the code into an event and not look? Such as the event that fires when the contents of a text box changes (hint hint)
     
  5. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    I THINK I understand your question. If you want to find labels dynamically, you can use the FindControl() function of the form. Like:


    Code:
    While ATReader.Read()
       Label lbl = (Label)MyForm.FindControl("lbl" & AgencyName & "Students");
       lbl.Text = "something";
    End While
    
    EDIT: I don't know VB very well, so hopefully you can massage my C# to fit your needs. The "iffy" part is with the typecast. I'm too lazy to look it up in VB :o
     
  6. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    Isn't it Labelname.Caption? That's what it was from VB2 to VB6; God help them if they actually changed that for no good reason, when they rolled out VB.NET.
     
  7. whup

    whup I wish you had children and.. so that I could step

    Joined:
    Feb 12, 2007
    Messages:
    1,603
    Likes Received:
    0
    They changed it to Text for a very good reason: to standardize the property rather than having to guess between Title/Text/Caption depending on what control you were using.

    Windows Forms does not have a FindControl method.

    What you'll have to do is something like:

    Code:
    While ATReader.Read()
       Controls["lbl"&AgencyName&"Students"].Text = "something"
    End While
    
    But don't forget to check for Null rather than just access the Text property :p

    Also you might want to have a look at this: How to: Make Thread-Safe Calls to Windows Forms Controls
     
  8. whup

    whup I wish you had children and.. so that I could step

    Joined:
    Feb 12, 2007
    Messages:
    1,603
    Likes Received:
    0
    Ewww I hate VB. I think that might have to be Controls with parentheses () rather than C# brackets [] soz.

    PS Learn C# dammit!
     
  9. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    :werd: MUCH better than the older way

    good call, i must have been thinking webforms :o
     
  10. Javi

    Javi New Member

    Joined:
    Sep 13, 2004
    Messages:
    37,785
    Likes Received:
    0
    Location:
    Houston, TX
    I will probably agree with this, but the code is in an event already. The reason why I'm doing this is that I need to fill a webform with 60+ unique values, all coming from a database from 20+ queries. So I'm using a loop to create the queries dynamically based on elements from an array.
     
  11. Javi

    Javi New Member

    Joined:
    Sep 13, 2004
    Messages:
    37,785
    Likes Received:
    0
    Location:
    Houston, TX

    This helped (as I'm using a webform). Here's the result:

    Code:
                    dim lbl as Label
                    strTemp = "lbl" & AgencyName & "Students"
                    lbl = form1.FindControl(strTemp)
                    lbl.Text = ATReader.Item("StudentsTrained").ToString()
    
    In the meantime in trying to find an answer, I came up with this:

    Code:
                    'Select Case AgencyName
                    '    Case "Federal"
                    '        lblFederalStudents.Text = ATReader.Item("StudentsTrained").ToString()
                    '    Case "State"
                    '        lblStateStudents.Text = ATReader.Item("StudentsTrained").ToString()
                    '    Case "Local"
                    '        lblLocalStudents.Text = ATReader.Item("StudentsTrained").ToString()
                    '    Case "Other"
                    '        lblOtherStudents.Text = ATReader.Item("StudentsTrained").ToString()
                    'End Select
    
    Like I said, this thing is nasty. Every iteration of the loop requires a new query, so that's why I'm doing this.
     
  12. Javi

    Javi New Member

    Joined:
    Sep 13, 2004
    Messages:
    37,785
    Likes Received:
    0
    Location:
    Houston, TX
    I do know C#, but my company wants to do everything in VB since we used ASP with vbscript :hs:
     
  13. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    Strangely, I never had any problem with it. *.Caption is for text that the app provides to the user, and *.Text is for text that the user provides to the app. Another way to put it is *.Caption is read-only and *.Text isn't.
     
  14. whup

    whup I wish you had children and.. so that I could step

    Joined:
    Feb 12, 2007
    Messages:
    1,603
    Likes Received:
    0
    Bastards!

    Sorry I assumed you were doing WinForms because you didn't say ASP.NET. SLED's way is definitely the best practice for that.

    deusexaethera I'm sure you didn't have an issue with the old way, but you can see how it's an improvement right?
     
  15. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    it's now "text" in .net... This was to standardize the property among many different controls (labels, text boxes, buttons, forms, etc). It also standardizes it between languages -- C#, VB, ASP, J#, etc)
     
  16. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    Very poor way to do it. I assume you have a "Query" button??? The code to generate teh SQL query should be called ONCE and should be called when the button_click event is fired.

    Your programming style is very poor and would be frowned upon by everyone I know. You have the opportunity to fix it... so do so!
     
  17. Javi

    Javi New Member

    Joined:
    Sep 13, 2004
    Messages:
    37,785
    Likes Received:
    0
    Location:
    Houston, TX
    i guess you misunderstand what I'm trying to do. one click (or in this case, a dropdown change) generates all the data I need.

    all the data I need is from two tables, but the structure of the tables prevent me from using just one query. if the DB design was better, maybe I could do more with it. but I'm working with what I have.
     
  18. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    then you're still better off generating the query in a subroutine, and calling that sub when the "changed" event fires of the respective control...

    The timer is a big no-no.

    Generally speaking, you want to avoid timers whenever possible.
     
  19. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    Not really. Why would you want all controls to use the same terminology for two different functions? What if you had a control like a questionnaire field, that has a spot to display text to the user AND a spot for user to enter their own text? Then what do you do? The old way had a solution for that: the text shown to the user is the .Caption, and the text the user types in is the .Text. They've gone and over-simplified it now.
     
  20. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    no, And .net has excellent support of custom properties... So if you were making a custom control, it would be rediculously simple for you to do what you wanted.
     
  21. Javi

    Javi New Member

    Joined:
    Sep 13, 2004
    Messages:
    37,785
    Likes Received:
    0
    Location:
    Houston, TX
    goddammit, who says I'm using a timer? :ugh:
    what i've done is exactly what you've outlined in your post.
     
  22. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    sorry, that was a diff thread/user.... But my point is still very valid... using a loop to "pull" is very bad juju. Windows is a "push" environment. Use events.
     
  23. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    I believe you're referring to polling vs. interrupting. Yes, interrupting is better; interrupts don't constantly consume resources, they just force the CPU to run your code when Event X occurs.

    Should I remember this the next time you bust my ass for dumbing-down technical terms to make them easier to understand?
     
  24. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    You didn't address the main portion of my post. Just saying "no, you're wrong" doesn't count; I wasn't asking your opinion.
     
  25. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    you posted on an internet forum. That is a de-facto acceptance that anyone/everyone may address you.

    If you want to ask a specific person a question, then you need to do it in a PM.
     

Share This Page