I am a fairly new to visual studio. I have 2 forms with dropdown cb_CBOX1 & dropdown list cb_CBOX2.
I want user to add data to cb_CBOX1 and have the data be inserted in alphabetical order in cb_CBOX1 dropdown and be inserted in alphabetical order in cb_CBOX2 dropdown list also.
I'm trying to use the below statement. TIA
cb_CBOX2.Items.Add(cb_CBOX1.Text)
Update 2017-11-07:
I have 2 forms both have combobox with drop down list. I want user to insert data to cb_CBOX1 and have the data added alphabetically to the cb_CBOX1 drop down and added alphabetically to cb_CBOX2 drop down. When the user types in data in cb_CBOX1, they will click button1 to call the Add function.
My code:
Private Sub button1.Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
cb_CBOX2.Items.Add (cb_CBOX1.Text)
End Sub
Extract the items, add the new item, sort, then apply the items back. It's fairly simple with Linq.
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
addToAndSortComboBox(cb_CBOX1, cb_CBOX1.Text)
addToAndSortComboBox(cb_CBOX2, cb_CBOX1.Text)
cb_CBOX1.Text = "" ' optional
End Sub
Private Sub addToAndSortComboBox(cb As ComboBox, value As String)
Dim items = cb.Items.Cast(Of String).Concat({value}).OrderBy(Function(v) v).ToList()
cb.Items.Clear()
cb.Items.AddRange(items.ToArray())
End Sub
Just a comment on the design: it doesn't seem very intuitive. I feel like the input should be done in a TextBox with the ComboBoxes having ComboBox.DropDownStyle = DropDownList.
It sounds like you want to be able to add data to a list, Whatever the user types into combobox1, will appear on combobox2 on a different window?
I'd suggest binding your combo boxes to a list, and then setting the item-sources of both combo-boxes to this list.
Create the list, and reference it in a class so you can access it from a different window...
Dim comboboxitemlist As New List(Of String)
Bind your list to CB2...
cb_CBOX2.itemssource = comboboxitemlist
In a button or however you are adding...
comboboxitemlist.add(cb_cbox1.text)
Related
I am generating Auto Serial number in DataGridView using below code:
Public Class Form1
Dim table As New DataTable()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
table.Columns.Add("Sl", Type.GetType("System.Int32"))
table.Columns.Add("Id", Type.GetType("System.Int32"))
table.Columns.Add("Name", Type.GetType("System.String"))
table.Columns.Add("Amount", Type.GetType("System.Int32"))
DataGridView1.DataSource = table
DataGridView1.Columns(0).ReadOnly = True
DataGridView1.Columns(2).ReadOnly = True
End Sub
Private Sub DgvRowCountChanged()
For Each dgvr As DataGridViewRow In Me.DataGridView1.Rows
dgvr.Cells(0).Value = dgvr.Index + 1
Next
End Sub
Private Sub DataGridView1_RowsAdded(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
Me.DgvRowCountChanged()
End Sub
Private Sub DataGridView1_RowsRemoved(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowsRemovedEventArgs) Handles DataGridView1.RowsRemoved
Me.DgvRowCountChanged()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim Index As Integer
If DataGridView1.RowCount > 1 Then
Index = DataGridView1.CurrentCell.RowIndex
DataGridView1.Rows.RemoveAt(Index)
End If
End Sub
End Class
Serial number appears, but when I click on next column it disappears. Why is that happening?
Do this, make life easy:
Ditch all that code
Make a new form
Add a new item of type DataSet to your project, and give it a nice name (not DataSet1)
Double click the dataset, so its design surface appears
Right click the surface, choose "Add new.. DataTable", give it a more imaginative name than DataTable1. I'll assume you choose License (serial number? name? seems licensey)
Right click the new datatable and choose "Add New.. Column"
Name the new column Sl, and use the properties grid to give it a type of System.Int32
Repeat for your other columns
Save
Open the Data Sources window on the View menu (Other Windows item)
Open the new blank form you made in step 2
Expand every node you can see in the Data Sources window
Drag the node representing your datatable (the one with an icon looking like a datagridview next toit) out of the data sources window and drop it on the form
Remove (delete) the bindingnavigator it created (you wont need it)
CLick the datagridview, CLick the small arrow that appeared in the top right of the control to show the popup menu, choose Edit Columns
Make whatever columns you want read only
Set other properties like sizes of columns, fill weights, header texts etc
Open the code of your form. add a single row of data to the datatable, in the constructor, after the initializecomponent() call:
Me.myImaginativeDataSetName.License.AddLicenseRow(1, 1, "Name Blah", 1234)
That's it. It looks like a lot because I've broken it down into the absolute step by step - about the only thing that isn't there is reminding you to take a breath every few steps because you'll be so blown away how easy it makes your life when you get the IDE to write code for you, ;)
OK, it's maybe not that exciting... But you already use the Forms designer to write reams of code for you, so this is how you leverage the other tools so you don't have to work with un-typed datasets all the time. Ugh.
The dataset this creates has a full suite of nicely named properties; don't use the basic stringy stuff ever again:
'yes - do this
For Each ro as LicenseRow in myDataSet.License
If ro.IsNameNull Then ro.Name = "Default Name" & ro.Sl
Next ro
'no - heck no
For Each ro as DataRow in myDataSet.Tables("License").Rows
If ro.IsNull("Naem") Then ro.item("Name") = "Default Name" & Convert.ToInt32(ro.Item("Sl"))
Next ro
See how much cleaner the first one is? ro.Sl is a nice Integer property, no casting or converting, no incessant Tables this or Columns/Rows that, Intellisense helps you out becaise it's all strongly named stuff, no typos in string column names like I made in the second...
It looks like youre trying to prevent the user from adding rows with this:
If DataGridView1.RowCount > 1 Then
Index = DataGridView1.CurrentCell.RowIndex
DataGridView1.Rows.RemoveAt(Index)
End If
End Sub
If so, click the datagridview on the form designer and in the properties grid set AllowUserToAddRows to false. If youre also trying to prevent deletion set the same on AllowUserToDeleteRows
I am working on a new application and am trying to work with a Listbox. What I am trying to do is run a task with each item in the Listbox one at a time.
Example:
Items in Listbox:
Dog
Cat
Fish
Cow
For this example, I would like the program to start at the top at 'Dog', display the result, then go to 'Cat', and so on until there are no more items.
Side note: I would also like it if there was a way to highlight what item it was currently on.
Thanks in advance guys, I am working on learning some of the other features of the ListBox with this new application.
The ListBox has some properties you may be interested in.
You want to highlight the item. Therefore, you'll need the SelectedIndex
Since you want to select it, a standard For loop will be needed.
To access the items in the list, you use the Items collection.
The Items collection has a Count property.
Here is an example based on what you provided. AnimalList is a ListBox, and ShowNames is a Button:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AnimalList.Items.AddRange({"Dog", "Cat", "Fish", "Cow"})
End Sub
Private Sub ShowNames_Click(sender As Object, e As EventArgs) Handles ShowNames.Click
For i As Integer = 0 To AnimalList.Items.Count - 1
AnimalList.SelectedIndex = i
MessageBox.Show(AnimalList.Items(i).ToString())
Next
End Sub
If you set the SelectedIndex, you can also access the SelectedItem property.
When you have a multi-select list, there are also SelectedIndices and SelectedItems properties.
What I Wish to achieve is to extract the select item text and display it into a messagebox (for start, then i'm going to use it for a SQL Query...)
I want to extract that particular selected ITEM, for instance : "SPR (Suivi piece rechange)" in the following image :
I tried this, but when I click on "Menu", it returns the name of my menu strip "MenuStrip1" :
Private Sub MenuStrip1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuStrip1.Click
MessageBox.Show(DirectCast(sender, MenuStrip).Name)
End Sub
EDIT:
I forgot to mention that all the ITEMS are added dynamically from the database,
so there will be no predefined Private Sub....End Sub Procedure for these Items.
Thanks in advance.
The MenuStrip object only refers to the actual menu strip itself, not the individual menu items, which are actually ToolStripMenuItem objects. You're looking for the Text property of those objects. For example:
DirectCast(YourDynamicMenuItemObjectHere, ToolStripMenuItem).Text
If you're looking for a way to capture events, you'll need to create a generic event handler:
Private Sub GenericMenuItem_Click(sender As System.Object, e As System.EventArgs)
MessageBox.Show(DirectCast(sender, ToolStripMenuItem).Text)
'Whatever else you need to do based on the text of the menu item
End Sub
And then hook that handler to the menu items whenever they're created:
'Code that creates YourDynamicallyGeneratedMenuItem
AddHandler YourDynamicallyGeneratedMenuItem.Click, AddressOf GenericMenuItem_Click
This is a VB.NET winforms project, using EF. The ComboBox is bound to a seperate datasource which contains the colums tName and tNumber. TName is a brief description of that value and tNumber is the actual number that is saved in the Financial Table under column named transaction_type. Everything works flawlessly on the display of existing data that is in the Financial Table.
The comboboxes for each item in the grid all show the correct description for the transaction_type. But when ever I change the value of any of the combo boxes and click the save button it does not save any value to the transaction_type.
Any ideas why this might be? Any missing code I will add if required..
The form_load event looks like this:
Private Sub paymentHistoryView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FinancialDataGridView.DataSource = db.financials.Where(Function(f) f.TenantId = tenentId).OrderBy(Function(f) f.transaction_date)
TranstypeBindingSource.DataSource = db.transtypes.OrderBy(Function(f) f.tNumber)
BindingNavigatorDeleteItem.Enabled = False
End Sub
And the savebutton click event is as follows:
Private Sub FinancialBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles FinancialBindingNavigatorSaveItem.Click
db.SaveChanges()
End Sub
The properties for the ComboBox are shown Below:
It should be noted that all other changes to the datagrid are saved correctly when the save button is clicked... After further testing the value will actually save if the ComboBox is no longer selected. I guess a work around would be to focus on something else after the value of a comboBox is changed. If this seems like the best way how would I hook on SelectedIndexChanged event for comboBoxs in the datagridview???
I think that if you check, the same thing will actually happen with the other columns in the datagridview, if you do not move out of the cell before clicking the save button. The good news is that you only need to add one line of code.
FinancialDataGridView.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
Now your SaveItem_Click event handler should look like this:
Private Sub FinancialBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles FinancialBindingNavigatorSaveItem.Click
FinancialDataGridView.CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
db.SaveChanges()
End Sub
This will commit the dirty cell before the save.
See https://stackoverflow.com/a/6469559/269123
I want to adding checked item from checkedlistbox to my combobox, but i have a little problem here. Combobox only show 1 item last checked.
This is my sample code.
If CheckedListBox1.CheckedItems.Count <> 0 Then
For i As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
cbCheckedItem.Text = CheckedListBox1.CheckedItems(i).ToString
Next i
End If
anyone can help me show all checked item??
thank's for your help...
You aren't adding the items to the combo box, you're only setting its Text property. That's only changing the text currently displayed in the combo box, and only one item can be displayed at a time.
If you want the items to be permanent and selectable, you need to add them to the combo box control's Items collection.
Sample code:
If CheckedListBox1.CheckedItems.Count > 0 Then
For Each checkedItem In CheckedListBox1.CheckedItems
cbCheckedItem.Items.Add(checkedItem.ToString())
Next
End If
Or, better yet, use the AddRange method:
If CheckedListBox1.CheckedItems.Count > 0 Then
Dim checkedItems() As String = CheckedListBox1.CheckedItems.Cast(Of String).ToArray()
cbCheckedItems.Items.AddRange(checkedItems)
End If
Oddly enough the CheckedListBox has a CheckedItems property, which is a collection. As such you can loop through it like you can any other collection, using a For or For Each loop.
then, Each item needs to be added to the Items collection of the ComboBox.
like this sample:
Public Class frmCheckedListBox
Private Sub frmCheckedListBox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CheckedListBox1.Items.Clear()
Me.CheckedListBox1.BeginUpdate()
Me.CheckedListBox1.Items.Add("One")
Me.CheckedListBox1.Items.Add("Two")
Me.CheckedListBox1.Items.Add("Three")
Me.CheckedListBox1.Items.Add("Four")
Me.CheckedListBox1.Items.Add("Five")
Me.CheckedListBox1.EndUpdate()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each Item As String In Me.CheckedListBox1.CheckedItems
Me.ComboBox1.Items.Add(Item)
Me.ComboBox1.SelectedIndex = 0
Next
End Sub
End Class
As sample code shows, the CheckedItems collection contains the items that are checked, just as the name suggests. It doesn't contain a Boolean value for each an every item to indicate whether it is checked or not. If an item is checked then that item is in the CheckedItems, and if it isn't then it isn't. You simply need to loop through the collection and get every item in it, because it contains all the items that are checked and none that aren't.
in the end you can put :
Me.Combobox1.items.clear()
because when it would click with sample code it would have the one that clicked then on the next click would return the previous one it had clicked and then the new one all compiled in the combobox selection menu
perhaps my answer can help you solve your problems
Combobox doesn't have a multiselect option. so only one item at a time could be selected.