如何在vb.net中更改控件调整大小的方向 [英] How to change direction in which a control resizes in vb.net

查看:410
本文介绍了如何在vb.net中更改控件调整大小的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在VB.NET中创建一个应用程序,并且有一个控件(具体来说是标签),并且已根据其中的文本将其设置为自动调整大小.目前,标签框的大小会向左和向下调整:

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:

[标签]->
|
v

[Label] ->
|
v

我希望标签在左右方向调整大小

I want the label to resize to the right and down:

<-[标签]
|
v

<-[Label]
|
v

我该怎么做?

标签显示的是Windows帐户名.它与窗口的右侧对齐,因此这就是为什么文本必须自动调整大小并向左扩展而不是向右扩展的原因.

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.

推荐答案

我能想到的唯一方法是根据大小的变化来调整位置.这里有一些代码可以做到这一点.我使用Tag属性在调整大小之前将其保持为当前大小.然后在Resize事件处理程序中调整位置.每当标签的文本更改时,标签都会获取大小.调用resize时,大小已更改,将2进行比较将告诉我们更改位置的数量.由于默认的自动调整大小操作已经关闭,因此我没有更改.

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

这篇关于如何在vb.net中更改控件调整大小的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