简体   繁体   English

调整表单上某些控件大小的最佳方法是什么?

[英]What is the best way to resize only certain controls on a form?

I'm coding a chat/server client and I'm having some issues trying to get resizing working correctly. 我正在编码一个聊天/服务器客户端,但在尝试重新调整大小时遇到​​了一些问题。

I have a chat form that looks like this. 我有一个看起来像这样的聊天表格。

在此处输入图片说明

I'm wanting to keep the top labels, textbox, and buttons the same size and in the same exact spot. 我想使顶部标签,文本框和按钮的大小和位置完全相同。 I want to adjust the chat textbox, the listbox, and the textbox at the bottom that you type the message to. 我要调整聊天文本框,列表框以及在其上键入消息的底部的文本框。 The send button I want to stay the same size also and always to the right of the send textbox. 我也想保持相同大小的发送按钮,并且总是在发送文本框的右侧。 I'm having a really hard time trying to figure out how to code the resizing part of the code. 我很难弄清楚如何编写代码的大小调整部分。

I could really use some assistance I've tried many things like. 我已经尝试过很多类似的东西,我真的可以使用一些帮助。

txtchat.height = me.scaleheight - 100 adding the 100 in there to try and take off some for the controls on the top of the form. txtchat.height = me.scaleheight - 100 ,在其中添加100,以尝试txtchat.height = me.scaleheight - 100表单顶部的控件。 Also tried 也尝试过

txtchat.height = me.scaleheight - txtsend.height - 100 and a ton of other things. txtchat.height = me.scaleheight - txtsend.height - 100和大量其他内容。 I guess I just don't understand how this resizing works. 我想我只是不了解此调整大小是如何工作的。

As I said, this would be much simpler in .Net (Winforms has docking panels that automate this) but here's what you need to do in VB6: 就像我说的那样,这在.Net中会简单得多(Winforms具有可自动完成此操作的对接面板),但这是在VB6中需要执行的操作:

I don't have VB6 installed so I'm not going to write out the code but the steps should be enough to get it right. 我没有安装VB6,所以我不会写出代码,但是步骤应该足够正确。

I assume txtSend is the textbox to the left of the Send button. 我假设txtSend是“发送”按钮左侧的文本框。 To get the size of the chat box, you need to: 要获取聊天框的大小,您需要:

Bottom_area_height = Statusbar.Height + Statusbar_Padding + _
    txtSend.Height + txtSend_Padding
txtChat.Height = ScaleHeight - (txtChat.Top + Bottom_area_height) ' Fixed here
txtChat.Width = ScaleWidth - lstUsers.Width

txtSend.Top = ScaleHeight - Bottom_area_height
txtSend.Width = ScaleWidth - btnSend.Width

btnSend.Top = txtSend.Top

The padding values are the vertical gaps between the controls - the status bar padding looks like 2 grid cells and the padding for txtSend seems to be half a grid cell. 填充值是控件之间的垂直间隙-状态栏填充看起来像2个网格单元,而txtSend的填充似乎是一个网格单元的一半。 You're not using the grid properly and it makes it very hard to see the distances of controls. 您使用的网格不正确,因此很难看到控件的距离。

Edit 编辑

To add horizontal padding, just move the controls' left edge to where you want to have the left margin and then subtract the margin twice from the width of the textbox (to account for both margins). 要添加水平填充,只需将控件的左边缘移到要有左边距的位置,然后从文本框的宽度中减去两次边距(以解决这两个边距)。

txtChat.Width = ScaleWidth - (txtChat.Left * 2 + User_list_padding)
txtSend.Width = ScaleWidth - (txtSend.Left * 2 + Send_button_padding)
btnSend.Left = ScaleWidth - (btnSend.Width + Send_button_padding)

The padding values are the horizontal gaps between the controls, if you want any. 如果需要,填充值是控件之间的水平间隙。

Dim intAreaHeight As Integer

intAreaHeight = stsBar.Height + 40 + txtSend.Height

txtChat.Height = Me.ScaleHeight - 1450
txtChat.Width = Me.ScaleWidth - lstUsers.Width

lstUsers.Height = Me.ScaleHeight - 1450
lstUsers.Left = txtChat.Width + 40

txtSend.Top = Me.ScaleHeight - intAreaHeight + 20
txtSend.Width = Me.ScaleWidth - cmdSend.Width

cmdSend.Left = txtSend.Width + 40
cmdSend.Top = txtSend.Top

This gets me this result as you can see the left padding and right padding is against the side of the form with no padding at all. 这使我得到了这个结果,因为您可以看到左侧填充和右侧填充抵靠表单的一面,完全没有填充。 I can't seem to figure out a good solution to get a padding. 我似乎无法找到一个好的解决方案来获得填充。

在此处输入图片说明

EDIT: Updated code below worked great thanks for the help. 编辑:下面更新的代码非常感谢您的帮助。

Dim intAreaHeight As Integer

intAreaHeight = stsBar.Height + 40 + txtSend.Height

txtChat.Height = Me.ScaleHeight - 1450
txtChat.Width = Me.ScaleWidth - lstUsers.Width - (txtChat.Left * 2 + 40)

lstUsers.Height = Me.ScaleHeight - 1450
lstUsers.Left = txtChat.Width + 80

txtSend.Top = Me.ScaleHeight - intAreaHeight + 20
txtSend.Width = Me.ScaleWidth - cmdSend.Width - (txtSend.Left * 2 + 40)

cmdSend.Left = txtSend.Width + 80
cmdSend.Top = txtSend.Top

How about: 怎么样:

Just resize the ones you want resized. 只需调整您想要调整的尺寸即可。

Sounds "best" to me. 对我来说听起来“最好”。 I've tried hoping Windows will read my mind but I haven't had any luck yet. 我尝试过希望Windows能读懂我的想法,但我还没有运气。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM