Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6751185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:53:49+00:00 2026-05-26T12:53:49+00:00

I have a class Client, a Class Assignment and a static class Clients which

  • 0

I have a class Client, a Class Assignment and a static class Clients which has a static list. The Client class constructor takes an Assignment object. Once the client and assignment object has been created, the Client is then added to the static list.

My problem lies where the user then needs to be able to select any client in the listbox, and have the Assignments Description value displayed into a text box. How do i do this without getting an error telling me “Unable to cast object of type ‘System.String’ to type ‘Rimu.Client’.”

private void clientListBox_MouseClick(object sender, MouseEventArgs e)
{
    if (clientListBox.SelectedItem != null)
    {
        Client current = (Client)clientListBox.SelectedItem;
        current.CurrentAssignment.Description = descriptionText.Text;
    }
}

any help would be greatly appreciated, Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:53:49+00:00Added an answer on May 26, 2026 at 12:53 pm

    Since you want to cast the SelectedItem directly from the ListBox you must add them directly to the list box, like so:

    foreach (var client in Clients) 
    {
      clientListBox.Add(client);
    }
    

    Now the Client objects are being stored in the ListBox, but the string displayed to the user on the winform (or wpf app, you didn’t specify) will be the class name which isn’t very meaningful to the user.

    To fix this override the ToString() method of the Client class

    public class Client
    {
      // you've already written this
    
      public override string ToString() 
      {
        // construct a meaningful string here
        return string.Format("{0} {1}", this.FirstName, this.LastName);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class Client which has a attribute of dogs public class ClientsMap
I have a class that has a constructor. The constructor passes a param to
I have a class Client like that: public class Client { public Person Pers
I have a Client class like that: public class Client { public Person Pers
I`m writing client-server app for windows using WinSock and I have class for server.
I have a class with following description: public class Customer { public ISet<Client> Contacts
Hope I'm asking this correctly: I have a project Projects.Client I have my class
If I have a Client domain class, and that Client hasMany Courses. How do
In my GWT app I have the following model class: import com.google.gwt.user.client.rpc.IsSerializable; public class
I have a class that executes the MSNP15 protocol. The protocol requires clients to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.