I have a string array and want to bind to this array with some text boxes on a windows form
The array has a fixed length of 5 elements, on my form I have 5 text boxes, I want to bind textbox1 to element 1 of the string array, textbox2 to element 2 of the string array and so on.
How do I go about that? Is it even possible?
txtMonth1.DataBindings.Add(New Binding("Text", bsReport, "LabelName(0)", True))
txtMonth2.DataBindings.Add(New Binding("Text", bsReport, "LabelName(1)", True))
etc
etc
Cheers
Yes, you can bind TextBoxes to individual elements of an array.
Something like below:
txtMonth1.DataBindings.Add(New Binding("Text", bsReport[0], "LabelName(0)", True))
txtMonth2.DataBindings.Add(New Binding("Text", bsReport[1], "LabelName(1)", True))
Related
After a user clicks on any number of items in a CheckedListBox, I want to programmatically remove the checks on those items when the window is closed. With the CheckedListBox named lstChoices, I have:
For I As Integer = 0 To lstChoices.SelectedItems.Count - 1
Dim lbi As Xceed.Wpf.Toolkit.Primitives.SelectorItem = CType(lstChoices.ItemContainerGenerator.ContainerFromIndex(I), Xceed.Wpf.Toolkit.Primitives.SelectorItem)
lbi.IsSelected = False
Next
The problem is that the SelectedItems property is NOT the selected items. Oddly, the SelectedItems.Count property is computed correctly but the loop just goes through the first ListBoxItems up to Count whether they are selected or not.
The documentation says the SelectedItems property "Gets the collection of checked items". Either the documentation is wrong or there's a bug in that control. How can I get just the checked items.
You are currently iterating over the Items collection as ContainerFromIndex returns an item based on the Items property and not the SelectedItems property.
You should iterate the SelectedItems and use lstChoices.ItemContainerGenerator.ContainerFromItem instead:
For index As Integer = 0 To lstChoices.SelectedItems - 1
Dim selectedItem = lstChoices.SelectedItems(index)
Dim selectedItemContainer = TryCast(lstChoices.ItemContainerGenerator.ContainerFromItem(selectedItem), Selector)
selectedItemContainer.IsSelected = False
Next
You have to be careful to make the difference between the elements of SelectedItems and Items.
Try this :
For I As Integer = 0 To lstchoices.SelectedItems.Count - 1
lstchoices.SetItemCheckState(lstchoices.Items.IndexOf(lstchoices.SelectedItems(I)), CheckState.Unchecked)
Next
I have subclassed the WPF DataGrid in my VB.NET application because I will need to use this component frequently but also need to have some extra features, in this case adding a new row when the tab key is pressed on the bottom right cell.
I have Overidden the OnKeyDown event of the base class. This is being triggered when the tab keys is pressed while the grid is focused, exactly as I want. However, when the event is triggered I need to be able to determine whether I'm on the bottom-right cell or not. To do this I'd like to get the SelectedItem property of my DataGrid and use that to determine which cell is selected.
I am doing all of this programmatically because I don't want users to have to write any more XAML than they would have to for a regular datagrid. It should function in exactly the same way except if you tab on the bottom right cell a new row is added and the first cell of that row is selected. this should apply no matter how many rows/columns the user has in the datagrid.
The code below shows what I want to do but the SelectedItem is not set.
Protected Overrides Sub OnKeyDown(e As KeyEventArgs)
MyBase.OnKeyDown(e)
If e.Key = Key.Tab Then
Dim colIndex As Integer = Me.Columns.IndexOf(Me.CurrentColumn)
Dim colCount As Integer = Me.Columns.Count - 1
If -1 = colIndex Then
'the next line throws a System.NullReferenceException because SelectedItem is not set
If SelectedItem.Equals(Items(Items.Count - 1)) Then
Focus()
Dim dgrCI = New DataGridCellInfo(Items(Items.Count - 1), Columns(colIndex))
ScrollIntoView(Items(Items.Count - 1))
BeginEdit()
End If
End If
End If
End Sub
The SelectedItem property should be set to the last item in the table but is instead set to Nothing. Why is this?
Edit:
The answer from Ppp is correct. it seems that by the time the OnKeydown event is triggered the DataGrid has already lost focus. I resolved this issue by using the OnPreviewKeydown event instead.
When your tab key is pressed in the last cell you basically tab out of the datagrid which is why I am guessing your SelectedItem is null.
I have an assignment to create a program that basically grades a multiple choice test. I have a form with 20 textboxes to accept input (only the letters "a", "b", "c", or "d"), an array with the correct answers, and I created an array to hold all the text boxes:
dim txtboxes() as TextBox = {txtInput1, txtInput2...txtInput20}
I know there's got to be a way to use For Each where the type of control = textbox to iterate over all the textboxes, but I am kind of stuck on actually extracting the values from the textboxes and adding them to an array of their own. I've gotten as far as:
dim txtbox as TextBox
for each txtbox in Controls.OfType(Of TextBox)
which isn't very far at all...
The following code should return the list of textbox values.
Public Sub Convert()
Dim lstTxtBoxValues As List(Of String) = New List(Of String)
For Each txtBox As TextBox In txtboxes
lstTxtBoxValues.Add(txtBox.Text)
Next
End Sub
In my form I have a DataGridView bound to a BindingSource that has a DataSet as DataSource. I also have some TextFields and ComboBoxes on the form that are bound to different columns in the DataSet through the BindingSource. The idea is that the values in the columns on the selected row in the DataGridView are reflected in the other controls on the form. Maybe I make it sound a bit complicated, but it's fairly easy to connect the TextFields, and also the ComboBoxes bound to tables in the dataset.
My problem is that this time I want to set the items in the ComboBox from an array and not from a table in the DataSet. This is what I've tried:
Me.ComboBox.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.TblBindingSource, "ColumnName", True))
Dim ItemArray(2) As String
ItemArray(0) = ""
ItemArray(1) = "Default"
ItemArray(2) = "User-set"
ComboBox.DataSource = ItemArray
Now, this seems to work partially as the ComboBox is populated correctly, and I can select a value, and it appears in the DataGridView. But it doesn't update its selected value as I change rows in the DataGridView. The column ("ColumnName") is a ComboBoxColumn that gets its item list in the way shown above, and it seams to work as expected.
If it wasn't clear; I have several ComboBoxes with similar functionality that works, but they are bound to a column in a DataTable, as follows:
Me.ComboBox1.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.Tbl1BindingSource, "WiDMethodX", True))
Me.ComboBox1.DataSource = Me.Tbl2BindingSource
Me.ComboBox1.DisplayMember = "SomeColumn"
Me.ComboBox1.ValueMember = "SomeColumn"
If it matters, the DataSet comes from an Access Database.
SelectedValue is used in conjunction with the ValueMember property, but since your array list doesn't have descriptive fields, that won't work.
Try using SelectedItem for your binding:
Me.ComboBox.DataBindings.Add(New Binding("SelectedItem", _
Me.TblBindingSource, "ColumnName", True))
I realize this questions is old, but I had similar problem I resolved. I bound the text property of the combo box to the binding source member that relates to the value member or display member of my combo box datasource. Make sure that you fill your data tables (for binding source and combo box datasource) AND bind your combo box to its datasource prior to databinding text of combo box.
Dim dtForBindingSource as DataTable
Dim bs as BindingSource
Dim dtForComboBox as DataTable
'Code to fill dtForBindingSource would go here
bs.DataSource = dtForBindingSource
'Code to fill dtForComboBox would go here
ComboBox.DataSource = dtForComboBox
ComboBox.DisplayMember = "ColumnToDisplay"
ComboBox.ValueMember = "ColumnXYZ"
'Now that datasources exist and combo box is set up I do databindings.
ComboBox.DataBindings.Add("Text", bs, "ColumnToDisplay")
I'm trying to populate a ComboBox programatically. I am creating ComboBoxItems and would like to set their text (the text that is visible for the end-user) and their value (the object that I will handle in the background after the user has selected it.
However the ComboBoxItem seems to only have one member for these two requirements: the Content variable. At the same time this would not fit my needs as I want to distinguish the text and value properties and want to do this without data binding. Is there some viable solution to achieve this?
My current code looks as follows:
ComboBox comboBox;
ComboBoxItem item = new ComboBoxItem();
item.Content = "First Item";
item.Value = 1; // Does not work, no such member as Value!
comboBox.Items.Add(item);
Guess you can use the Tag property.