Center Form Procedure

Hello All,

I have strayed across something very odd here and cannot seem to put my finger on why Express is doing what it is doing to me. I wrote a very simple function to center a child form inside it's parent form, since the startup position property does not appear to work. The procedure is as follows:

frmChild.Left = CInt(frmParent.Left + ((frmParent.Width - frmChild.Width) / 2))

frmChild.Top = CInt(frmParent.Top + ((frmParent.Height - frmChild.Height) / 2))

Express forced the CInt conversion stating that doubles could not implicitly be converted to integers. This seems a bit odd to me since all the property values in question here are integer data types. Hence, no data conversion should be necessary. Where in the world is Express getting the idea that a double data type is even being used here. Blows my mind. Can somebody please shed some light on this I have written the CenterForm function a million times in every version of VB and it has NEVER had to do any data conversion before. The code should look like this:

frmChild.Left = frmParent.Left + ((frmParent.Width - frmChild.Width) / 2)

frmChild.Top = frmParent.Top + ((frmParent.Height - frmChild.Height) / 2)

I do have Option Strict set on in my IDE and I suppose this is why this is happening, but it still should make no difference. Someone please explain this one to me.

Thanks In Advance,

V. Shane Curtis




Answer this question

Center Form Procedure

  • TopDog_4486

    Thanks for helping me get my mind around what was going on, I understand completely now, still say it's strange though. I have always used Option Explict in my code before, and I do have Option Strict turned on in my IDE because I want my code to be as compliant and tight as possible. I am doing a combination conversion and rewrite of one of my old VB6 apps to VB. Express and am trying to adhere as much as possible to the .Net way of doing things. The code and screens are being converted using the conversion wizard but they serve no other function than as a point of reference for the .Net app. All the screens are being done as true Win forms. I am using what code I can but converting what must be converted to .Net syntax. I am not using any of the compatability stuff. Although I do wish that they had not removed Control Arrays from .Net, they were useful, in my opinion, especially for labels. Thanks Again.

  • jamil

    The number would be smaller but there is the potential for a remainder which must be expressed as a decimal and most completely as a double! WHat happens when the width is an odd number ...53 and you divide by 2...you get a remainder of .5...

    > VB never did this before. How to I VB to leave things alone.

    > The conversion is unnecessary to begin with and it just

    >complicates an otherwise simple piece of code

    This is the expected in .NET and it is there to avoid overflows and casting errors...It may seem as if it is unneccessary, but if you get in the habit of using option explicit, option strict and explicity converting types it will cause you less of a head ache in the long run...especially on very large project!



  • CNSD

    As a work-around, you can build a List(of Label).  Here's a subroutine that will make such a list with the labels in the order that you placed them on the form in design-mode.  (the order thing doesn't work if you  are also placing some labels programatically on the form...but you still get a collection of labels...and that problem could be overcome, too)

     'declare the List variable first...

    Private myLabels As New List(Of Label)()

    'some more code here, then later our cool subroutine

    Sub FindTheLabels()

        For i As Integer = Me.Controls.Count - 1 To 0 Step -1

             If Me.Controls.Item(i).GetType().Name = "Label" Then

                'convert general control type to a Label type

                 Dim thisLabel As Label

                  thisLabel = CType(Me.Controls.Item(i), Label)

                 'add it to our List

                  Me.myLabels.Add(thisLabel)

             End If

          Next

    End Sub


  • Lester Hewett

    Thanks for the reply. This still makes little sense to me since a division operation would yeild a smaller value than it's component parts. VB never did this before. How to I VB to leave things alone. The conversion is unnecessary to begin with and it just complicates an otherwise simple piece of code.

  • SDG

    Thanks for the suggestion about the Control Array Solution. It was very helpful. You seem to have a firm grasp of .Net so perhaps you might have a solution for another problem that is driving me insane. VB 6 had a nice little add-in that I used quite often when I needed to create class modules called Class Builder. It made it much easier to create class modules by generating the property procedures for you and such. It was very generic code and needed cleaning up but it worked. One of the things that has driven me nuts about .Net is that they did not provide the Class Builder add-in in .Net. Do you know if there is a similar tool for .Net and where it can be obtained

    Thanks for your help

    V. Shane Curtis



  • Mark Grinols MSFT

    The product of any division is by default a double

  • Nickie

    Hi V,

    Sorry I took so long to reply. No, I don't know about any class builder. I'm afraid I'm new to to both .NET and to VB, and my first experience with VB is VB Express (which uses the .NET platform).

    by the way...I've learned a little more about "testing types"...

    in the code I provided you I had this line:
    If Me.Controls.Item(i).GetType().Name = "Label" Then

    Would be better if it read:
    If TypeOf Me.Controls.Item(i) Is Label Then

    (it'll work both ways...it's just better the second way)

    Also, I wouldn't consider myself to have a firm grasp of .NET. Rather, since my first experience with VB has been with .NET, I'm not having to unlearn a bunch of stuff.


    Cheers,
    --William


  • Center Form Procedure