473,659 Members | 2,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ListBox - DataSource - SelectedValue problems

Hi

NET1.1 / Winforms

I have a listbox that I am binding to a table via the DataSource property.
However I want to be able to programmaticall y select values in this listbox
so I am using the code below...

With lstJobTypes
.DataSource = BusinessRules.C ommon.JobTypes. All(True)
.DisplayMember = "Descriptio n"
.ValueMember = "JobTypeID"
End With

If Not _DefaultJobType s Is Nothing Then
For Each j As PlasmaCommon.Jo bType In m_DefaultJobTyp es
lstJobTypes.Sel ectedValue = CInt(j)
Next
End If

If I step through using the debugger, I can see the
lstJobTypes.Sel ectedItems.Coun t property increasing everytime I set the
SelectedValue and all seems good.

However when the form is then displayed the Count property becomes 1 and
only the first item in the listbox (standard functionality) is selected??!?!?!

The listbox selection mode is set to MultiExtended.

This also happens if I use the SetSelected method rather than the
SelectedValue method.

Any help is greatly appreciated as I am pulling my hair out here :(

Jul 17 '07 #1
2 5533
On Tue, 17 Jul 2007 14:48:03 +0200, Stephen Ritchie <St************ @discussions.mi crosoft.comwrot e:
Hi

NET1.1 / Winforms

I have a listbox that I am binding to a table via the DataSource property.
However I want to be able to programmaticall y select values in this listbox
so I am using the code below...

With lstJobTypes
.DataSource = BusinessRules.C ommon.JobTypes. All(True)
.DisplayMember = "Descriptio n"
.ValueMember = "JobTypeID"
End With

If Not _DefaultJobType s Is Nothing Then
For Each j As PlasmaCommon.Jo bType In m_DefaultJobTyp es
lstJobTypes.Sel ectedValue = CInt(j)
Next
End If

If I step through using the debugger, I can see the
lstJobTypes.Sel ectedItems.Coun t property increasing everytime I set the
SelectedValue and all seems good.

However when the form is then displayed the Count property becomes 1 and
only the first item in the listbox (standard functionality) is selected??!?!?!

The listbox selection mode is set to MultiExtended.

This also happens if I use the SetSelected method rather than the
SelectedValue method.

Any help is greatly appreciated as I am pulling my hair out here :(

Hi Stephen,

I did a test and could not immediatly reproduce your problem (C# code added below), but then I noticed "... when the form is displayed". Are you by chance running this code in the constructor? DataBindings can be tricky and some do not perform well at all if you manipulate them before the controls have been created.

If you are running the code in the constructor, try moving it to the load event instead. (I did get the same problem if I ran the code from the constructor, but it worked fine from the load event or using a button totrigger it).

protected override void OnLoad(EventArg s e)
{
base.OnLoad (e);
button1_Click(t his, null);
}

int[] def = new int[]{1, 3, 5};
private void button1_Click(o bject sender, System.EventArg s e)
{
ArrayList list =new ArrayList();
list.Add(new MyObj("Zero", 0));
list.Add(new MyObj("One", 1));
list.Add(new MyObj("Two", 2));
list.Add(new MyObj("Three", 3));
list.Add(new MyObj("Four", 4));
list.Add(new MyObj("Five", 5));
list.Add(new MyObj("Six", 6));
listBox1.DataSo urce = list;
listBox1.Displa yMember = "Name";
listBox1.ValueM ember = "ID";

listBox1.SetSel ected(0, false); // to prevent the default item selected
foreach(int i in def)
{
listBox1.Select edValue = i;
}
}

class MyObj
{
private string _Name;
private int _ID;
public string Name
{
get{return _Name;}
set{_Name = value;}
}
public int ID
{
get{return _ID;}
set{_ID = value;}
}
public MyObj(string name, int id)
{
this.Name = name;
this.ID = id;
}
}
--
Happy coding!
Morten Wennevik [C# MVP]
Jul 18 '07 #2
Hi Morten

The code does already run within the form load event. If I move the code out
to a standard windows form it works okay, whereas within my application the
listbox (which is the standard windows control) is contained within a user
control, which is contained in another user control that sits on a
derived/inherited form, so I think this layering is having an effect :(

Subsequently the customer has now decided they want it to be a checked
listview, and this works perfectly well :)

This problem has been filed under "just one of them things" :)

"Morten Wennevik [C# MVP]" wrote:
On Tue, 17 Jul 2007 14:48:03 +0200, Stephen Ritchie <St************ @discussions.mi crosoft.comwrot e:
Hi

NET1.1 / Winforms

I have a listbox that I am binding to a table via the DataSource property.
However I want to be able to programmaticall y select values in this listbox
so I am using the code below...

With lstJobTypes
.DataSource = BusinessRules.C ommon.JobTypes. All(True)
.DisplayMember = "Descriptio n"
.ValueMember = "JobTypeID"
End With

If Not _DefaultJobType s Is Nothing Then
For Each j As PlasmaCommon.Jo bType In m_DefaultJobTyp es
lstJobTypes.Sel ectedValue = CInt(j)
Next
End If

If I step through using the debugger, I can see the
lstJobTypes.Sel ectedItems.Coun t property increasing everytime I set the
SelectedValue and all seems good.

However when the form is then displayed the Count property becomes 1 and
only the first item in the listbox (standard functionality) is selected??!?!?!

The listbox selection mode is set to MultiExtended.

This also happens if I use the SetSelected method rather than the
SelectedValue method.

Any help is greatly appreciated as I am pulling my hair out here :(

Hi Stephen,

I did a test and could not immediatly reproduce your problem (C# code added below), but then I noticed "... when the form is displayed". Are you by chance running this code in the constructor? DataBindings can be tricky and some do not perform well at all if you manipulate them before the controls have been created.

If you are running the code in the constructor, try moving it to the load event instead. (I did get the same problem if I ran the code from the constructor, but it worked fine from the load event or using a button to trigger it).

protected override void OnLoad(EventArg s e)
{
base.OnLoad (e);
button1_Click(t his, null);
}

int[] def = new int[]{1, 3, 5};
private void button1_Click(o bject sender, System.EventArg s e)
{
ArrayList list =new ArrayList();
list.Add(new MyObj("Zero", 0));
list.Add(new MyObj("One", 1));
list.Add(new MyObj("Two", 2));
list.Add(new MyObj("Three", 3));
list.Add(new MyObj("Four", 4));
list.Add(new MyObj("Five", 5));
list.Add(new MyObj("Six", 6));
listBox1.DataSo urce = list;
listBox1.Displa yMember = "Name";
listBox1.ValueM ember = "ID";

listBox1.SetSel ected(0, false); // to prevent the default item selected
foreach(int i in def)
{
listBox1.Select edValue = i;
}
}

class MyObj
{
private string _Name;
private int _ID;
public string Name
{
get{return _Name;}
set{_Name = value;}
}
public int ID
{
get{return _ID;}
set{_ID = value;}
}
public MyObj(string name, int id)
{
this.Name = name;
this.ID = id;
}
}
--
Happy coding!
Morten Wennevik [C# MVP]
Jul 20 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
15160
by: Irwin M. Fletcher | last post by:
ListBox DataSource DisplayMember Property Problems in C# I have a (single select) listbox with data and when I click on the list I can't get the right text selected. My listbox is setup using the DataSource property. I set my ValueMember and DisplayMember listBox1.ValueMember = "TypeID";
0
4608
by: Jesse Martinez | last post by:
My problem occurs when I use an ArrayList as a ListBox.DataSource. When the ArrayList attached to the Listbox is not empty the Listbox behave normal without problem, but if I remove all items from the ArrayList and re-attach the ArrayList to the ListBox the program hangs for about 3-5 seconds then keep running normal. I'm following the examples from the MSDN and dont see anything abnormal. This is a sample of the code I'm using: 1...
4
22216
by: kids_pro | last post by:
Hi, On form_load() I bind a combobox to a dataset and listbox to a datatable. When I want to unbind it I call comboBox1.DataSource = null, listBox1.DataSource = null It unbind but the Items still persist in the comboBox and listBox. I expect it to be empty list. Can anyone tell me how to unbind the comboBox and automatically clear it
1
1926
by: Zoury | last post by:
Hi there! :O) I have a WebService that contains the following struct definition. this struct is defined right after the WebService class, ie : //*** namespace mynamespace { public class MyWebService : WebService {// code here..} public struct Record {
4
4203
by: dtblankenship | last post by:
Hello everyone, I know this question has been asked many times in the forums, and after spending a few days reading, I am still confused as to the answer. I have a ListBox (lstBox), SqlConnection (sqlConnection), SqlDataAdapter (daLookupData), SqlDataAdapter (daData), DataSet (dsLookupData), and DataSet (dsData), all created via the IDE during design-time. Here is the design of my simple tables: (An example since I can't post my...
1
1415
by: COHENMARVIN | last post by:
I am trying to display the contents of a directory in a listbox. I manage to get an array of fileInfo objects, and when I print them out they indeed contain several file names. But when I do 'imgBanner.DataSource = fileInfos', I'm left with an empty listbox. Here is the code: ========================= Dim fileInfos() as FileInfo Dim fi as FileInfo myDir = New DirectoryInfo(MapPath("/vwSpecials"))
2
4585
by: Zorpiedoman | last post by:
Ok, I have a DataTable: ID Name -- ------------ 1 John 2 Jacob 3 Jingleheimer 4 Schmidt 5 John
1
5397
by: clickon | last post by:
Forget about the controlParameter for the moment, for testing purposes i have created the following Markup: <asp:Table ID="tblSelectRoute" runat="server" CssClass="asp-table"> <asp:TableRow> <asp:TableCell CssClass="asp-table-header">Select Route<asp:Label ID="lblTest" runat="server"></asp:Label> </asp:TableCell>
1
5310
by: WhiteWizard | last post by:
First my apologies, this may be longer than the normal question. I have a windows app (.NET 2.0, VS2005), and I've written a user control that will allow the user to "drag and drop" a directory from one computer (files are in NVM), to another (files on a PCMIA memory card). The actual directory names are meaningless to the user, so we display a data item and a datetime that the directory was created. When I fill the listbox that is the...
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8332
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8525
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.