如何在代码中动态调整控件的大小(Visual Basic)

Novices to Visual Basic may appreciate that you can visually build your application via the Visual Studio (or Visual Basic Express) interface, but what about when you create applications where you want the controls to dynamically resize to fit the size of the window. First, we need to logically think how this will be done:

Visual Basic的新手可能会喜欢您可以通过Visual Studio(或Visual Basic Express)界面直观地构建应用程序,但是在创建希望控件动态调整大小以适合窗口大小的应用程序时该怎么办。 首先,我们需要从逻辑上考虑如何做到这一点:

  • First, we need to set the initial dynamic size as the application is loaded.

    首先,我们需要在加载应用程序时设置初始动态大小。
  • Then,  handle an event where when the window is resized we further dynamically resize the controls to fit the new resized size.

    然后,处理一个事件,在该事件中,当调整窗口大小时,我们将进一步动态调整控件的大小,以适应新的调整大小。

And that’s all there is to it! Let’s get started

这就是全部! 让我们开始吧

Here’s an example application I’m creating – a simple Web browser. I’ve added two controls on the form, and changed the Location property for each control to be in the top left corner in order for the resizing to work correctly. The first is a textbox and the second being a WebBrowser control.

这是我正在创建的示例应用程序–一个简单的Web浏览器。 我在窗体上添加了两个控件,并将每个控件的Location属性更改为位于左上角,以便调整大小能够正常工作。 第一个是文本框,第二个是WebBrowser控件。

For my application I’m going to set an event against the TextBox to see when the Enter key has been pressed. To do this I go to the Events section of the Properties window (the lightning bolt) and scroll until I find KeyPress and add the code that detects when the Enter key has been pressed:

对于我的应用程序,我将针对TextBox设置一个事件,以查看何时按下Enter键。 为此,我转到“属性”窗口(闪电)的“事件”部分,滚动直到找到KeyPress并添加检测何时按下Enter键的代码:

Public Class Form1 Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If AscW(e.KeyChar) = 13 Then WebBrowser1.Navigate(TextBox1.Text) End If End Sub End Class

Public Class Form1 Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If AscW(e.KeyChar) = 13 Then WebBrowser1.Navigate(TextBox1.Text) End If End Sub End Class

I’ll explain what’s happening here. The AscW function returns an Integer representation of a character – such as the Enter key, A, Q, etc – each key on a keyboard has such an integer value – called an ASCII value, technically. We simply compare the key entered to the ASCII value to see if the Enter key was pressed. However, in order to compare it to an integer value we first need to get an integer representation of the key entered, as if we just did e.KeyChar = 13 it would be comparing a char to an integer – and that’s going to result in an error. The e variable is declared through the subroutines argument (if you look carefully) against the KeyPressEventArgs class – and this has the KeyChar property which holds the character entered.

我会解释这里发生了什么。 AscW函数返回字符的整数表示形式-例如Enter键,A,Q等-键盘上的每个键都具有这样的整数值-技术上称为ASCII值。 我们只需将输入的键与ASCII值进行比较,看看是否按下了Enter键。 但是,为了将其与整数值进行比较,我们首先需要获得所输入键的整数表示,就好像我们只是将e.KeyChar = 13它会将一个char与一个整数进行比较–这将导致一个错误。 e变量是通过KeyPressEventArgs类的子例程参数(如果您仔细看的话)声明的–该变量具有KeyChar属性,该属性保存输入的字符。

We then set the Navigate subroutine to TextBox1.Text, and of course, the Navigate subroutine is responsible for navigating the WebBrowsr control to a new website. There’s also the Url property that does the exact same thing but with a slightly different implementation since it is set as a “data type” of System.Uri while Navigate is of data type String, hence we use that for simplicity.

然后,将Navigate子例程设置为TextBox1.Text,当然,Navigate子例程负责将WebBrowsr控件导航到新网站。 还有一个Url属性,其功能完全相同,但实现方式略有不同,因为它设置为System.Uri的“数据类型”,而Navigate的数据类型为String,因此为简单起见,我们将其使用。

Now let’s get going to resize the controls as the form (application/window) is first loaded. So go back to the form and double click the title bar of the form window to get the Load event code up.

现在,让我们在首次加载表单(应用程序/窗口)时调整控件的大小。 因此,返回到表单并双击表单窗口的标题栏以获取Load事件代码。

In the event subroutine, we’ll enter the following:

在事件子例程中,我们将输入以下内容:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load WebBrowser1.Size = Me.ClientSize WebBrowser1.Height = WebBrowser1.Height - TextBox1.Height TextBox1.Width = Me.ClientSize.Width End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load WebBrowser1.Size = Me.ClientSize WebBrowser1.Height = WebBrowser1.Height - TextBox1.Height TextBox1.Width = Me.ClientSize.Width End Sub

So what’s happening here? The Size property of the WebBrowser class (“WebBrowser1”) allows us to dynamically set the size of both the width and height at the same time, and then we assign it to Me.ClientSize that is the size of the client area or what I tend to call, the content area.

那么这是怎么回事? WebBrowser类的Size属性(“ WebBrowser1”)允许我们同时动态设置宽度和高度的大小,然后将其分配给Me.ClientSize,即客户区的大小或倾向于调用内容区域。

However, it doesn’t take into account any other controls on the form, so we need to subtract the height of the TextBox from the WebBrowser control.

但是,它没有考虑窗体上的任何其他控件,因此我们需要从WebBrowser控件中减去TextBox的高度。

Finally, we change the width of the TextBox too.

最后,我们也更改了TextBox的宽度。

Now, we need to set the dynamic size as the form is resized, as right now when we resize the form the controls stay in the same static position. All we do is get the Resize event in the Events section of the Properties window against the Form, and add the same code we just added to it – and we’re done!

现在,我们需要在调整窗体大小时设置动态大小,就像现在在调整窗体大小时一样,控件保持在相同的静态位置。 我们要做的就是在“属性”窗口的“事件”部分中,将“调整大小”事件与“表单”相对应,然后添加刚刚添加到其中的相同代码-完成!

Like this post ?

喜欢这个职位吗?

Share on your Social Networking Profile ( Facebook, Twitter & Google+ ) and get a flat 10% Recurring discount on our VPS Hosting and Dedicated Servers.

在您的社交网络配置文件(Facebook,Twitter和Google+)上共享,并在我们的VPS托管和专用服务器上获得10%的固定定期折扣。

Email us the shared link at : [email protected] or speak to our live chat operator now, by clicking on the “Live Chat” Scroller on the left-hand side of this page and we will provide you with the discount Coupon right away!

通过以下电子邮件将共享链接发送给我们: [受电子邮件保护],或通过单击此页面左侧的“实时聊天”滚动条立即与我们的实时聊天操作员联系,我们将立即为您提供折扣优惠券!

翻译自: https://www.eukhost.com/blog/webhosting/how-to-dynamically-resize-controls-in-code-visual-basic/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值