skip to Main Content

I have several data fields that I want to be on one line and kept in fixed size columns in a combo box. I have tried padding and looks good till I add to combo box, where string loses its format.

Before adding string lines they look like this.

    "aaa    " - "bbbbbbbb " - "ccccc  "
    "aaaaa  " - "bbb      " - "cc     "

after add, listing in combo box shows this

    "aaa" - "bbbbbbb" -  "ccccc"
    "aaaaa" - "bbb" -  "cc"

I have tried stringbuilder and arrays but meet with errors such as can’t type to listitems, etc. How can you maintain a string format that is made up of several variables, in a combo box?

2

Answers


  1. Try formatting your font with a fixed width and then using string you can format with tabs.
    ie something like this

    ComboBox1.Font = New Font("Courier New", 10)
    Dim item1 As String = "aaa" & vbTab & "bbbbbbbb" & vbTab & "ccccc"
    Dim item2 As String = "aaaaa" & vbTab & "bbb" & vbTab & "cc"
    ComboBox1.Items.Add(item1)
    ComboBox1.Items.Add(item2)
    
    Login or Signup to reply.
  2. This can be done if you use a fixed size font.

    You also have to convert blanks to a html non breaking space (nbsp;).

    And you also have to use HTML encoding for the string.

    With above in mind?

    So, say we select a hotel (saving the hidden PK, but display hotel name and city and rate.

    So, markup:

            <asp:DropDownList ID="ddlHotels" runat="server" 
                Height="30px" Width="500px" 
                DataTextField="HotelName"
                DataValueField="ID"
                Font-Names="Courier New">
            </asp:DropDownList>
    

    And code behind:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            LoadData
        End If
    End Sub
    
    Sub LoadData()
    
        Using con As New SqlConnection(My.Settings.TEST4)
            Dim strSQL =
                "SELECT ID, HotelName, City, NightRate from tblHotelsA ORDER BY HotelName"
    
            Using cmdSQL As New SqlCommand(strSQL, con)
                con.Open()
                Dim rstData As New DataTable
                rstData.Load(cmdSQL.ExecuteReader)
    
                For Each dRow In rstData.Rows
                    Dim sOneLine As String = ""
                    sOneLine = dRow("HotelName").ToString().PadRight(32) & "| " &
                        dRow("City").ToString().PadRight(15) & "| " &
                        String.Format("{0:C2}", dRow("NightRate")).ToString().PadLeft(8)
                    sOneLine = HttpContext.Current.Server.HtmlDecode(sOneLine.Replace(" ", "&nbsp;"))
    
                    ddlHotels.Items.Add(New ListItem(sOneLine, dRow("ID")))
    
                Next
    
            End Using
        End Using
    

    Result:

    enter image description here

    I would suggest that you pop a dialog, and thus say popup a GridView, as that would allow better formatting, but the above can work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search