For a project I'm working on, a stopwatch-like timer needs to be displayed to the form. If I remember correctly in VB, text could be outputted to a picture box, but I can't find a way to do it in c#. A label would work, if there is a way to prevent the resizing of the box. Thanks.
A Label would probably be the simplest option. I'm not sure why you would need to prevent resizing of the picturebox (which is also simple to do).
If you are worried about your picturebox being resized and your Label no longer being centered, or being the wrong size, you can just put code in the resize event which dynamically updates the size and location of the Label and its font, based on the current size of the picturebox.
However, if you are determined to not use labels, you can always have your picturebox subscribe to the Paint event and then use e.Graphics to just draw your text whenever the picturebox is repainted.
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using (Font myFont = new Font("Microsoft Sans Serif", 10))
{
e.Graphics.DrawString("This time is...Hammertime", myFont, Brushes.Black, new Point(0,0));
}
}
However, you would also have to redraw this for every iteration of your timer.
Like I said, Label would be a better option than this. Calling e.Graphics wouldn't give you any real advantage over Label , but it would create more things for you to worry about.
Using a TextBox is probably the most appropriate thing for this; just set ReadOnly to true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With