What is the use of RefreshItems() from Listbox in c# - c#

In winforms c#, there is a control named ListBox. It has a method RefreshItems()
What is the real use of this method ? can someone explain with example and sample code ?

According to the documentation it does the following:
Refreshes all ListBox items and retrieves new strings for them.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox.refreshitems?view=windowsdesktop-6.0
Which is a bit vague.
If we look at the RefreshItem(Int32) method, it has a remark which clears things up a bit
If the DisplayMember property is set and the property in the data source that is assigned to DisplayMember changes, use the RefreshItem method to update the value in the ListBox control.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox.refreshitems?view=windowsdesktop-6.0
This suggests that if you've set a display to an item, and update the display string, you want to call the RefreshItem so that the UI will show the correct value.
RefreshItems is the same, but for all items.

Related

WinRT XAML ComboBox opens in middle of list

So one particular behavior of the XAML combobox in WinRT is causing me a huge headache, because my client sees it as a defect, and doesn't care if it's the behavior of the control, he wants it changed. However, I cannot find anything that tells how to change it. The behavior I'm speaking of is that when nothing is selected the ComboBox popup opens displaying the ItemsSource in the middle of the list. I have a sorted list of countries, with the exception of US, UK, CAN being at the top. These 3 items are the most often selected items and the client wants them on top rather than having to scroll through the list to find them. That's easy enough, but because the list opens in the middle, you still have to scroll quite a bit to get to them. Is there some property I'm missing that turns this behavior off? I was able to finally convince them that the CarouselPanel wasn't a defect, but this one isn't going to fly.
Thanks in advance!
UPDATE:
So this combobox is databound through a ViewModel. in this instance, the ViewModel has no value (it is an empty string) for that particular property and so the Combobox shows empty, which is fine and desirable. When you click on the Combobox to select a value, it displays the list in the middle of the available values. this is the behavior that is undesirable. it should be showing the 1st value in the list at the top!
Well, one would think that the out of the box Combobox (there is no other built in dropdown control) would be able to work like any other combobox control in any other MS technology to date, but of course this is MS, so why should things be consistent. At any rate, I ended up having to create a "blank" entry and pre-select that item if the value in the VM is empty, and then write code in the setter of that property to ignore if "blank" item if it is selected. It's kludgy and wreaks of code smell, but it works
When you set the SelectedItem property to an object, the ComboBox attempts to make that object the currently selected one in the list. If the object is found in the list, it is displayed in the edit portion of the ComboBox and the SelectedIndex property is set to the corresponding index. If the object does not exist in the list, the SelectedIndex property is left at its current value.

Binding controls to a collection of objects

I need help with data binding. Imagine this situation. I have two classes, one named Isotope, another named Photon. Class Isotope contains BindingList Photons. I also have a static class StaticVariables, where I put BindingList Isotopes. Now, I want to make a form which will allow me to browse the list of isotopes. I created a combobox CBIsotopes, that I bound to StaticVariables.Isotopes:
CBIsotope.DataSource = StaticVariables.Isotopes;
CBIsotope.ValueMember = "IsotopeName";
CBIsotope.DisplayMember = "IsotopeName";
So far, everything works. Now I want to create a datagridview DGVPhotons that will show all the photons of the selected isotope. My first instinct was to do something along the way of
DGVPhotons.DataSource = StaticVariables.ListOfIsotopes.Photons
which of course, doesn't work. Another thing I tried is to use SelectedItem property of the ComboBox:
(1)
DGVPhotons.DataSource = (CBIsotope.SelectedItem as Isotope).Photons;
This works, but not as well as I would like. If I do it on load time, nothing happens, because ComboBox is empty. If I do it when an item is actually selected in ComboBox, then it works, but as I change the selection in ComboBox, DataGridView stays the same. The solution would be to put line (1) in SelectedIndexChanged of the ComboBox, but it seems like a brute force method to me, and I feel that my approach is fundamentally wrong... Is there some more elegant solution?
Ok, the key here is to use DataBind solution. After you change source of your element don't forget to use DataBind method after, in order to bind new data.
And also, on PageLoad event, don't forget to use IsPostBack sign in order to initialize page only when request is handled for the first time.

Why we use Bound field in GridViews

