Hello,
how to set label location in top middle at run time according to the text length. For example i have one panel control of size 316,162 and one label at top it displays the security que from the database like what is your pet's name? Or who is your favourite childhood hero? So according to the text length it should come in the middle. For long text no probs but for small it display in left only.

So what exactly are you trying to do? Move the label around so that the text always appears in the middle?

Could you not just make the label the maximum size it could be and set the text align to middle?

I don't want alignment of a text. I want that whole lable should move in a that way that whatevev the text small or long it should appear in tha top middle. For example if i will put label im the left side ane text is long than it look in the center bt if text is small than it display in left only.

Forgive me if im wrong for arguing though, if you made your label span from one side of the form to the other, and did text align center, the text (regardless of length) will always sit within the center or the form.

From what your saying thats the simplest way to gain your outcome. Unless your wanting something else and im misunderstanding in which case clarify further.

I got the solution. Thanks mikey

I feel like the solution was not posted in this thread and I think I shall add it for all of those who will future reference this post. The way you center any type of text on a forms application, or an XNA game screen is to get the screen size divided by two and subtract the text size divided by two from that. For example:

label1.Location = new Point((this.Width / 2) - (label1.Width / 2), yLocation);

As for containing this in a panel or group box or any other type of container control in visual studio, it is usually just making sure that the coordinates are inside the container.

The solution was i have set labels auto size property to false. Than i have stretched the label till both the ends and than i have set text align property to center. Khatam. Now whatever be the text it will display in center only.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.