am building a task management system using vb.net. How to set a textbox field beside each checkboxlist so when the user tick the checkbox he can comment too for this item and submit the whole finished tasks ? below is my code
Sub GetGroups()
cblGroups.DataSource = Task.Components.Tasks.GetAllTasks
cblGroups.DataTextField = "TaskName"
cblGroups.DataValueField = "ID"
cblGroups.DataBind()
End Sub
For Each item As ListItem In cblGroups.Items
If item.Selected Then
'reading each item value
End if
Next
I think you should be using a DataGridView instead. In there you can have a checkbox column and a textbox column, also a new row placeholder to put new tasks. If you don't like DataGridView, you can use alternatives.
Your other option would be to maintain a list of CheckBox controls and their TextBox pairs, essentially doing data grid's job. You are okay at first, but then you may need scrolling etc., so why not use a built-in control, where such issues are already solved out of the box.
'Check through each of the items
For Each item As ListItem In cblGroups.Items
'If this particular item is checked
If item.Selected = True Then
'Dynamically create a HTML Textbox
item.Text = [String].Format("{0}<input id=""TextBox{0}"" name=""TextBox{0}"" / >", item.Text)
Else
'Otherwise simply store the normal value
item.Text = [String].Format("{0}", item.Text)
End If
Next
Related
Probably it's very easy, but I have searched for a post or somehone who have the same problem without fortune. I have a Listbox in Vb Net, which contains a list of names.
in vb6, while typing in the listbox the selected item changed automatically based on the letters typed until completion, but I can't find a method that repeats the same thing in VS, as the only thing it lets me do is identify only the first one letter typed in the listbox. So, if in the list there are two similar names like Autocarro or Automobile, if after the 'A' I type the 'U' the cursor moves to the 'U' of 'Urban'.
Could anyone help me find a solution whithout using a textbox?
Thanks in advance
You can use a ComboBox with the DropDownStyle set to Simple. This displays a TextBox combined with a ListBox (hence the name Combo Box).
To make it select entries automatically you can add a TextChanged event handler with the following code:
private void ComboBox1_TextChanged(object sender, EventArgs e)
{
int inputLength = comboBox1.Text.Length;
if (inputLength > 0) {
int index = comboBox1.FindString(comboBox1.Text);
if (index >= 0) {
comboBox1.SelectedIndex = index;
comboBox1.Focus();
comboBox1.SelectionStart = inputLength;
comboBox1.SelectionLength =
((string)comboBox1.Items[index]).Length - inputLength;
}
}
}
Note, however, that editing the text box part becomes a bit difficult as the selection mechanism kicks in every time you edit the text. E.g. deleting text with Dellete or Backspace does not work well, as the automatic selection restores the text immediately. You can delete the whole text by typing Ctrl-A, Delete.
ComboBox after having typed "cl":
Try this (Works both for text box and combobox, not sure if it works directly in list box, you can try in this lime), what you can do is add textbox on top of list box and run second code to add data to textbox as suggestion
ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
Or this one
Dim iddata As New AutoCompleteStringCollection()
Dim idtable As New DataTable
idtable = (your datatable source)
For Each r As DataRow In idtable.Rows
iddata.Add(r("id").ToString)
Next
With Textbox1
.AutoCompleteCustomSource = iddata
.AutoCompleteMode = AutoCompleteMode.Suggest
.AutoCompleteSource = AutoCompleteSource.CustomSource
End With
Once I select a value from a Combobox and save the form, ComboBox.SelectedText contains the value selected but ComboBox.SelectedIndex is returning 0 always for each item in the list. Below is just a sample code for reference.
If (combobox1.SelectedIndex = 0 Or combobox1.SelectedText = "")
MessageBox.Show("No value selected")
else
MessageBox.Show("Some value selected")
End If
Some code to illustrate usage
Private Sub ComboBox1_SelectedIndexChanged(sender As Object,
e As EventArgs) Handles ComboBox1.SelectedIndexChanged
'check for no item selected
If ComboBox1.SelectedIndex < 0 Then
Stop 'no item
Else
Dim idx As Integer = ComboBox1.SelectedIndex
Dim val As String = CStr(ComboBox1.SelectedItem) '<-- use SelectedItem
Stop
End If
End Sub
Small Answer
SelectedText is not the same as SelectedItem. Take a look at ComboBox.SelectedText:
Gets or sets the text that is selected in the editable portion of a ComboBox.
I think that you are confusing this with ComboBox.SelectedItem
SelectedIndex: -1 if nothing selected, otherwise the index of the selected item
SelectedValue: null if nothing selected, otherwise the selected item
SelectedText: the text that the operator marked in the editable part of the combo box.
** Room for improvement **
You use VB. My VB is a bit rusty, so I'll give my answer in C#. I guess you'll get the gist.
In Winforms, whenever you want to fill a that shows a sequence of items, like ComboBoxes, ListBoxes, DataGridViews, Charts, etc, there are usually two methods:
Fill the ComboBox one by one with the texts that you want to display
Use a DataSource: fill it with the Items that you want to be selectable, and tell the ComboBox which property of the selectable items you want to display.
Use the first method if you only want to display a constant array of strings. Fill ComboBox.Items. When an item is selected, use SelectedIndex to get the index of the selected string. Use ComboBox.Items[selectedIndex] to get the selected string.
If the string represents something more than just a string, for instance the text represents a Customer, or a Product. It is usually easier to use the DataSource method.
To do that, you use property ComboBox.DataSource to tell the ComboBox which Customersit should display. In ComboBox.ValueMember you tell the ComboBox which Customer poperty should be used to represent the Customer, for instance the name of the Customer.
Once the operator selected the name of the Customer, you get the complete Selected Customer using property ComboBox.SelectedItem:
List<Customer> availableCustomers = ...
ComboBox combo1 = new ComboBox(...);
combo1.ValueMember = nameof(Customer.Name); // the Customer property that you want to display
combo1.DataSource = availableCustomers;
After the operator selected an item, you can process the event, and fetch the selected customer immediately:
Customer selectedCustomer = (Customer)cmbo1.SelectedValue;
ProcessSelectedCustomer(selectedCustomer);
Of course you should only select a property that is unique. If you have two Customers named "Hans Brinker", operators wouldn't know which name represents which Customer.
Apart from the nice thing that you don't have to do a lookup from SelectedIndex to what this selected item represents (a Customer), you are independent of the order in which the Customers are displayed.
Another nice thing: if in future versions you want to change from ComboBox to ListBox, or maybe even to DataGridView, you won't have to change your model drastically: the control still shows a sequence of Customers, and once an operator selects something that represents this Customer (Name? Id?, DataGridView Row?), you get the complete Customer.
(1)ComboBox1.SelectedIndex starts from -1, when you are not selected, ComboBox1.SelectedIndex=-1
(2)When you click combobox1.items, SelectedText is always "".
This is because, at the time of these events(SelectedIndexChanged, SelectedValueChanged.. ), the previous SelectedText value has been cleared and the new value has not yet been set. You can use the SelectedItem property instead.
E.g:combobox1.SelectedItem
Please try the following code.
If ComboBox1.SelectedIndex = -1 Then
MessageBox.Show("No value selected")
Else
MessageBox.Show("Some value selected")
End If
I am trying to get the selected text in a ListBox to show in a list box.
I have a button that when I click, will show the text of a Selected item in a ListBox.
I've already tried getting the list box to show the text in a textbox using Listbox.SelectedItem:
Listbox1.SelectedItem = Textbox1.Text
When I do this, I get the error:
System.NullReferenceException: 'Object variable or With block variable
not set.'
Your description is somewhat upside-down.
It's not clear whether you want to set a ListBox.SelectedItem using the Text of a TextBox or you want to set a TextBox.Text with the text of the SelectedItem of a ListBox.
The code says one thing, the description another. The error you have is probably caused by a null SelectedItem (you haven't selected anything).
But, if you want to set a TextBox.Text with the ListBox.SelectedItem text, you can use the GetItemText() method. This method has a plus, it won't raise an exception if the Listbox has no selected Items (the SelectedItem is null (nothing)).
TextBox1.Text = ListBox1.GetItemText(ListBox1.SelectedItem)
The opposite:
You can use the ListBox FindString() and FindStringExact() methods to locate an item in the control's collection corresponding to a given string.
The former finds the first items in the ListBox that starts with the specified string, the latter matches only the whole string.
The search can begin from a specific index. It's not case sensitive.
listBox1.SelectedIndex = listBox1.FindString(textBox1.Text, 0)
' or
listBox1.SelectedIndex = listBox1.FindStringExact(textBox1.Text, 0)
You can continue the search specifying, as the starting point, the index of the item previously found:
private int lboxSearchIndex = -1;
Private Sub ButtonSearch_Click(sender As Object, e As EventArgs) Handles ButtonSearch.Click
lboxSearchIndex = listBox1.FindString(textBox1.Text, lboxSearchIndex)
listBox1.SelectedIndex = lboxSearchIndex
End Sub
Setting the SelectedItem is like using the FindStringExact() method.
Only a full match will set the item, but it's case sensitive.
Listbox1.Text=textbox1.text
Let me know its help you
I am new to this programming method. The case is I have created the checkbox and textbox dynamically where the name of the text box and check box is equal and it is handled using database.
The data I need to get is from the textbox which is selected using checkbox by the user.
The details in the array need to be name of the textbox and the value of the textbox
I created the checkbox and textbox using this method. Some thing like this
Dim checkBox = new CheckBox()
Form1.Controls.Add(checkBox)
checkBox.Location = New Point(offset, 10)
checkBox.Text = cur
checkBox.Checked = True
checkBox.Size = New Size(100, 20)
I successfully created checkbox at run time. Help me getting the value, and suggest me with the suitable array list.
the naming for the check box is number 1 to n values same for the textbox
I can not locate the correct method to make the first item in a combo box visible.
The app starts with an empty combo box. The user makes a radio box selection then clicks Go! (how original). The combo box is loaded via an LDAP query. All this is working just fine. The problem is the combo box still appears to the user to be empty. They must click the arrow to see the options.
How do I make the first option 'visible' after the users clicks Go!?
' Your code filling the combobox '
...
If myComboBox.Items.Count > 0 Then
myComboBox.SelectedIndex = 0 ' The first item has index 0 '
End If
Just go to the combo box properties - DropDownStyle and change it to "DropDownList"
This will make visible the first item.
OR
you can write this down in your program
Private Sub ComboBoxExp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles MyBase.Load
AlarmHourSelect.Text = "YOUR DEFAULT VALUE"
AlarmMinuteSelect.Text = "YOUR DEFAULT VALUE"
End Sub
so when you start your program, the first thing it would do is set it on your assigned default value and later you can easily select your required option from the drop down list.
also keeping the DropDownStyle to DropDownList would make it look more cooler.
-Starkternate
because you have set index is 0 it shows always 1st value from combobox as input.
Try this :
With Me.ComboBox1
.DropDownStyle = ComboBoxStyle.DropDown
.Text = " "
End With
You can try this:
Me.cbo1.Text = Me.Cbo1.Items(0).Tostring
If ComboBox1.SelectedIndex = -1 Then
ComboBox1.SelectedIndex = 0
End If
Much simpler solution, Select the Combo-box, and in the option of Selected item, select the item index (0 for the first item) and set it to be the default value in the combo box.
Another good method for setting a DropDownList style combobox:
Combox1.SelectedIndex = Combox1.FindStringExact("test1")