简体   繁体   中英

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. Also tried

txtchat.height = me.scaleheight - txtsend.height - 100 and a ton of other things. 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:

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.

I assume txtSend is the textbox to the left of the Send button. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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