VB form moves down...

VB form moves down...

Anonymous
Not applicable
360 Views
11 Replies
Message 1 of 12

VB form moves down...

Anonymous
Not applicable
I have this form (frmOptions) with a check box (graphical displayed as a toggle button "cmbMoreOptions") that expands my form for some more options. But if I run the code, the whole form moves down!!! So I tried to "copy" and "re-apply" the Top and Left values, but the Top value is incorrect... Does any one have any ideas??? Private Sub cmbMoreOptions_Click() Dim OptionsTop As Double Dim OptionsLeft As Double OptionsTop = frmOptions.Top OptionsLeft = frmOptions.Left If cmbOptions.Value = 1 Then cmbOptions.Caption = "<<<" frmOptions.Width = frmOptions.Width + 4000 frmOptions.Top = OptionsTop frmOptions.Left = OptionsLeft Else cmbOptions.Caption = ">>>" frmOptions.Width = frmOptions.Width - 4000 frmOptions.Top = OptionsTop frmOptions.Left = OptionsLeft End If End Sub
0 Likes
361 Views
11 Replies
Replies (11)
Message 2 of 12

Anonymous
Not applicable
Your code should work OK but could be simplified to: Private Sub cmbOptions_Click() If cmbOptions.Value = vbChecked Then cmbOptions.Caption = "<<<" frmOptions.Width = frmOptions.Width + 4000 Else cmbOptions.Caption = ">>>" frmOptions.Width = frmOptions.Width - 4000 End If End Sub - I assumed that the code was for the cmdOptions checkbox, not some other control called cmbMoreOptions? - It is good practice to use vb's constant values in place of hard coded numbers (vbChecked). - If your code never makes reference to the .Top property it should never change. - If there something else going on, place a breakpoint in your code and add a watch on the variables and properties of interest (frmOptions.Top in this case). Step through the code and try to find where or when the change is occurring. Cheers, Neil "Teun Ham (IV9)" wrote in message news:41400acd_2@newsprd01... > I have this form (frmOptions) with a check box (graphical displayed as a > toggle button "cmbMoreOptions") that expands my form for some more options. > But if I run the code, the whole form moves down!!! > > So I tried to "copy" and "re-apply" the Top and Left values, but the Top > value is incorrect... > > Does any one have any ideas??? > > Private Sub cmbMoreOptions_Click() > > Dim OptionsTop As Double > Dim OptionsLeft As Double > > OptionsTop = frmOptions.Top > OptionsLeft = frmOptions.Left > > If cmbOptions.Value = 1 Then > cmbOptions.Caption = "<<<" > frmOptions.Width = frmOptions.Width + 4000 > frmOptions.Top = OptionsTop > frmOptions.Left = OptionsLeft > Else > cmbOptions.Caption = ">>>" > frmOptions.Width = frmOptions.Width - 4000 > frmOptions.Top = OptionsTop > frmOptions.Left = OptionsLeft > End If > > End Sub > >
0 Likes
Message 3 of 12

Anonymous
Not applicable
Hi Neil, I am starting to believe it's a VB bug. Using VBA, this code works like it should: Private Sub ToggleButton1_Click() If ToggleButton1.Value = True Then ToggleButton1.Caption = "<<<" UserForm1.Width = UserForm1.Width + 400 Else ToggleButton1.Caption = ">>>" UserForm1.Width = UserForm1.Width - 400 End If End Sub Using the same code in VB, the UserForm will move down! Does this code work for you in VB? And "vbChecked", I will remember that one! Thanks Neil! "Neil Munro" wrote in message news:414012df_2@newsprd01... > Your code should work OK but could be simplified to: > > Private Sub cmbOptions_Click() > > If cmbOptions.Value = vbChecked Then > cmbOptions.Caption = "<<<" > frmOptions.Width = frmOptions.Width + 4000 > Else > cmbOptions.Caption = ">>>" > frmOptions.Width = frmOptions.Width - 4000 > End If > > End Sub > > - I assumed that the code was for the cmdOptions checkbox, not some other > control called cmbMoreOptions? > - It is good practice to use vb's constant values in place of hard coded > numbers (vbChecked). > - If your code never makes reference to the .Top property it should never > change. > - If there something else going on, place a breakpoint in your code and add > a watch on the variables and properties of interest (frmOptions.Top in this > case). Step through the code and try to find where or when the change is > occurring. > > Cheers, > Neil > > > "Teun Ham (IV9)" wrote in > message news:41400acd_2@newsprd01... > > I have this form (frmOptions) with a check box (graphical displayed as a > > toggle button "cmbMoreOptions") that expands my form for some more > options. > > But if I run the code, the whole form moves down!!! > > > > So I tried to "copy" and "re-apply" the Top and Left values, but the Top > > value is incorrect... > > > > Does any one have any ideas??? > > > > Private Sub cmbMoreOptions_Click() > > > > Dim OptionsTop As Double > > Dim OptionsLeft As Double > > > > OptionsTop = frmOptions.Top > > OptionsLeft = frmOptions.Left > > > > If cmbOptions.Value = 1 Then > > cmbOptions.Caption = "<<<" > > frmOptions.Width = frmOptions.Width + 4000 > > frmOptions.Top = OptionsTop > > frmOptions.Left = OptionsLeft > > Else > > cmbOptions.Caption = ">>>" > > frmOptions.Width = frmOptions.Width - 4000 > > frmOptions.Top = OptionsTop > > frmOptions.Left = OptionsLeft > > End If > > > > End Sub > > > > > >
0 Likes
Message 4 of 12

