Dude with ScrollBar

放肆的年华 提交于 2020-01-16 08:26:53

问题


i've a problem with de ScrollBar in VB6. Watch de next gif:

How can I solve that?

Here's my all code:

Option Explicit
    Private old Post As Integer
    
    Dim index As Integer
    Dim indicee As Integer
    
    Public Disclaimer
    
    Private Sub btnAdd_Click ()
    index = index + 1 'we increase the index
    indicee = indicee + 0 'we start it at 0
    pic1 (indicee) .Visible = True
    
    'Label and TextBox type
    lblType (indicee) .Visible = True
    cmbAddType (indicee) .Visible = True
    
    'Label and TextBox prefix
    lblAddPrefix (indicee) .Visible = True
    txtAddPrefix (indicee) .Visible = True
    
    'Number Label and TextBox
    lblAddNum (indicee) .Visible = True
    txtAddNumber (indicee) .Visible = True
    
    chkAddPrincipal (indicee) .Visible = True
    
    'Label and TextBox link
    lblAddVin (indicee) .Visible = True
    cmbAdd Link (indicee) .Visible = True
    
    'uc1
    Load pic1 (index) 'we create the control
    pic1 (index) .Visible = True 'we make it visible
    pic1 (index) .Top = pic1 (index - 1) .Top + pic1 (index - 1) .Height + 20
    
    'lblType
    Load lblType (index)
    Set lblType (index) .Container = pic1 (index)
    lblType (index) .Visible = True
    lblType (index) .Top = lblType (index - 1) .Top
    'cmbAddType
    Load cmbAddType (index)
    Set cmbAddType (index) .Container = pic1 (index)
    cmbAddType (index) .Visible = True
    cmbAddType (index) .Top = cmbAddTipo (index - 1) .Top
    
    'lblAddPrefix
    Load lblAddPrefix (index)
    Set lblAddPrefix (index) .Container = pic1 (index)
    lblAddPrefix (index) .Visible = True
    lblAddPrefix (index) .Top = lblAddPrefix (index - 1) .Top
    'txtAddPrefix
    Load txtAddPrefix (index)
    Set txtAddPrefix (index) .Container = pic1 (index)
    txtAddPrefix (index) .Visible = True
    txtAddPrefix (index) .Top = txtAddPrefix (index - 1) .Top
    
    'lblAddNum
    Load lblAddNum (index)
    Set lblAddNum (index) .Container = pic1 (index)
    lblAddNum (index) .Visible = True
    lblAddNum (index) .Top = lblAddNum (index - 1) .Top
    'txtAddNumber
    Load txtAddNumber (index)
    Set txtAddNumber (index) .Container = pic1 (index)
    txtAddNumber (index) .Visible = True
    txtAddNumber (index) .Top = txtAddNumber (index - 1) .Top
    
    'checkAddPrincipal
    Load chkAddPrincipal (index)
    Set chkAddPrincipal (index) .Container = pic1 (index)
    chkAddPrincipal (index) .Visible = True
    chkAddPrincipal (index) .Top = chkAddPrincipal (index - 1) .Top
    
    'lblAddVin
    Load lblAddVin (index)
    Set lblAddVin (index) .Container = pic1 (index)
    lblAddVin (index) .Visible = True
    lblAddVin (index) .Top = lblAddVin (index - 1) .Top
    'cmbAdd Link
    Load cmbAdd Link (index)
    Set cmbAdd Link (index) .Container = pic1 (index)
    cmbAdd Link (index) .Visible = True
    cmbAddLink (index) .Top = cmbAddLink (index - 1) .Top
    
    End Sub
    
    
    
    
    
    Private Sub Form_Load ()
       scrollAdd.Min = 0
       scrollAdd.Max = 1000
       scrollAdd.SmallChange = Screen.TwipsPerPixelX * 10
       scrollAdd.LargeChange = scrollAdd.SmallChange
    End Sub
    
    Private Sub scrollAdd_Change ()
    ScrollPictureBox
    End Sub
    
    Private Sub scrollAdd_Scroll ()
    ScrollPictureBox
    End Sub
    
    Private Sub ScrollPictureBox ()
       Dim c As Control
    
       For Each c In Me.Controls
          If c.Container.Name = "pic1" And Not TypeOf c Is VScrollBar Then
             c.Top = c.Top + (oldPos - scrollAdd.Value)
          End if
       Next
    
       oldPos = scrollAdd.Value
    End Sub

