locked
Is it possible to align text in a listbox? RRS feed

  • Question

  • How can i align text in list box into right side ?? is it possible??
    Tuesday, November 29, 2011 3:02 PM

Answers

  • The RightToLeft property is for languages that are written right to left, it is not the same as right justifying text that is read left to right.

    Try changing the listbox's DrawMode to OwnerDrawFixed, then override the DrawItem event with the following code...

          // lbNotes is the ListBox in question.
    private void lbNotes_DrawItem(object sender, DrawItemEventArgs e)
    {
    string output = lbNotes.Items[e.Index].ToString();
    float olength = e.Graphics.MeasureString(output, e.Font).Width;
    float pos = lbNotes.Width - olength;
    SolidBrush brush = new SolidBrush(e.ForeColor);
    e.Graphics.DrawString(output, e.Font, brush, pos, e.Bounds.Top);
    }

    I have no idea what this will do to any horizontal scroll bars.

     


    Wednesday, November 30, 2011 5:25 AM
  • There is a possible answer to this on the other thread,

    http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/abf82327-737f-4e54-a656-25778d832e1c

     

    Basically, you need to draw the background colour before you draw the text (my oversight, sorry).

    Tuesday, December 6, 2011 9:59 AM
  • Hi Ante Meridian

    Thank you very much

    I got answer.

     

    private void m_lstHistory_DrawItem(object sender, DrawItemEventArgs e)
            {
                string sOoutput = m_lstHistory.Items[e.Index].ToString();
                float folength = e.Graphics.MeasureString(sOoutput, e.Font).Width;
                float fpos = m_lstHistory.Width - folength;
                SolidBrush brushBack = new SolidBrush(e.BackColor);
                e.Graphics.FillRectangle(brushBack, e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height);
                SolidBrush brush = new SolidBrush(e.ForeColor);
                e.Graphics.DrawString(sOoutput, e.Font, brush, fpos, e.Bounds.Top);
               
            }

    Tuesday, December 6, 2011 10:43 AM