Anonymous
Not applicable
Some additional info: You were right on your assumptions. I edited the sample code but I forgot to change "Private Sub cmbMoreOptions_Click()" to "Private Sub cmbOptions_Click()". My mistake! And I first had the simplified code, but the form moved down, so I added the .Top and .Left values. The form moves down as soon as the .Width value is being changed. I have no idea why VB would modify the .Top value.
0 Likes
Message 5 of 12

Anonymous
Not applicable
It works fine here (VB6 sp5), but my project was just the form and the checkbox. Do you have some code in the form's resize event that might be affecting the Top property? Again, breakpoints and stepping through the code is usually the best way to find answers. Good luck, Neil "Teun Ham (IV9)" wrote in message news:414023d2_2@newsprd01... > Some additional info: > > You were right on your assumptions. > I edited the sample code but I forgot to change "Private Sub > cmbMoreOptions_Click()" to "Private Sub cmbOptions_Click()". My mistake! > And I first had the simplified code, but the form moved down, so I added the > .Top and .Left values. > > The form moves down as soon as the .Width value is being changed. > I have no idea why VB would modify the .Top value. > >
0 Likes
Message 6 of 12

Anonymous
Not applicable
Nope, I don't change the .Top value anywhere. But to be honest, I don't even know on which SP I am :-) Perhaps I should download the latest SP right now... Brb :-D "Neil Munro" wrote in message news:4140276d$1_1@newsprd01... > It works fine here (VB6 sp5), but my project was just the form and the > checkbox. Do you have some code in the form's resize event that might be > affecting the Top property? Again, breakpoints and stepping through the code > is usually the best way to find answers. > > Good luck, > Neil > > "Teun Ham (IV9)" wrote in > message news:414023d2_2@newsprd01... > > Some additional info: > > > > You were right on your assumptions. > > I edited the sample code but I forgot to change "Private Sub > > cmbMoreOptions_Click()" to "Private Sub cmbOptions_Click()". My mistake! > > And I first had the simplified code, but the form moved down, so I added > the > > .Top and .Left values. > > > > The form moves down as soon as the .Width value is being changed. > > I have no idea why VB would modify the .Top value. > > > > > >
0 Likes
Message 7 of 12

Anonymous
Not applicable
Are there any reasons why you're not on SP6? "Neil Munro" wrote in message news:4140276d$1_1@newsprd01... > It works fine here (VB6 sp5), but my project was just the form and the > checkbox. Do you have some code in the form's resize event that might be > affecting the Top property? Again, breakpoints and stepping through the code > is usually the best way to find answers. > > Good luck, > Neil > > "Teun Ham (IV9)" wrote in > message news:414023d2_2@newsprd01... > > Some additional info: > > > > You were right on your assumptions. > > I edited the sample code but I forgot to change "Private Sub > > cmbMoreOptions_Click()" to "Private Sub cmbOptions_Click()". My mistake! > > And I first had the simplified code, but the form moved down, so I added > the > > .Top and .Left values. > > > > The form moves down as soon as the .Width value is being changed. > > I have no idea why VB would modify the .Top value. > > > > > >
0 Likes
Message 8 of 12

Anonymous
Not applicable
Ok, I have found the problem... When loading the form, I use this to keep the form in front of Inventor: Private Sub Form_Load() Call SetParent(Me.hWnd, InventorApp.MainFrameHWND) End Sub Where "SetParent" is a public function: Public Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long If I remove the SetParent call from the Form_Load(), the Userform will NOT move down. How can I solve this??? "Neil Munro" wrote in message news:4140276d$1_1@newsprd01... > It works fine here (VB6 sp5), but my project was just the form and the > checkbox. Do you have some code in the form's resize event that might be > affecting the Top property? Again, breakpoints and stepping through the code > is usually the best way to find answers. > > Good luck, > Neil > > "Teun Ham (IV9)" wrote in > message news:414023d2_2@newsprd01... > > Some additional info: > > > > You were right on your assumptions. > > I edited the sample code but I forgot to change "Private Sub > > cmbMoreOptions_Click()" to "Private Sub cmbOptions_Click()". My mistake! > > And I first had the simplified code, but the form moved down, so I added > the > > .Top and .Left values. > > > > The form moves down as soon as the .Width value is being changed. > > I have no idea why VB would modify the .Top value. > > > > > >
0 Likes
Message 9 of 12

