Avatar of zimmer9
Flag for United States of America asked on

How to select a row from a listbox and get the proper value using C# with VS 2005?

I am converting an application from Access 2003 VBA to a C# Windows application using VS2005.

How would you convert the following Access VBA snippet to C#?

List Box: lstCustRpts  
Row Source SELECT tblCustReports.ReportID, tblCustReports.ReportName, tblCustReports.ReportDescription FROM tblCustReports ORDER BY ReportID;

For Each vnt In lstCustRpts.ItemsSelected
        strQuery = lstCustRpts.Column(1, vnt)
        If (strQuery = "qryCuPFQ") Then
        ElseIf (strQuery = "qryCuPSQ") Then



ReportID  ReportName        ReportDescription
-----------  ----------------       ---------------------------
3                 qryCuPFQ                 ACS Fall Report
4                 qryCuPSQ                 ACS Spring Report

I tried the following C# code but I get the same value

itemText = "qryCuPFQ" for each row I select from the listbox
string selectedyear = (string)this.comboBox1.SelectedItem;
            DataRowView drv = (DataRowView)listBox3.Items[0];
            string itemText = drv.Row["ReportName"].ToString();
            if (itemText == "qryCuPFQ")
            else if (itemText == "qryCuPSQ")
.NET ProgrammingC#

Avatar of undefined
Last Comment

8/22/2022 - Mon
Kiran Sonawane

Have you tried like this...

foreach ( vnt in lstCustRpts.ItemsSelected) {
      strQuery = lstCustRpts.Column(1, vnt);
      if ((strQuery == "qryCuPFQ")) {
      } else if ((strQuery == "qryCuPSQ")) {



EDIT : Please ignore answer

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

I have set the SelectionMode property to "One" for the ListBox which is my intent. But I must be doing something wrong in my code because NO MATTER which ReportName I select from the ListBox, the return value is the 1st ReportName, which is "qryCuPFQ".
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.