I am pretty shocked no one asked this question before as when I searched I couldn't anything related,
https://stackoverflow.com/search?q=what+is+a+bound+field+c%23
Anyway my question is,
Can someone explain what a bound field is please in easy words and when we use it with example.
Research I did
We use it in a GridView or DataView but why we can't use the default option for displaying data then using Bound Fields.
Well Data Binding in general is the principle of declaratively stating that some user interface element's value will come from some source, and be populated by the runtime rather than the developer manually setting and getting values from controls in codebehind files.
So in WPF, for example, you can set the DataContext property of an entire window to an object, and then for each control on that window say from which properties of that object the WPF runtime should get their value.
For example, for an Employee viewmodel with Forename and Surname properties, you might create an EmployeeView window with two textboxes, where one is "bound" to the Forename property and the other is "bound" to the Surname property. At runtime, the framework will look at the bindings on each control, fetch the value from the data automatically and populate the control's value field. Likewise, when the value in the control is modified by the user, data-binding can push the new value to the data model it is bound to.
This is in contrast to the typical approach in the days of VB6, where setting those textboxes' content would be done in the codebehind of the form (e.g. forenameTextBox.Text = employee.Forename). Data binding in VB6 (and WinForms, for that matter) is different, where the framework does what I described above, but automates getting data from a database in the process. That's fallen out of favour in recent years, though (and for good reason).
The BoundField class is used by data-bound controls (such as GridView
and DetailsView) to display the value of a field as text. The
BoundField object is displayed differently depending on the data-bound
control in which it is used. For example, the GridView control
displays a BoundField object as a column, while the DetailsView
control displays it as a row.
For more visit MSDN Help Bound Field Description

C#(Winforms) Databinding using Listbox.SelectedValue as data source (master-detail)

I have a listbox bound to a List<object> as its DataSource. What I'm wanting to do is use the SelectedValue property of the Listbox (i.e the object corresponding to the current selection) as a DataSource for some textboxes that will display certain values of the object for editing.
I've tried
TextBox.DataBindings.Add(new Binding("Text", ListBox, "SelectedValue.name"));
and
TextBox.DataBindings.Add(new Binding("Text", ListBox.SelectedValue, "name"));
but as there is nothing selected in the ListBox (because the form hasn't been shown yet), I get an exception about "Value cannot be null".
Now I know that I can (re)bind to ListBox.SelectedValue in my form's SelectionChangeCommitted handler (that is, after a selection has been made), but if i have to do that I might as well just set the TextBox's value directly (admittedly I could just do this to resolve the issue, but I'd like to learn more about databinding).
So my question is, in short: Is it possible to bind to ListBox.SelectedValue once (initially, before the ListBox has a selection) and avoid the null value exception, and if so, how?
I'm not sure which control your projectNameCtrl is, but you'll want to bind your TextBox. Something like this:
textBox1.DataBindings.Add(new Binding("Text", listBox1, "selectedvalue"));
Where:
textBox1 is your TextBox
listBox1 is your ListView
EDIT
You should be able to data bind a ListBox even if that ListBox has no selected items so your 'value cannot be null' must be for another reason. I suggest using the debugger to determine which object specifically is null.
You can ensure you don't data bind a control more than once by first checking the control's DataBindings.Count property; if it's equal to zero you haven't yet data bound that control:
if (textBox1.DataBindings.Count == 0) {
// OK to data bind textBox1.
}
Off the top of my head, I think you'd need to do something on each selectedItemChanged event...
I know this doesn't answer your question, but I'd look at using WPF instead since this is so much more elegant to do in WPF, and let's face it, by not creating a GUI in code (using XAML instead) your sanity will be much more intact when you finish your project. I don't recall enough windows forms, but in WPF, you just implement INotifyPropertyChanged on your back-end object that you're binding to, and then when you bind to the SelectedItem property of that ListBox, you automatically get updates since the SelectedItem property is a DependencyProperty.

Refreshing WPF ComboBox

I have ComboBox that lists all enum values using ObjectDataProvider. Since enum-values are not always that clear and they need to be localized, I use converter to do that. This all works fine, but when I change the language, I have to somehow update the combobox. How I can do that? ComboBox'es UpdateLayout() and Items.Refresh() have no effect at all.
Try calling Refresh on the ObjectDataProvider itself.

Categories

Resources