Showing posts with label Winforms. Show all posts
Showing posts with label Winforms. Show all posts

Monday, 2 April 2007

Winforms datasource runtime change

ScottSimply put, i wanted to change the datasource of a binding source at runtime. The design behind this is a tab control where all tabs share a common control, therefore the common control needs to be context sensitive to the selected tab so as to display the correct data. Simple enough!!! So why does the code below through generic event handler exceptions???

_MyBindingSource.Clear() // Clears the binding source.
_MyBindingSource.DataSource = CurrentTabContextSensitiveData.
_MyBindingSource.RefreshBindings() // Common control to refresh the bound data.

OR

_MyBindingSource.DataSource = null;
_MyBindingSource.DataSource = CurrentTabContextSensitiveData.
_MyBindingSource.RefreshBindings() // Common control to refresh the bound data.

I dont know why these code samples cause generic exceptions!!

The code that does actually work is;
_MyBindingSource = new BindingSource();
_MyBindingSource.DataSource = CurrentTabContextSensitiveData.
_MyBindingSource.RefreshBindings() // Common control to refresh the bound data.

I hate it when a solutions is found but cant be fully explained or understood. Any ideas out there????

Monday, 2 October 2006

Radio Buttons / UltraOptionSet

ScottI am currently using data binding extensivly in a winforms application. This has proved to be very successful until radio buttons were required. The radio button control does have a databindings property but this means each radio button itself would have to be created and data bound to which is no good when the options are dynamic :(

Dynamic databound radio buttons were successfully acheived using the Infragistics UltraOptionSet. This control allows DisplayMember, ValueMember and DataBindings properties to be set and from this a dynamic number of correctly labelled radio buttons are created and on top of that they are all databound :)

Winforms DateTime Databinding

ScottWinforms and databinding has significantly improved in .NET 2.0 and a line of code such as - txtName.DataBindings.Add("Text", DataObject, "ClientName"); - just works with both data and nulls :)

However, the DateTimePicker and its ability to handle nulls is not quite a simple. To handle nulls with the DateTimePicker a few extra lines of code is needed.

The format property of the DateTimePicker control needs to be set. I have chosen Custom and as such the custom format is also set.
dtpAppointment.Format = DateTimePickerFormat.Custom;
dtpAppointment.CustomFormat = "dd/MMM/yyyy HH:mm";


This next line is really the most crucial. Notice the only difference with the simple single data binding line above is now there is a true paramater at the end. This is the formattingEnabled argument and without formattingEnabled set to true the handling of nulls just does not work!!!
dtpAppointment.DataBindings.Add("Value", DataObject, "AppointmentDate", true);

Happy binding :)

Friday, 18 August 2006

Winforms visibility and event firing

ScottI am currently working with a MDI application and at times MDI children forms toggle visibility for various reasons which i wont bore you with ;) With events being used quite extensively in the design i just wanted to share this little snippet ... when a winform has it's visible property set to false, the form events do not fire.

Winforms Combo Box and Sorted Property

Scott

Had a very strange one the other day ... the conclusion is the Sorted property of the forms.combobox control should never be true when the combo box is using a datasource.

The combo box was set up no different - Set the DataSource with a DataRow[], Set the DisplayMember and ValueMember. The DataRow[] was already in the desired Description ASC sort order;

IDDescription
302Alopecia
345Appetite Decreased
346Appetite Increased
303Behavioural Abnormality
304Bleeding
313Dystocia

So with the DisplayMember = Description column, the ValueMember = ID column and the Sorted property set to false.

As expected the SelectedValue returns the expected ID value.

However, with the DisplayMember = Description column, the ValueMember = ID column and the Sorted property set to true. The combo box data actually is;

IDDescription
302Alopecia
303Appetite Decreased
304Appetite Increased
305Behavioural Abnormality
306Bleeding
307Dystocia

So the ValueMember has begun from the first ID value and just incremented as each item was bound.

In this case the use of SelectedItem.ID returns the correct ID value.

So just my experience ... maybe obvious ... but i have not seen this documented anywhere and surely i cant be the first person to have experienced it ?!?!?!!?

Tuesday, 8 August 2006

System.Windows.Forms.Keys.Return & beep

ScottWhen using Keys.Return you will automatically be provided with the classic PC beep. If this is what you want then you are on a winner. However, if this is not what you want then this here is the gem to turn the beep off;
Within the KeyPress event, where e is KeyPressEventArgs.
e.Handled = true;