Anonymous
Not applicable
Try this instead: Place the following in a standard module: '*************************************************************************** **************** ' declarations and constants for setting window on top (of parent application) '*************************************************************************** **************** Public Const HWND_TOP = 0 Public Const HWND_TOPMOST = -1 Public Const HWND_NOTOPMOST = -2 Public Const SWP_NOSIZE = &H1 Public Const SWP_NOMOVE = &H2 Public Const SWP_NOACTIVATE = &H10 Public Const SWP_SHOWWINDOW = &H40 Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _ ByVal X As Long, ByVal Y As Long, ByVal cx As Long, _ ByVal cy As Long, ByVal wFlags As Long) As Long '*************************************************************************** **************** Declare the following form level variable: Private m_lOriginalParenthWnd As Long In the form's load event, add the following: (Where m_oIVApp is the variable holding a reference to the Inventor application object). 'set parent window to main IV window (always on top of IV app) On Error Resume Next m_lOriginalParenthWnd = SetWindowLong(Me.hwnd, GWL_HWNDPARENT, m_oIVApp.MainFrameHWND) If Err Then Err.Clear On Error GoTo 0 End If Finally, restore the original parent window in the form's unload event: 'restore the original parent window before unloading Call SetWindowLong(Me.hwnd, GWL_HWNDPARENT, m_lOriginalParenthWnd) Neil "Teun Ham (IV9)" wrote in message news:414042d4$1_3@newsprd01... > Ok, I have found the problem... > > When loading the form, I use this to keep the form in front of Inventor: > > Private Sub Form_Load() > Call SetParent(Me.hWnd, InventorApp.MainFrameHWND) > End Sub > > Where "SetParent" is a public function: > > Public Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, > ByVal hWndNewParent As Long) As Long > > If I remove the SetParent call from the Form_Load(), the Userform will NOT > move down. > > How can I solve this??? >
0 Likes
Message 10 of 12

Anonymous
Not applicable
Thanks Neil, I will give this a shot. (But I did liked my approach...much less code :-)) "Neil Munro" wrote in message news:414072e2_3@newsprd01... > Try this instead: > > Place the following in a standard module: > > '*************************************************************************** > **************** > ' declarations and constants for setting window on top (of parent > application) > '*************************************************************************** > **************** > > Public Const HWND_TOP = 0 > Public Const HWND_TOPMOST = -1 > Public Const HWND_NOTOPMOST = -2 > Public Const SWP_NOSIZE = &H1 > Public Const SWP_NOMOVE = &H2 > Public Const SWP_NOACTIVATE = &H10 > Public Const SWP_SHOWWINDOW = &H40 > > Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal > hWndInsertAfter As Long, _ > ByVal X As Long, ByVal Y As > Long, ByVal cx As Long, _ > ByVal cy As Long, ByVal wFlags > As Long) As Long > > '*************************************************************************** > **************** > > > > Declare the following form level variable: > > Private m_lOriginalParenthWnd As Long > > > > In the form's load event, add the following: (Where m_oIVApp is the variable > holding a reference to the Inventor application object). > > 'set parent window to main IV window (always on top of IV app) > On Error Resume Next > m_lOriginalParenthWnd = SetWindowLong(Me.hwnd, GWL_HWNDPARENT, > m_oIVApp.MainFrameHWND) > If Err Then > Err.Clear > On Error GoTo 0 > End If > > > > Finally, restore the original parent window in the form's unload event: > > 'restore the original parent window before unloading > Call SetWindowLong(Me.hwnd, GWL_HWNDPARENT, m_lOriginalParenthWnd) > > > Neil > > > "Teun Ham (IV9)" wrote in > message news:414042d4$1_3@newsprd01... > > Ok, I have found the problem... > > > > When loading the form, I use this to keep the form in front of Inventor: > > > > Private Sub Form_Load() > > Call SetParent(Me.hWnd, InventorApp.MainFrameHWND) > > End Sub > > > > Where "SetParent" is a public function: > > > > Public Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, > > ByVal hWndNewParent As Long) As Long > > > > If I remove the SetParent call from the Form_Load(), the Userform will NOT > > move down. > > > > How can I solve this??? > > > >
0 Likes
Message 11 of 12

Anonymous
Not applicable
Oops, missed a constant declaration. Add this to the standard module as well. Public Const GWL_HWNDPARENT = (-8) Sorry, Neil "Teun Ham (IV9)" wrote in message news:414073eb$1_2@newsprd01... > Thanks Neil, I will give this a shot. > > (But I did liked my approach...much less code :-)) >
0 Likes
Message 12 of 12

Anonymous
Not applicable
No apologies needed! "Neil Munro" wrote in message news:414074f4$1_1@newsprd01... > Oops, missed a constant declaration. Add this to the standard module as > well. > > Public Const GWL_HWNDPARENT = (-8) > > Sorry, > Neil > > > "Teun Ham (IV9)" wrote in > message news:414073eb$1_2@newsprd01... > > Thanks Neil, I will give this a shot. > > > > (But I did liked my approach...much less code :-)) > > > >
0 Likes