Here are my answers:
- Something like this:
Choose some spacing integer value, such as ySpacer
, declare it explicitly as a constant. Create a method for creating just one label with parameters, like text and location, returning the reference to a newly created label. First create one to be used as a sample. From an obtained label reference, take its height = sampleLabel.Height
. For a next label, shift its x-location by (height + ySpacer) * n
, where n
is the number of the label, starting from zero.
More accurately: you don't need a specially label sample, because when you just created a label, even before it is inserted in the parent control, it already has a correct height (which is actually defined when you set its font). So, you can set its Top
based in its own height. - The answer by ThePhantomUpvoter on this item is correct, but it's not so suitable if you have more than one parameter to be inserted in your text. This is because strings are immutable, so repeated concatenation will be very inefficient (do I need to explain why?) and the code purely readable and awkward to write.
So, use string.Format
instead:
myLabel.Text = string.Format("Hello. My name is {0}", myTextBox.Text);
Please see:
http://msdn.microsoft.com/en-us/library/system.string.format.aspx[^]. - You don't need to maintain the positions of controls in response to events like resizing. Instead, you should design the layout the way it is done automatically and the UI is tolerant to the change in form size. Best way to do it is to base your layout on hierarchy of panels, with all controls docked with the use of the properties
Control.Dock
and Control.Padding
.
Please see my past answers on this topic:
Zom Out malfunctions when Screen resolution changes[^],
how to dock button so that it can adjust with the form[^] (you will find a simple code sample here).
Some general ideas on UI design:
GUI Apperance - C#.Net[
^].
[EDIT]
Some better ideas alternative to #1:
#1a: Instead of explicitly assigning location for a label or other control, use the specialized-layout panel, such as
TableLayoutPanel
:
http://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel.aspx[
^].
You will have to make such panel a parent of all your multiple items (or group if items, such as a label with labeled control). The child items might not be the labels directly, but a set of small panels (of the
Panel
type) each hosting a label and another control(s) with proper docking (see #3). It will also provide proper spacing between items.
#1b: I feel that you are trying to use those labels merely to provide a set of texts in your form. Actually, a label should be used almost exclusively to provide a key
accelerator to some labeled control, which gets a keyboard focus when you hit Alt+SomeKey, where the key is defined by "
&char...
" placed in the label's text. If you don't use a label for labeling another control and an accelerator, you should think about using something else. In you case, if you just need a set of texts arranges one under another, you could be better off with…
ListBox
. You will be able to add/remove/modify any item at any time and never worry about their layout.
Good luck,
—SA