2014年7月13日 星期日

[RESOLVED] Line up drop down menu items?


Hi,


Using the code below I am populating a drop down menu but the text does not line up. Is there a way so that when the drop down loads that the City part is all in line? Currently the city part can be more to left or more to the right depending on the length
of the column before it? Thanks!


While reader.Read
customers.Add(reader("Provider Last Name (Legal Name)").ToString + ", " + reader("Provider First Name").ToString + " " + reader("Provider Business Mailing Address City Name").ToString + ", " + reader("Provider Business Mailing Address State Name").ToString + " " + reader("Provider Business Mailing Address Postal Code").ToString)

End While



I've created some similar functionality in the past for aligning items within Dropdowns that leverages the empty character (ALT+255) to create spaces within drop-downs that are recognized within


It uses some of the available padding within the String formatting function to allocate exactly how many characters you want to use for a specific field and then has an additional function to replace those spaces with the "invisible character" : 


string FormatForDropDown(string s, int length)
{
//Builds a string
StringBuilder sb = new StringBuilder();
//Iterates through and replaces the empty values with the empty character (not a space)
for (int i = 0; i < length; i++)
{
sb.Append((i < s.Length) ? s[i].ToString() : " ");
}
//Outputs the string
return sb.ToString();
}

and as far as actually adding the actual entry, it should be used as such : 


//Example of the item you would add to your Drop-down list
String.Format("{0,24} | {1,12} | {2,24}", FormatForDropDown(valueA, 24), FormatForDropDown(valueB, 12), FormatForDropDown(valueC, 24));

(Warning : This is old code)







Since you are using Visual Basic, it may look something like this : 


Public Function FormatForDropDown(ByVal s As String, ByVal length As Integer) As String
Dim sb = New StringBuilder()
For i As Integer = 0 To length
sb.Append(If(i < s.Length, s(i).ToString(), " "))
Next
Return sb.ToString()
End Function

and


'String to Add'
Dim entry = String.Format("{0,24} | {1,24} | {2,24} | {3,24} | {4, 24}",reader("Provider Last Name (Legal Name)").ToString(),reader("Provider Business Mailing Address City Name").ToString(), reader("Provider First Name").ToString(),reader("Provider Business Mailing Address State Name").ToString(), reader("Provider Business Mailing Address Postal Code").ToString()
'Add the entry'
customers.Add(entry)

Alternatively, you could simply replace the strings of spaces that you are using with the invisible character " " as mentioned : 


While reader.Read
customers.Add(reader("Provider Last Name (Legal Name)").ToString() + ", " + reader("Provider First Name").ToString + "          " + reader("Provider Business Mailing Address City Name").ToString() + ", " + reader("Provider Business Mailing Address State Name").ToString() + "  " + reader("Provider Business Mailing Address Postal Code").ToString())
End While

I attempted to replace them in the code above however it may not have worked properly. It's just important to note that all of the spaces that you see above within your strings are not actual spaces but the invisible character (ALT+255).







Thanks, this seems to be close to what i need, proglem is some of the last names have a "-" so it throws off the alignment. Thanks for your help with this. Maybe can it be a set width so its like an excel sheet where its just like a straight line between
with a set length?


               JOHNSON |                    AJITA |                     BEAR |                       DE |                197013036


               JOHNSON-Smith |                    AJITA |                     BEAR |                       DE |                197013036


 


沒有留言:

張貼留言