0

I'm working on a project in VB.net (VS19) where various data gets imported into an Access database. I have 9 import forms and they all look similar, a datagridview with 4 buttons: Update, Import From Access, Import From Excel, and Cancel.

The datagridview (DGV) is so the user can see the data before it goes into the database.

I got smart and instead of creating 9 forms manually, I decided to build them with a class. The only difference in the forms is the headers in the DGV. So I built one in the designer and used that to guide my InputForm Class. However, when I go to run this, it garbles the buttons. Here is the Form Code: '''

    Private Sub formInput_Load(sender As Object, e As EventArgs) Handles Me.Load
    Me.btUpdate = New System.Windows.Forms.Button()
    Me.btImportAccessFile = New System.Windows.Forms.Button()
    Me.btImportExcelFile = New System.Windows.Forms.Button()
    Me.btCancel = New System.Windows.Forms.Button()

    CType(Me.dgvImportData, System.ComponentModel.ISupportInitialize).BeginInit()
    Me.SuspendLayout()
    '
    'dgvImportData
    '
    Me.dgvImportData.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
    Me.dgvImportData.Location = New System.Drawing.Point(12, 35)
    Me.dgvImportData.Name = "dgvImportData"
    Me.dgvImportData.Size = New System.Drawing.Size(1446, 882)
    Me.dgvImportData.TabIndex = 0
    '
    'Update Button
    '
    Me.btUpdate.Location = New System.Drawing.Point(12, 8)
    Me.btUpdate.Name = "btUpdate"
    Me.btUpdate.Size = New System.Drawing.Size(144, 21)
    Me.btUpdate.TabIndex = 1
    Me.btUpdate.Text = "Update"
    Me.btUpdate.UseVisualStyleBackColor = True
    '
    'Import Access Button
    '
    Me.btImportAccessFile.Location = New Point(btUpdate.Left + 150, 8)
    Me.btImportAccessFile.Name = "btImportAccessFile"
    Me.btImportAccessFile.Size = New System.Drawing.Size(144, 21)
    Me.btImportAccessFile.TabIndex = 2
    Me.btImportAccessFile.Text = "Import Access File..."
    Me.btImportAccessFile.UseVisualStyleBackColor = True
    '
    'Import Excel button
    '
    Me.btImportAccessFile.Location = New Point(btImportAccessFile.Left + 150, 8)
    Me.btImportAccessFile.Name = "btImportExcelFile"
    Me.btImportAccessFile.Size = New System.Drawing.Size(144, 21)
    Me.btImportAccessFile.TabIndex = 3
    Me.btImportExcelFile.Text = "Import Excel File..."
    Me.btImportAccessFile.UseVisualStyleBackColor = True
    '
    'Cancel Button
    '
    Me.btImportAccessFile.Location = New System.Drawing.Point(btImportExcelFile.Left + 150, 8)
    Me.btImportAccessFile.Name = "btCancel"
    Me.btImportAccessFile.Size = New System.Drawing.Size(144, 21)
    Me.btImportAccessFile.TabIndex = 4
    Me.btCancel.Text = "Cancel"
    Me.btImportAccessFile.UseVisualStyleBackColor = True

    Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    Me.ClientSize = New System.Drawing.Size(1470, 929)
    Me.Controls.Add(Me.btUpdate)
    Me.Controls.Add(Me.btImportAccessFile)
    Me.Controls.Add(Me.btImportExcelFile)
    Me.Controls.Add(Me.btCancel)
    Me.Controls.Add(Me.dgvImportData)
    Me.Name = "formInput"
    Me.Text = "Import Window"
    CType(Me.dgvImportData, System.ComponentModel.ISupportInitialize).EndInit()
    Me.ResumeLayout(False)

End Sub

'''

It is called in the main form by a button click (different buttons have different header names):

'''

    Private Sub btImpRtC_Click(sender As Object, e As EventArgs) Handles btImpRtC.Click
    Dim inpForm = New formINPUT()
    inpForm.SetupDGV("Heading A", "Heading B", "Heading C")
    inpForm.Show()
    End Sub

'''

The third and fourth buttons are jumbled and the first two buttons are running into one another. Everything looks ok in the manually designed forms and the placement values are based on that. The DGV looks fine and updates depending on what I name the headers. Is there a setting I'm missing that should be in a code built form that is inherent in a designer built form?

M. Hump
  • 5
  • 4
  • 1
    Remove these things: `(...) New Point(btUpdate.Left + 150, 8)`. Specify absolute positions. You could also build a Form Template and inherit your Forms from this *blue print*. So you can use the designer and just modify the DGV's headers where needed, at run-time. Unrelated: I suggest to set `AutoScaleMode = Dpi` and remove `Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)` – Jimi Dec 16 '19 at 19:46
  • I originally had absolutes but these didn't work either which caused me to try relative positions. I'll work with your advice on the other items. Thanks. – M. Hump Dec 17 '19 at 18:50
  • Also consider to make your app DpiAware if you haven't. Add an `app.Manifest` and make the modifications shown [here](https://stackoverflow.com/a/13228495/7444103). – Jimi Dec 17 '19 at 18:54

0 Answers0