Can anyone help me? I need to solve that problem with the ScrollBar. I need it to move correctly, how do I do it?

~I added gifs to you can understand my "bug/error" My english is not good but i hope that you can understand I want the scrollbar to move correctly without moving the form as seen in the gif. The idea is that when pressing the button, fields are added and with the ScrollBar they can be seen but as you will see the whole form moves including the ScrollBar~

What I need is that there is a ScrollBar that allows scrolling to see all the elements that are added each time the button is pressed.


回答1:


Since this is your third question on this topic, I will present a complete and much simpler solution. The first step is to create a UserControl visually using the Designer. The result should look like this:

You then start building your main form. You can use the UserControl for both the top section and any additional instances you require. The form ends up looking like this:

The red circle shows a UserControl named uc1 with an Index of 0. The blue circle shows a VScrollBar control inside a PictureBox named Picture1. All additional UserControl's will also be placed inside the PictureBox. Now on to the code:

Option Explicit

Private index As Integer
Private oldPos As Integer

Private Sub Form_Load()
   scrollAdd.Min = 0
   scrollAdd.Max = 3000
   scrollAdd.SmallChange = Screen.TwipsPerPixelX * 10
   scrollAdd.LargeChange = scrollAdd.SmallChange

   Picture1.Visible = False
End Sub

Private Sub scrollAdd_Change()
   ScrollControls
End Sub

Private Sub scrollAdd_Scroll()
   ScrollControls
End Sub

Private Sub btnAdd_Click()
   index = index + 1

   Load uc1(index)
   Set uc1(index).Container = Picture1  'place the control inside the PictureBox
   uc1(index).Visible = True
   uc1(index).Top = IIf(index = 1, 0, uc1(index - 1).Top + uc1(index - 1).Height + 20)

   Picture1.Visible = True
End Sub

Private Sub ScrollControls()
   Dim c As Control

   For Each c In Me.Controls
      If c.Container.Name = "Picture1" And Not TypeOf c Is VScrollBar Then
         c.Top = c.Top + (oldPos - scrollAdd.Value)
      End If
   Next

   oldPos = scrollAdd.Value
End Sub

Notice how simple the code has become, in particular the Add event handler. It also works the way you need. At some point you will need to gain access to the state of a UserControl. What I typically do is define properties for the UserControl:

Option Explicit

Public Property Get AddType() As String
   AddType = cmbAddType.Text
End Property

Public Property Let AddType(ByVal Value As String)
   cmbAddType.Text = Value
End Property

Public Property Get AddPrefix() As String
   AddPrefix = txtAddPrefix.Text
End Property

Public Property Let AddPrefix(ByVal Value As String)
   txtAddPrefix.Text = Value
End Property

Public Property Get AddNumber() As String
   AddNumber = txtAddNumber.Text
End Property

Public Property Let AddNumber(ByVal Value As String)
   txtAddNumber.Text = Value
End Property

Public Property Get AddPrincipal() As Integer
   AddPrincipal = chkAddPrincipal.Value
End Property

Public Property Let AddPrincipal(ByVal Value As Integer)
   chkAddPrincipal.Value = Value
End Property

Public Property Get AddLink() As String
   AddLink = cmbAddLink.Text
End Property

Public Property Let AddLink(ByVal Value As String)
   cmbAddLink.Text = Value
End Property

With the properties in place, you can now set and get the state of a UserControl using any valid index:

Private Sub TestTheScreen()
   'you can initialize the controls as needed
   uc1(0).AddPrefix = "My Prefix"
   uc1(0).AddPrincipal = vbChecked

   'and at some point retrieve the state
   Dim ap As Integer
   ap = uc1(0).AddPrincipal
End Sub


来源:https://stackoverflow.com/questions/59410212/dude-with-scrollbar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!