All replies

  • Set the 'RightToLeft' property of the listbox to Yes

    Tuesday, November 29, 2011 3:08 PM
  • ya it is possible

    have a look at this link. the code is in VB

    http://social.msdn.microsoft.com/Forums/en/Vsexpressvb/thread/6f17c253-8ee6-4182-81e6-577037e81fc1

    if u couldnt understand do post then we wil post the code here

    Tuesday, November 29, 2011 3:31 PM
  • ListBox.RightToLeft gets or sets a value indicating whether text displayed by the control is displayed from right to left.
    aelassas.free.fr
    Tuesday, November 29, 2011 4:00 PM
  • ... but then watch where you put punctuation signs.

            ListBox listBox1 = new ListBox();
            public Form1()
            {
                InitializeComponent();
    
                listBox1.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
                this.Controls.Add(listBox1);
    
                listBox1.Items.Add("Hello");
    
                //watch the puctuation signs location...
                listBox1.Items.Add("?how are you");
            }
    

    Regards,

      Thorsten

    Tuesday, November 29, 2011 5:08 PM
  • Hello Nidhiraj please have a look at following thread

    http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/8ac5a2b0-08e4-4fb3-9793-d8389626043e/


    Want to add MVP with my name.
    Tuesday, November 29, 2011 5:39 PM
  • While it's possible, it's probably not the right tool for the job.  Consider using a ListView in Details style with a single column.  Columns in ListViews have an alignment property where you can set the contents to be right aligned.
    Tuesday, November 29, 2011 6:38 PM
  • while i am setting 'Right To Left' property also have some problems,

    my string is -123 or 123$ after adding this item to list box then list box contains value like 123- or $123 why it is happening,

    Wednesday, November 30, 2011 4:19 AM
  • while i am setting 'Right To Left' property also have some problems,

    my string is -123 or 123$ after adding this item to list box then list box contains value like 123- or $123 why it is happening,
    Wednesday, November 30, 2011 4:20 AM
  • hallo Bahushekh

     

    while i am setting 'Right To Left' property also have some problems,

    my string is -123 or 123$ after adding this item to list box then list box contains value like 123- or $123 why it is happening,

    Wednesday, November 30, 2011 4:54 AM
  • The RightToLeft property is for languages that are written right to left, it is not the same as right justifying text that is read left to right.

    Try changing the listbox's DrawMode to OwnerDrawFixed, then override the DrawItem event with the following code...

          // lbNotes is the ListBox in question.
    private void lbNotes_DrawItem(object sender, DrawItemEventArgs e)
    {
    string output = lbNotes.Items[e.Index].ToString();
    float olength = e.Graphics.MeasureString(output, e.Font).Width;
    float pos = lbNotes.Width - olength;
    SolidBrush brush = new SolidBrush(e.ForeColor);
    e.Graphics.DrawString(output, e.Font, brush, pos, e.Bounds.Top);
    }

    I have no idea what this will do to any horizontal scroll bars.

     


    Wednesday, November 30, 2011 5:25 AM
  • Thanks Ante Meridain
    Tuesday, December 6, 2011 6:58 AM
  • now right align problem is solved. but one problem for this code.

    if one column is selected then i cant see that item. item completely goes to white.

    how can i solve this problem.

     

    Tuesday, December 6, 2011 9:40 AM
  • There is a possible answer to this on the other thread,

    http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/abf82327-737f-4e54-a656-25778d832e1c

     

    Basically, you need to draw the background colour before you draw the text (my oversight, sorry).

    Tuesday, December 6, 2011 9:59 AM
  • Hi Ante Meridian

    Thank you very much

    I got answer.

     

    private void m_lstHistory_DrawItem(object sender, DrawItemEventArgs e)
            {
                string sOoutput = m_lstHistory.Items[e.Index].ToString();
                float folength = e.Graphics.MeasureString(sOoutput, e.Font).Width;
                float fpos = m_lstHistory.Width - folength;
                SolidBrush brushBack = new SolidBrush(e.BackColor);
                e.Graphics.FillRectangle(brushBack, e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height);
                SolidBrush brush = new SolidBrush(e.ForeColor);
                e.Graphics.DrawString(sOoutput, e.Font, brush, fpos, e.Bounds.Top);
               
            }

    Tuesday, December 6, 2011 10:43 AM
  • Hai Ante Meridian

    In listbox item have one problem

    string length is 250 and  listbox width is 150

    how i draw this string as double line in listbox with same index

    is it possible with same index??

    Tuesday, December 6, 2011 3:10 PM
  • Yes, it's possible.

    You must change the ItemHeight property to be big enough to hold two lines, then in the DrawItem method split the strings into two parts and draw each part at a different y-location. How you split the strings is up to you (equal halves, most on the first line and what's left on the next, split at some special character such as a dollar sign, and so on), so I can't give you exact code for your situation, but here's an example to give you the idea.

    This code will split each line into halves with an equal number of characters.

          private void lbNotes_DrawItem(object sender, DrawItemEventArgs e)
          {
             SolidBrush brushBack = new SolidBrush(e.BackColor);
             e.Graphics.FillRectangle(brushBack, e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height);

             for (int i = 0; i < 2; i++)
             {
                string sOoutput = lbNotes.Items[e.Index].ToString();
                if (i == 0)
                   sOoutput = sOoutput.Substring(0, sOoutput.Length / 2); // First half.
                else
                   sOoutput = sOoutput.Substring((sOoutput.Length / 2)); // Second half.
                float folength = e.Graphics.MeasureString(sOoutput, e.Font).Width;
                float fpos = lbNotes.Width - folength;
                float fY = e.Bounds.Top + (i * e.Bounds.Height / 2.0f); // y-location, positive down.
                SolidBrush brush = new SolidBrush(e.ForeColor);
                e.Graphics.DrawString(sOoutput, e.Font, brush, fpos, fY);
             }
          }

    (The code insertion tool is broken at the moment, so it's not formatted properly)

    If you want every string to occupy two lines, then you only need to change ItemHeight once, for the ListBox. If you want just a few extra long strings to occupy two lines and the rest to occupy just one line, then you must set the ListBox's DrawMode to OwnerDrawVariable and set the height of each individual item.

     

     

    Tuesday, December 6, 2011 11:04 PM
  • Hi Nidhinraj,

    I'm glad to hear that you got it working. If you have any difficulty in future programming, we welcome you to post here again.


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us
    Wednesday, December 7, 2011 3:34 AM
  • Hai Ante Meridian

    thanks for response

    I incresed itemheight then whole listbox item height is changed, I want to change each item have different height.

    that is first item have one line, second item have 3 line ...

    is it possible???

     

    regards

    Nidhinraj

    Wednesday, December 7, 2011 10:21 AM
  • Hi Bob Shen

    thanks for your response.

    In future I'm expecting your help

     

    regards

    Nidhinraj

    Wednesday, December 7, 2011 10:30 AM
  • It is possible to have items of varying height. You must change the DrawMode to OwnerDrawVariable, then change the height of each item in the MeasureItem event (e.ItemHeight = whatever;).

    There is an example in Microsoft's help here...

    http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.measureitem.aspx 

    Wednesday, December 7, 2011 11:12 AM