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 9241803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:26:35+00:00 2026-06-18T08:26:35+00:00

I am attempting to use the following code to find the width of the

  • 0

I am attempting to use the following code to find the width of the longest item in a listbox and then alter the HorizontalExtent property of the listbox to fit the item within the bounds of the horizontal scroll bar:

Graphics widthFinder = listBox_Transactions.CreateGraphics();
int needScrollWidth = 0; int checkVal = 0;
for (int i = 0; i < listBox_Transactions.Items.Count; i++)
{
    checkVal = (int)widthFinder.MeasureString(listBox_Transactions.Items[i].ToString(), listBox_Transactions.Font).Width + 1;
    if (needScrollWidth < checkVal)
    { needScrollWidth = checkVal; }
}

listBox_Transactions.HorizontalScrollbar = true;
listBox_Transactions.HorizontalExtent = needScrollWidth;
listBox_Transactions.Invalidate();

The code seems to work as expected, with the exception that widthFinder.MeasureString(listBox_Transactions.Items[i].ToString(), listBox_Transactions.Font).Width always returns 164. I’ve searched for reasons this could be happening, but haven’t found any. Any ideas?

  • 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-06-18T08:26:36+00:00Added an answer on June 18, 2026 at 8:26 am

    It’s hard to know for sure, and sadly as I write this I don’t have the reputation required to ask for clarification as a comment, however…

    I’ve tried your code and it works fine for me. All I can think of is that if you’re using a different DisplayMember and ValueMember then I’m assuming that you’re adding objects to the Items property rather than simple types. In that case your

    listBox_Transactions.Items[i].ToString()
    

    will end up providing you with the name of the object, rather than the value you were hoping for.

    Suppose I have a List of class Foo and add it to your listbox

    List<Foo> fooList = new List<Foo>();
    fooList.Add(new Foo() { Bar = 1 });
    fooList.Add(new Foo() { Bar = 2 });
    fooList.Add(new Foo() { Bar = 3 });
    fooList.Add(new Foo() { Bar = 45 });
    
    listBox_Transactions.Items.AddRange(fooList.ToArray());
    listBox_Transactions.DisplayMember = "Bar";
    listBox_Transactions.ValueMember = "Bar";
    

    then in your code when I get to

    listBox_Transactions.Items[i].ToString()
    

    I will get the value

    "Namespace.Foo"
    

    Rather than the value I was meaning to get. This would therefore always end up giving the same string length.

    To fix this, cast back to your object type in the above code like so:

    ((Foo)listBox_Transactions.Items[i]).Bar.ToString()
    

    Hopefully this will help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to use the following code to search a database using words
I am attempting to use the following code to upload a photo to Facebook
So I am attempting to use the following code to center a div in
In the following code I am attempting to use regex to extract portions of
I'm attempting to use the following code to serialize an anonymous type to JSON:
I'm attempting to use the following sql to retrieve the total amount received, and
I get the following error when attempting to use django-openid-auth OpenID discovery error: No
I am attempting to use java scanner with the following set of delimiters &
I am attempting to use a jquery drowndown checklist control. Following some simple examples,
I'm attempting to learn how to use Cucumber and creating steps using the following

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.