Avatar billede tjacob Juniormester
28. juni 2009 - 12:55 Der er 5 kommentarer og
1 løsning

Initialize kode i form

Jeg har en windowsform hvori der skal køres noget initialisering.
Det har jeg hidtil gjort i Form_Load eventet, og det har fungeret fint.

Nu skal jeg imidlertid have noget kode kørt i forbindelse med at bruger resizer formen. Enten ved at trække i en kant eller ved maximer/minimer knapperne.

I denne kode indgår et par globale variable, der bliver sat i initialiseringen i Form_Load eventet.

Mit problem er, at ved opstart bliver alle resize events kørt FØR Form_Load, og dermed får jeg fejl.

Jeg ser to løsninger:

1. Hvordan får jeg min initialisering kørt FØR nogen resize events bliver kaldt?

eller:
2. Hvordan tester jeg på de globale variable i resize-eventet? (koden behøver nemlig ikke køres i forbindelse med opstart). Jeg har f.eks. prøvet at teste længden på et array, men det fungerer ikke...
Avatar billede hmortensen Nybegynder
28. juni 2009 - 12:57 #1
Du kan bruge constructoren i stedet for Form_load eventen.
Avatar billede tjacob Juniormester
28. juni 2009 - 13:11 #2
OK, men hvor?
Jeg har lavet det hele i IDE form designeren, og jeg kan ikke umiddelbart finde konstructoren. Min formdesigner kode ser sådan ud:


