Results 1 to 3 of 3

Thread: TextBox object to string assignment - New to .net

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    1

    TextBox object to string assignment - New to .net

    New to vb.net, worked mainly with vb6 and 20 year old applications that I've written. As I begin to move into vb.net, 2022, I find some nice features, but some where I believe MS dropped the ball and could have gone a lot further with it.

    So the problem. Building forms with, let's say for example, 50 textboxes, 50 command button, etc. Currently using the following code to assign the textbox to a string array.

    Private Sub Initialize_frmMain_Control_Array()

    txtHostOrg(1) = TextBox1
    txtHostOrg(2) = TextBox2
    txtHostOrg(3) = TextBox3
    txtHostOrg(4) = TextBox4
    txtHostOrg(5) = TextBox5
    txtHostOrg(6) = TextBox6
    txtHostOrg(7) = TextBox7
    txtHostOrg(8) = TextBox8
    txtHostOrg(9) = TextBox9
    txtHostOrg(10) = TextBox10

    cmdButtons(1) = Button1
    cmdButtons(2) = Button2
    cmdButtons(3) = Button3
    cmdButtons(4) = Button4

    End Sub

    The textbox and command button routines are next

    '-------------------------------------------------------------------------------
    'intercepting textbox losing focus, then assign fields, or pass control for
    'handling any special field formatting
    '-------------------------------------------------------------------------------
    Private Sub TextBox_Leave_Procedure(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles TextBox1.Leave, TextBox2.Leave, TextBox3.Leave, TextBox4.Leave, TextBox5.Leave,
    TextBox6.Leave, TextBox7.Leave, TextBox8.Leave, TextBox9.Leave, TextBox10.Leave

    Dim iPtr As Short ' to hold textbox ##
    iPtr = CInt(Mid(sender.Name.ToString, 8)) ' get textbox ##

    Select Case iPtr
    Case 3 : Format_Phone_Number(3) ' format phone #
    Case 4 : Format_Phone_Number(4) ' format fax #
    Case Else
    txtHostFields(iPtr) = txtHostOrg(iPtr).Text ' assign data fields
    End Select

    'Field to TextBox are 1 for 1, meaning field(1) = textbox1, etc

    End Sub

    '---------------------------------------------------------------------------------
    'intercepting command button clicked, then pass control to Process_Cmd_Actions for
    'the button number that was triggered
    '---------------------------------------------------------------------------------
    Private Sub CmdButton_Procedure(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click

    Dim iPtr As Short ' to hold button ##
    iPtr = Int(Mid(sender.Name.ToString, 7)) ' get button ##
    Process_Cmd_Actions(iPtr)

    'Button1=Exit, 2=Browse/Select, 3=Edit Organization, 4=Save/Update

    End Sub

    Trying to understand in the first part, how I would the number of the textbox, then assign "TextBox" & # and end up duplicating the full assignments as first indicated.

    A lot of this is new to me, just trying to see if anyone else has run into this, and has a workable solution.

    Thank you,

    Kevin

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

    Re: TextBox object to string assignment - New to .net

    Quote Originally Posted by yaggieke View Post
    assign the textbox to a string array
    First things first, you wouldn't do that because TextBoxes are TextBoxes and Strings are Strings. What is it that you actually want? Do you want an array of TextBoxes or do you want an array of Strings? If it's the latter, you need to get the Text property of each TextBox, not the TextBoxes themselves. Given the code you use later, it appears that you actually want a TextBox array, so the code is correct but your description of it is not.

    Anyway, the simplest option is to make sure that all those TextBoxes are in the same container, in the appropriate z-order and are the only controls of that type in that container. You can check the z-order using the Document Outline window in the designer. For that last condition, you can add a Panel to your form to contain the TextBoxes if needs be. You can then do this:
    vb.net Code:
    1. txtHostOrg = myContainer.Controls.OfType(Of TextBox)().ToArray()
    That will create the array as well as populate it, so there's no need to create it elsewhere. You can do something similar for the Buttons. Note that 'myContainer' would be the form or the Panel or whatever that directly contains the controls. Note also that arrays are zero-based in .NET, so the first element is at index 0.

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

    Re: TextBox object to string assignment - New to .net

    Don't do this:
    vb.net Code:
    1. Dim iPtr As Short ' to hold textbox ##
    2. iPtr = CInt(Mid(sender.Name.ToString, 8)) ' get textbox ##
    or this:
    vb.net Code:
    1. Dim iPtr As Short ' to hold button ##
    2. iPtr = Int(Mid(sender.Name.ToString, 7)) ' get button ##
    That that code will even compile means that you must have Option Strict Off. You should set it On in the project properties immediately and also in the VS options, so that it is On by default for all future projects.

    You should put the appropriate number in the Tag property of each control. You can then get that number in an event handler like so:
    vb.net Code:
    1. Dim controlNumber = CInt(DirectCast(sender, Control).Tag)
    The thing is, I would question whether you really need these numbers at all. You're still thinking like you're using VB6. Having a single event handler for multiple controls and then working out what different action to perform for each control totally defeats the purpose. Only use a common event handler if you want to do the same thing for each control. If you want to perform different actions when you click different Buttons, just have a different event handler for each Button. You can then forget about identifying which action to perform using those numbers.

    Similarly for your TextBoxes, you can have one event handler for the ones that need phone numbers formatted and another for those that don't. That said, you probably don't even need to populate a String array when the user leaves a TextBox. Just get all the values when you need them using LINQ, much as I demonstrated for the TextBox array:
    vb.net Code:
    1. Dim allTextValues = myContainer.Controls.
    2.                                  OfType(Of TextBox)().
    3.                                  Select(Function(tb) tb.Text).
    4.                                  ToArray()
    If some values need to be processed, you could indicate that in the Tag of the control, e.g.
    vb.net Code:
    1. Dim allTextValues = myContainer.Controls.
    2.                                  OfType(Of TextBox)().
    3.                                  Select(Function(tb) If(CStr(tb.Tag) = "phone", FormatPhoneNumber(tb.Text), tb.Text)).
    4.                                  ToArray()

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