Align an Image and Text within a WinForms RadioButton -- How to prevent text from overlapping image?

S

Samuel R. Neff

I have a set of radio buttons in a win form that each have an image
and text, like so:

O [image] [label]
O [image] [label]
O [image] [label]
O [image] [label]

How can I set it up so that the image is left aligned, next to the
radio button indicator, and the text is left aligned next to the
image?

I don't see any way to align the text so that it's aware of the image.

Thanks,

Sam
 
G

Guest

Simple solution: make a label with the text and set its location to the radio
left + radio width + some padding.

Trickey solution: Calculate the image width and pad spaces to the radio text:

Dim g As Graphics = RadioButton1.CreateGraphics()
Dim ImageWidth As Integer = RadioButton1.Image.Width
Dim PadText As String = " "
Dim i As Integer = 0

While (True)

If g.MeasureString(PadText, RadioButton1.Font).Width * i >
ImageWidth Then Exit While

i += 1

RadioButton1.Text = PadText + RadioButton1.Text

End While

Maybe it won't work well with some fonts.
 
G

Guest

The best way would be to create a usercontrol that combines the radiobutton,
image and label (and not use the radio button text).

Then use the control will have a property for the image and the text. When
you render the image, set it to autosize, and then set the label control to
have a left of the image.left+image.width + spacervalue.

Le MasterChief
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top