Visual C# need picturebox with text on top

Associate
Joined
5 Dec 2002
Posts
141
Im trying to design a touchscreen jukebox program in visual c# (free express edition) and need to add a number of picturebox's on the screen then overlay some text on top. I've managed to overlay images(png's) on top of other picturebox's but can't seem to do it with text.

The part of the program is:

for (int i = 0; i < 8; i++)
{
albumTrackBoxes = new PictureBox();
albumTrackBoxes.Size = new Size(400, 50);
albumTrackBoxes.BackColor = Color.YellowGreen;
albumTrackBoxes.BorderStyle = BorderStyle.None;
albumTrackBoxes.Location = new Point(420, 100 + (i * 60));
albumTrackBoxes.TabIndex = i;
Controls.Add(albumTrackBoxes);
Graphics gr = albumTrackBoxes.CreateGraphics();
Font myFont = new Font("Arial", 14);
gr.DrawString("Track ", myFont, Brushes.Blue,420,100+(i*60));
gr.Dispose();
}

At this stage i'm not setting an image for the pictureboxes as I need to design it, so instead just setting a fill colour (YellowGreen). The boxes show on screen as expected but no matter what I do I can't get the text to show.

Any help would be appreciated.
 
Soldato
Joined
7 Apr 2004
Posts
4,212
You should be able to use a combination of control.SendToBack() and control.BringToFront().

For example, if your text is in Label control called lbl, just call lbl.BringToFront()

EDIT: oops I thought you were using seperate controls :p see below for sensible answers
 
Last edited:
Caporegime
Joined
18 Oct 2002
Posts
29,490
Location
Back in East London
You need to use the Paint event handler to draw a string on an picturebox. A quick example:

Code:
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public string ImagesPath = @"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures";
        public Dictionary<PictureBox, string> PictureNames = new Dictionary<PictureBox, string>();

        public Form1()
        {
            InitializeComponent();
            InitializeImages();
        }

        public void InitializeImages()
        {
            var files = Directory.GetFiles(ImagesPath, "*.jpg");

            for (var index = 0; index < files.Length; index++)
            {
                var file = files[index];
                var image = Image.FromFile(file);
                var picBox = new PictureBox
                                 {
                                     BackgroundImage = image,
                                     Size = new Size(200, 200),
                                     Location = new Point(index * 210, 0)
                                 };
                picBox.Paint += picBox_Paint;
                PictureNames.Add(picBox, file.Substring(file.LastIndexOf(Path.DirectorySeparatorChar) + 1));
                Controls.Add(picBox);
            }
        }

        public void picBox_Paint(object sender, PaintEventArgs e)
        {
            var name = PictureNames[(PictureBox)sender];
            e.Graphics.DrawString(name, new Font("Arial", 14), Brushes.White, 10, 10);
        }
    }
}

EDIT: a screenie..

2pq5jmc.jpg
 
Last edited:
Soldato
Joined
16 May 2005
Posts
6,509
Location
Cold waters
You're all about var, huh, Dj? :D

In the original code it looks like you're forgetting that the co-ordinate origin for your Graphics instance is at the top left of the PictureBox, so the co-ordinates passed to DrawString should reflect that.
 
Associate
OP
Joined
5 Dec 2002
Posts
141
Thanks everyone, think I know the problems i've been having now.
Until I get the designs of the background right i'm using label controls instead but this code will help me later.
Thanks
 
Back
Top Bottom