问题
I'm making an application in VB.NET and I have a control (a label to be specific) and it's been set to auto size based on the texts in it. Currently, the label box resizes to the left and down:
[Label] ->
|
v
I want the label to resize to the right and down:
<-[Label]
|
v
How would I do this?
Edit: the label display's the windows account name. It's aligned to the right side of the window, so that's why the text must be autosized and expand to the left rather then the right.
回答1:
The only way I can think of is to adjust the location according to the change in size. Here's some code that will do that. I used the Tag property to hold he current size before the resize. then in the Resize event handler adjusted the location. Whenever the label's text is changed the tag gets the size. When the resize is called the size has changed and comparing the 2 will tell us how much to change the location. since the defaul autosize operation is already down I didn't change that.
Private Sub Label1_TextChanged(sender As System.Object, e As System.EventArgs) Handles Label1.TextChanged
Label1.Tag = Label1.Size
End Sub
Private Sub Label1_Resize(sender As System.Object, e As System.EventArgs) Handles Label1.Resize
Dim TempSize As New Size(New Point(0))
If Label1.Tag Is Nothing Then Label1.Tag = Label1.Size
TempSize = DirectCast(Label1.Tag, Size)
Label1.Location = New Point(Label1.Location.X - (Label1.Size.Width - TempSize.Width), Label1.Location.Y)
End Sub
回答2:
I want the label to resize to the right and down:
Set label's property
Autosize = False
TextAlign = TopRight
来源:https://stackoverflow.com/questions/17584203/how-to-change-direction-in-which-a-control-resizes-in-vb-net