<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class frmMain
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer. 
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.MainMenu = New System.Windows.Forms.MenuStrip
        Me.mnuFilesTop = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuFiles1 = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuFiles2 = New System.Windows.Forms.ToolStripMenuItem
        Me.tsSep1 = New System.Windows.Forms.ToolStripSeparator
        Me.mnuFiles3 = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuFiles4 = New System.Windows.Forms.ToolStripMenuItem
        Me.tsSep2 = New System.Windows.Forms.ToolStripSeparator
        Me.mnuFiles5 = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuGameTop = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuGame1 = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuGame2 = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuSettingsTop = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuSettings10 = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuEditorTop = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuSolverTop = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuHelpTop = New System.Windows.Forms.ToolStripMenuItem
        Me.panBoard = New System.Windows.Forms.Panel
        Me.picBoard = New System.Windows.Forms.PictureBox
        Me.panInfo = New System.Windows.Forms.Panel
        Me.MainMenu.SuspendLayout()
        Me.panBoard.SuspendLayout()
        CType(Me.picBoard, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'MainMenu
        '
        Me.MainMenu.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.mnuFilesTop, Me.mnuGameTop, Me.mnuSettingsTop, Me.mnuEditorTop, Me.mnuSolverTop, Me.mnuHelpTop})
        Me.MainMenu.Location = New System.Drawing.Point(0, 0)
        Me.MainMenu.Name = "MainMenu"
        Me.MainMenu.Size = New System.Drawing.Size(982, 27)
        Me.MainMenu.TabIndex = 0
        Me.MainMenu.Text = "MenuStrip1"
        '
        'mnuFilesTop
        '
        Me.mnuFilesTop.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.mnuFiles1, Me.mnuFiles2, Me.tsSep1, Me.mnuFiles3, Me.mnuFiles4, Me.tsSep2, Me.mnuFiles5})
        Me.mnuFilesTop.Name = "mnuFilesTop"
        Me.mnuFilesTop.Size = New System.Drawing.Size(47, 23)
        Me.mnuFilesTop.Text = "Files"
        '
        'mnuFiles1
        '
        Me.mnuFiles1.Name = "mnuFiles1"
        Me.mnuFiles1.Size = New System.Drawing.Size(151, 24)
        Me.mnuFiles1.Text = "test sub 1"
        '
        'mnuFiles2
        '
        Me.mnuFiles2.Name = "mnuFiles2"
        Me.mnuFiles2.Size = New System.Drawing.Size(151, 24)
        Me.mnuFiles2.Text = "test sub 2"
        '
        'tsSep1
        '
        Me.tsSep1.Name = "tsSep1"
        Me.tsSep1.Size = New System.Drawing.Size(148, 6)
        '
        'mnuFiles3
        '
        Me.mnuFiles3.Name = "mnuFiles3"
        Me.mnuFiles3.Size = New System.Drawing.Size(151, 24)
        Me.mnuFiles3.Text = "test sub 3"
        '
        'mnuFiles4
        '
        Me.mnuFiles4.Name = "mnuFiles4"
        Me.mnuFiles4.Size = New System.Drawing.Size(151, 24)
        Me.mnuFiles4.Text = "test sub 4"
        '
        'tsSep2
        '
        Me.tsSep2.Name = "tsSep2"
        Me.tsSep2.Size = New System.Drawing.Size(148, 6)
        '
        'mnuFiles5
        '
        Me.mnuFiles5.Name = "mnuFiles5"
        Me.mnuFiles5.ShortcutKeys = CType((System.Windows.Forms.Keys.Control Or System.Windows.Forms.Keys.Q), System.Windows.Forms.Keys)
        Me.mnuFiles5.Size = New System.Drawing.Size(151, 24)
        Me.mnuFiles5.Text = "Exit"
        '
        'mnuGameTop
        '
        Me.mnuGameTop.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.mnuGame1, Me.mnuGame2})
        Me.mnuGameTop.Name = "mnuGameTop"
        Me.mnuGameTop.Size = New System.Drawing.Size(57, 23)
        Me.mnuGameTop.Text = "Game"
        '
        'mnuGame1
        '
        Me.mnuGame1.Name = "mnuGame1"
        Me.mnuGame1.Size = New System.Drawing.Size(177, 24)
        Me.mnuGame1.Text = "Select Collection"
        '
        'mnuGame2
        '
        Me.mnuGame2.Name = "mnuGame2"
        Me.mnuGame2.Size = New System.Drawing.Size(177, 24)
        Me.mnuGame2.Text = "Select Level"
        '
        'mnuSettingsTop
        '
        Me.mnuSettingsTop.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.mnuSettings10})
        Me.mnuSettingsTop.Name = "mnuSettingsTop"
        Me.mnuSettingsTop.Size = New System.Drawing.Size(70, 23)
        Me.mnuSettingsTop.Text = "Settings"
        '
        'mnuSettings10
        '
        Me.mnuSettings10.Name = "mnuSettings10"
        Me.mnuSettings10.Size = New System.Drawing.Size(165, 24)
        Me.mnuSettings10.Text = "Open Settings"
        '
        'mnuEditorTop
        '
        Me.mnuEditorTop.Name = "mnuEditorTop"
        Me.mnuEditorTop.Size = New System.Drawing.Size(57, 23)
        Me.mnuEditorTop.Text = "Editor"
        '
        'mnuSolverTop
        '
        Me.mnuSolverTop.Name = "mnuSolverTop"
        Me.mnuSolverTop.Size = New System.Drawing.Size(58, 23)
        Me.mnuSolverTop.Text = "Solver"
        '
        'mnuHelpTop
        '
        Me.mnuHelpTop.Name = "mnuHelpTop"
        Me.mnuHelpTop.Size = New System.Drawing.Size(49, 23)
        Me.mnuHelpTop.Text = "Help"
        '
        'panBoard
        '
        Me.panBoard.AutoScroll = True
        Me.panBoard.Controls.Add(Me.picBoard)
        Me.panBoard.Location = New System.Drawing.Point(633, 109)
        Me.panBoard.Name = "panBoard"
        Me.panBoard.Size = New System.Drawing.Size(193, 201)
        Me.panBoard.TabIndex = 1
        '
        'picBoard
        '
        Me.picBoard.Location = New System.Drawing.Point(0, 0)
        Me.picBoard.Name = "picBoard"
        Me.picBoard.Size = New System.Drawing.Size(106, 98)
        Me.picBoard.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.picBoard.TabIndex = 0
        Me.picBoard.TabStop = False
        '
        'panInfo
        '
        Me.panInfo.Dock = System.Windows.Forms.DockStyle.Left
        Me.panInfo.Location = New System.Drawing.Point(0, 27)
        Me.panInfo.Name = "panInfo"
        Me.panInfo.Size = New System.Drawing.Size(200, 582)
        Me.panInfo.TabIndex = 2
        '
        'frmMain
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 18.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None
        Me.ClientSize = New System.Drawing.Size(982, 609)
        Me.Controls.Add(Me.panInfo)
        Me.Controls.Add(Me.panBoard)
        Me.Controls.Add(Me.MainMenu)
        Me.Font = New System.Drawing.Font("Trebuchet MS", 9.163636!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.KeyPreview = True
        Me.MainMenuStrip = Me.MainMenu
        Me.Margin = New System.Windows.Forms.Padding(4)
        Me.MinimumSize = New System.Drawing.Size(1000, 650)
        Me.Name = "frmMain"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = " 5Soft Testting"
        Me.MainMenu.ResumeLayout(False)
        Me.MainMenu.PerformLayout()
        Me.panBoard.ResumeLayout(False)
        Me.panBoard.PerformLayout()
        CType(Me.picBoard, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub



    Friend WithEvents MainMenu As System.Windows.Forms.MenuStrip
    Friend WithEvents mnuFilesTop As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuGameTop As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuSettingsTop As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuEditorTop As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuSolverTop As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuHelpTop As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuFiles1 As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuFiles2 As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents tsSep1 As System.Windows.Forms.ToolStripSeparator
    Friend WithEvents mnuFiles3 As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuFiles4 As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents tsSep2 As System.Windows.Forms.ToolStripSeparator
    Friend WithEvents mnuFiles5 As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuGame1 As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuGame2 As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents panBoard As System.Windows.Forms.Panel
    Friend WithEvents picBoard As System.Windows.Forms.PictureBox
    Friend WithEvents mnuSettings10 As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents panInfo As System.Windows.Forms.Panel

End Class
Avatar billede tjacob Juniormester
28. juni 2009 - 13:30 #3
Ahh, jeg misforstod.

Har fundet løsningen, jeg har aldrig brugt New() før........

Læg et svar hmortensen.
Avatar billede tjacob Juniormester
28. juni 2009 - 13:54 #4
-Det løste dog ikke mit problem, da resize_eventen åbenbart bliver kørt allerede i InitializeComponent() i konstruktoren.


Jeg har nu løst det ved at køre initialiseringen i Resize_eventet med et flag der bliver sat første gang.
Avatar billede tjacob Juniormester
30. juni 2009 - 14:20 #5
Hvad så hmortensen? Skal du ikke have dine points?

Selvom det ikke direkte løste mit problem, så lærte jeg noget nyt, så læg et svar.
Avatar billede hmortensen Nybegynder
30. juni 2009 - 20:23 #6
Kommer her :)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester