473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Centering text in a listbox

In a WinForms app is there an easy way to center the text entries added to a
listbox?

--
Robert W.
Vancouver, BC
www.mwtech.com

Apr 12 '06 #1
5 8836
You'll need to create your own OwnerDrawn control based on the list
box. Try this for starters:

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace CentredListBox
{
public class MyListBox : ListBox
{
private System.ComponentModel.Container components = null;

public MyListBox()
{
InitializeComponent();
base.DrawMode = DrawMode.OwnerDrawFixed;
}

[Browsable(false)]
public override DrawMode DrawMode
{
get { return DrawMode.OwnerDrawFixed; }
set {}
}

protected override void OnDrawItem(DrawItemEventArgs e)
{
// Draw background for each item.
e.DrawBackground();

// Get's fired even if there are no itme sin the list!
if (Items.Count > 0)
{
string item = Items[e.Index].ToString();

SizeF itemSize = e.Graphics.MeasureString(item, e.Font, 0);
PointF centredPoint = new PointF(e.Bounds.Left + ((e.Bounds.Width -
itemSize.Width) / 2), e.Bounds.Top);
Brush itemBrush = SystemBrushes.ControlText;
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
itemBrush = SystemBrushes.HighlightText;
}

// draw item at correct location
e.Graphics.DrawString(item, e.Font, itemBrush, centredPoint);
}

// Draw a focus rectangle around the selected item.
e.DrawFocusRectangle();

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}

Apr 12 '06 #2
Hi,
"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:AC**********************************@microsof t.com...
In a WinForms app is there an easy way to center the text entries added to
a
listbox?

AFAIK there is no way of doing this..

There are several ways to do it though:
1- Calculate the size of the string in the display ( using
Graphinc.MeasureString ) and then insert the correct number of whitespaces
around the text.

2- Write your own control , take a look at the OwnerDraw control from
opennetcf.org (there is also a forum posting about this control)


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Apr 12 '06 #3
Jason,

Thank you! I don't have much experience at building my own custom controls.
Your code was easy to understand and implement.

--
Robert W.
Vancouver, BC
www.mwtech.com

Apr 12 '06 #4
The code provided earlier by Jason is good but it doesn't allow larger fonts
to be used. I tried several manipulations to make it more universal but
couldn't figure out the right one. Any thoughts?

--
Robert W.
Vancouver, BC
www.mwtech.com

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,
"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:AC**********************************@microsof t.com...
In a WinForms app is there an easy way to center the text entries added to
a
listbox?

AFAIK there is no way of doing this..

There are several ways to do it though:
1- Calculate the size of the string in the display ( using
Graphinc.MeasureString ) and then insert the correct number of whitespaces
around the text.

2- Write your own control , take a look at the OwnerDraw control from
opennetcf.org (there is also a forum posting about this control)


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Apr 12 '06 #5
Hi,

Go to
http://www.opennetcf.org/PermaLink.a...8-94ab3c124f63
, download the code and see that the class contain a Font , change it to
your convenience

You can even have different fonts/colors for your items !!!
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:AE**********************************@microsof t.com...
The code provided earlier by Jason is good but it doesn't allow larger
fonts
to be used. I tried several manipulations to make it more universal but
couldn't figure out the right one. Any thoughts?

--
Robert W.
Vancouver, BC
www.mwtech.com

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,
"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:AC**********************************@microsof t.com...
> In a WinForms app is there an easy way to center the text entries added
> to
> a
> listbox?

AFAIK there is no way of doing this..

There are several ways to do it though:
1- Calculate the size of the string in the display ( using
Graphinc.MeasureString ) and then insert the correct number of
whitespaces
around the text.

2- Write your own control , take a look at the OwnerDraw control from
opennetcf.org (there is also a forum posting about this control)


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Apr 13 '06 #6

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

Similar topics

1
by: fooooo | last post by:
How do I center each item in the ListBox widget? Also, is it possible to change the color of the selected item? right now it uses the OSes color. I would like it to be consistant on every machine.
11
by: Jeff Thies | last post by:
I have a series of blocks that are float left that I need centered on the page. <div class="center" align="center"> <div style="width: 100 px;float: left">thumbnail 1</div> <div style="width:...
3
by: Jonah Bishop | last post by:
I have a puzzling problem with centering text, and I'm hoping that someone here can help me out. First of all, let me state that I am using XHTML 1.0 Strict and CSS for all layout purposes (no...
3
by: CMAR | last post by:
To center my <div>, I am currently using something like <div id="navcontainer" align="center"> This centers fine, but is deprecated code. I have seen these two solutions recommended. 1) ...
15
by: red | last post by:
How do I center two side by side divs ? I've been writing css pages for a while but there's one thing tha still eludes me. I can center a div with margin auto. I can place two divs side by side...
3
by: John Pote | last post by:
1. Horizontal centering a <divin browser window. The current trend seems to be to place page content in a fixed width area in the middle of the browser window. How is this achieved? If I use a...
5
by: Markus Ernst | last post by:
Hello This is a test example: http://www.markusernst.ch/anthracite/ http://www.markusernst.ch/anthracite/living_divani.html After googling and experimenting for several hours, I ended up...
4
by: SAL | last post by:
Hello, is there a way to menu control dynamically center itself horizontally on a page? I have placed a menu control in a panel control (no ajax) and would like it to center itself. Depending on...
1
by: =?Utf-8?B?ZnJhbmt5?= | last post by:
Hello, I've created a table that has two rows that are span across three columns. The third row has three columns, each with an image. The last row is also span accross three columns. The span...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.