问题
This will hopefully be my last question for a while and with what i have gained from yourselves will help me really get on with this project! So... i currently have variosu combo boxes and text boxes being put into a list of strings on FORM2, all blanks ignored then output into list boxes on FORM3. this is my final piece.. combobox 1 will be populated with say "Premium", then textbox 1 will have 4 values "400,500,600,700" then combo box2 with "Cover" and textbox2 "TPO,TPFT,COMP"
i want it to look as follows any ---- are just for spacing out as it appears to ignore the spacebar
Listbox1 --- | --- ListBox2
Premium ------------ 400
-------------------------500
-------------------------600
-------------------------700
Cover-----------------TPO
-------------------------TPFT
etc etc.
what i am getting is
Listbox1 --- | --- ListBox2
Premium ------------ 400
Cover-----------------500
-------------------------600
-------------------------700
-------------------------TPO
-------------------------TPFT
there is no relation between the fields and i haven't a clue how to best describe or look-up how to do this, make it output combo box one, then textbox1 split into lines into a second box. then under these the next combo box etc. so i guess combobox + ((Textbox 1 lines -1 value) as blank lines) if i could put pictures up this would be so much easier! darnnn youu reputation!!!!
At last i can have pictures!!!!!
As you can hopefully see, form 1 is irrelevant at the momemnt (will have no impact on the GUI for form2/3) but you pick various fields in form 2 and they ar mapped to form3 ATM some of the data is being mapped in a strange order. and there is no formatting between list 1 and 2Find below a early concept of Form2 and Form3
This is what i am trying to do, the data is lay out in a readable format (and behind the scenes ran through various DROOLS that will morph the data and print the results of this).
回答1:
The problem is that you are using the wrong control for the job. You need a control that can display multiple columns of data for each item in a list. The ListBox
control is not well-suited for that, since it is designed for just showing a single column.
Assuming this is a WinForm project, and not WPF, I would recommend using either a DataGridView
control, or a ListView
control. Either of those controls can show multiple columns, although, the ListView
control only shows multiple columns with its View
property is set to Details
. Since I'm partial to the ListView
control, myself, I'll give you an example using that one :)
First, add a ListView
control to your form. Then, set the View
property of the control, in the designer, to Details
. Then click the button to edit the Columns
property of the control. Add two columns. Then, use code like this to populate the list:
Dim item As ListViewItem = ListView1.Items.Add("Premium")
item.SubItems.Add("400")
item = ListView1.Items.Add("")
item.SubItems.Add("500")
item = ListView1.Items.Add("")
item.SubItems.Add("600")
item = ListView1.Items.Add("")
item.SubItems.Add("700")
item = ListView1.Items.Add("Cover")
item.SubItems.Add("TPO")
item = ListView1.Items.Add("")
item.SubItems.Add("TPFT")
If, as seems to be the case, the text for the first column is stored in a combo box, then the text for the second column are stored in a comma-delimited text box, then you could do something like this:
For Each cbo As ComboBox In MyComboBoxes
Dim first As Boolean = True
For Each value As String In GetTextBoxForComboBox(cbo).Text.Split(",")
Dim item As ListViewItem = Nothing
If first Then
item = ListView1.Items.Add(cbo.Text)
Else
item = ListView1.Items.Add("")
End If
item.SubItems.Add(value)
first = False
Next
Next
Of course, rather than having a method like GetTextBoxForComboBox
, it would be better to have have a class that stores the pairing of controls, like this:
Private Class ControlPair
Public Property Cbo As ComboBox
Public Property Txt As TextBox
End Class
Then, you could just loop through them like this:
For Each pair As ControlPair In MyPairs
' ...
For Each value As String In pair.Txt.Text.Split(",")
' ...
ListView1.Items.Add(pair.Cbo.Text)
' ...
Next
Next
For Each
is a convenient syntax for iterating through all of the items in an IEnumerable
object (basically any list such as an Array
or a List(Of T)
). So, in this case, the MyPairs
would need to be a list of ControlPair
objects, for instance:
Dim MyPairs As New List(Of ControlPair)()
Dim pair1 As New ControlPair()
pair1.Cbo = ComboBox1
pair1.Txt = TextBox1
MyPairs.Add(pair1)
Dim pair2 As New ControlPair()
pair2.Cbo = ComboBox2
pair2.Txt = TextBox2
MyPairs.Add(pair2)
Or, if you don't want to make your own class, you could just use Tuple(Of ComboBox, TextBox)
.
Actually, after seeing your screenshots, it looks like the TreeView
control may actually be more appropriate. You could have each combo box value show as a root-level node, and then inside each root-level node would be a child-node for each of the delimited values in the text box, like this:
- Premium
- 400
- 500
- 600
- 700
- Cover
- TPO
- TPFT
回答2:
Never rely on white space or blank lines for formatting. Write proper markup.
<form>
<label>Premium</label>
<select name="premium">
<option>400</option>
<option>500</option>
<option>600</option>
<option>700</option>
</select><br><br>
<label>Cover</label>
<select name="cover">
<option>TPO</option>
<option>TPFT</option>
<option>COMP</option>
</select>
</form>
来源:https://stackoverflow.com/questions/15920322/outputting-2-lists-of-data-but-keeping-them-organised-in-vb