VB.net: How do I "refresh" my comboBox? - vb.net

I have a combobox that is populated by a database table I have. In my Load event for my form I have it populated by...
Me.BusinessTableAdapter.Fill(Me.BillingdbDataSet.Business)
My problem is whenever I "add", "edit", or "delete" a business I have stored I can't find a way to update the combobox I have. Whenever I exit and reopen my program and I can see the changes but was hoping someone could inform me on how to have it updated after a change has happened.

The problem is you need something that implements INotifyCollectionChanged.
MSDN
You can enumerate over any collection
that implements the IEnumerable
interface. However, to set up dynamic
bindings so that insertions or
deletions in the collection update the
UI automatically, the collection must
implement the INotifyCollectionChanged
interface. This interface exposes the
CollectionChanged event, an event that
should be raised whenever the
underlying collection changes.
You can refresh it manually, but the ideal situation is the bind to an observable collection class - http://msdn.microsoft.com/en-us/library/ms668604.aspx HTH
Wade

If you're using datasets you can use the dataview class which raises events when the underlying dataset is changed. Then you could either look into data binding them to the combo-box or write your own update code and put that in an event handler for the event raised when the list changes.

Related

Visual Basic 2010 - Referencing objects in different user controls

For an app that uses user controls instead of forms and the first user control has a listview, where the user clicks or selects "Create New" Or Delete, what is the best way to transfer the data selected in the listview to the detail screen (separate User Control) where the data can be edited?
Can I just reference the list view in the first UC in the Details UC? something like:
ucHeader.lvSetups.FocusedItem.SubItems.Count = 0
from the ucDetail user control?
Saying which way is the best would produce a heated discussion with every ones opinion. However, here are a few ways I would tackle this. While there are more options, these are what I would do:
You should expose any information you want to read from a user control in the form of a property, readonly if you must. Just an example because I don't know what your object types are:
Public ReadOnly Property SelectedItem as object
get
Return Listview1.SelectedValue
end get
End Property
You can also use events to tell the parent of your user control that a selection was made. You can pass whatever you want in this event, even the selected object. If you don't want to pass the selected object, grab it from the property you created (like #1) in the event handler.

event for combobox databinding complete?

I have a combobox on a winform, and need to be able to call a method when the combobox's databinding is complete. I am surprised to see that there is no DataBindingComplete event for the ComboBox control, like there is for the DataGridView.
How can I know when data binding is complete? Is databinding done synchronously for comboboxes? (i.e., if I have the following code, is it guaranteed that data binding will be complete when it hits the second line of code?)
myComboBox.DataSource = foo
SomeMethod()
You could make a class where you handle all the data access, then as the data is retrieved you can raise an event from that layer to the calling form you use to display your data.
Data Access Layer

How do I create a "wizard" style ui using vb.net?

The idea is that I would have a set of forms, users would click through a "forward" and "back" button, and the current form would change to a different one. My issue is that I can write code that just pops up a new form, but im not sure how to do a "replacement" of my current form. How is this usually done?
What I did recently was to create a form with buttons already in place and a large panel to contain each step. The dialog would accept an initial step in the form of a IWizStep instance, and the things would roll from there.
Each step was a class exposing a UserControl responsible for the visual aspect of the step, while the logic itself was handled by the class (it was a little more complicated that that, but that was the general idea).
The IWizStep interface, implemented by the step and accepted by the dialog, was on the lines of:
Interface IWizStep
Event StateChanged As EventHandler
ReadOnly Property Control As Control
ReadOnly Property Title As String
ReadOnly Property CanMovePrevious As Boolean
ReadOnly Property CanMoveNext As Boolean
Function MovePrevious As IWizStep
Function MoveNext As IWizStep
End Interface
To put everything together, a controller class would know how to compose the steps necessary for each given action. Therefore I had a controller for, say, "Emit Order", which needed some 10 steps, and a controller for "Emit Orders in Batch", which needed only a couple of steps.
Create a set of UserControls, and add and remove them from a Panel in a single form. (and set Dock to Fill)
You could define a user control which acts as a "wizard". It just needs the buttons you have and an array of content panels, just have it switch through the panels when the buttons are pressed assuming a certain condition is met within the controls on the panel. There's no real definitive "wizard" maker, since it's pretty easy to roll your own wizard.
You don't need to do a "replacement" of your current form really, you could just add a new one to the project. If you do need to for whatever reason, just grab the control collection with Me.Controls, copy that somewhere, and put the new controls up. When you don't need the wizard, swap them out again. It's generally best practice to make a new form however!

Deleting bound item from DataGridView

I have a List of an object, set to DataSource of a BindingSource, set to DataSource of a DataGridView. Then I added a DataGridViewButtonColumn which I want a click to remove that record from the collection. So I simply call collection.RemoveAt(e.rowIndex) from the DataGridView CellClick event. However this produces an IndexOutOfRange Exception; "1 does not have a value"..
So is happening here? I have maybe a vague idea, but would like to understand exactly which events are failing etc.. and what do i need to do to perform this action properly?
EDIT:
This seems not to happen if I use a BindingList.. But when using a List, I get this problem..
The reason you're seeing the error is because you're ultimately binding to a List. List doesn't include the notifications of changes that your DataGridView needs to receive to reflect the changes you make.
If you really want to work around this you could do the following: just before you make a changes to your List, set the BindingSource's DataSource property to Nothing. When you're done making changes reset your List to the DataSource.
This is a pretty ugly solution as you can imagine. What you'll want to do this bind to an object that implements IBindingList, either a custom collection you create or a BindingList.
Here's a reasonable starting point for investigating this further:
DataGridView Control Overview (Windows Forms)

PropertyChanged Event, raise when OnClick?

I have a WinForms application with some business objects which implement INotifyPropertyChanged and hook the PropertyChanged event via some controls & BindingSource on the form which raises an event back to my business objects layer...
There's just one issue - everything works fine, except only when the control loses focus. E.G., the user changes some text field, but he has to actually click on some other control somewhere BEFORE he hits the 'save' button (e.g. he has to cause focus on another control).
Is there a way to wire this up to OnClick or something so the change is propogated to my business objects layer as the user types the text (or even after he changed a drop down value) so I don't have to force the user to click on a different text box before he can save?
Googling has lead me nowhere but to examples on how to easily do this in WPF =(. But no WinForms examples...
Please Advise,
Drew
If you are using Binding class to connect object to UserControl, you may try to set binding's DataSourceUpdateMode to OnPropertyChanged.
Please, note that in this way you are writing data to your object before validation. This may be unsuitable for some tasks.