Kirjautuminen

Haku

Tehtävät

Keskustelu: Koodit: VB.NET: MDI projektin alku

tnb [22.03.2004 00:50:29]

#

MDI projektissa pää-ikkunalla on lapsi-ikkunoita, joita hallitaan Window valikon kautta. Oheiset listaukset sisältävät esimerkin MDI projektin alusta (tekstin käsittely).
Form1 = pääikkuna
Form2 = malli lapsi-ikkunasta, sisältää richtextbox1

Tässä ovat keskeiset lauseet (sisältyvät myös seuraavaan esimerkkikoodiin)

'Form load tapahtuma
        'pääformi määritellään sisältäväksi aliformeja
        Me.IsMdiContainer = True
        'Window menu laitetaan näyttämään lapsi-formeja
        MainMenu1.MenuItems(2).MdiList = True 'Window menu
        ' Lapsi-formit  maksimikokoon
        myMDIChild1.WindowState = FormWindowState.Maximized
        myMDIChild2.WindowState = FormWindowState.Maximized
        myMDIChild3.WindowState = FormWindowState.Maximized
        'Kerrotaan lapselle kuka on äitinsä
        myMDIChild1.MdiParent = Me
        myMDIChild2.MdiParent = Me
        myMDIChild3.MdiParent = Me
        'Kirjoitetaan niihin
        myMDIChild1.RichTextBox1.Text = "Tämä on lapsi-ikkuna 1"
        myMDIChild2.RichTextBox1.Text = "Tämä on lapsi-ikkuna 2"
        myMDIChild3.RichTextBox1.Text = "Tämä on lapsi-ikkuna 3"
        'Lopuksi näytetään ne
        myMDIChild3.Show()
        myMDIChild2.Show()
        myMDIChild1.Show()

Tässä on kokonainen esimerkki:

Form1

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem7 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem8 As System.Windows.Forms.MenuItem
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.MainMenu1 = New System.Windows.Forms.MainMenu
        Me.MenuItem1 = New System.Windows.Forms.MenuItem
        Me.MenuItem3 = New System.Windows.Forms.MenuItem
        Me.MenuItem4 = New System.Windows.Forms.MenuItem
        Me.MenuItem5 = New System.Windows.Forms.MenuItem
        Me.MenuItem2 = New System.Windows.Forms.MenuItem
        Me.MenuItem6 = New System.Windows.Forms.MenuItem
        Me.MenuItem7 = New System.Windows.Forms.MenuItem
        Me.MenuItem8 = New System.Windows.Forms.MenuItem
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem3, Me.MenuItem2})
        '
        'MenuItem1
        '
        Me.MenuItem1.Index = 0
        Me.MenuItem1.Text = "&File"
        '
        'MenuItem3
        '
        Me.MenuItem3.Index = 1
        Me.MenuItem3.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem4, Me.MenuItem5})
        Me.MenuItem3.Text = "&Edit"
        '
        'MenuItem4
        '
        Me.MenuItem4.Index = 0
        Me.MenuItem4.Text = "Copy"
        '
        'MenuItem5
        '
        Me.MenuItem5.Index = 1
        Me.MenuItem5.Text = "Paste"
        '
        'MenuItem2
        '
        Me.MenuItem2.Index = 2
        Me.MenuItem2.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem6, Me.MenuItem7, Me.MenuItem8})
        Me.MenuItem2.Text = "&Window"
        '
        'MenuItem6
        '
        Me.MenuItem6.Index = 0
        Me.MenuItem6.Text = "Cascade"
        '
        'MenuItem7
        '
        Me.MenuItem7.Index = 1
        Me.MenuItem7.Text = "TileHorizontal"
        '
        'MenuItem8
        '
        Me.MenuItem8.Index = 2
        Me.MenuItem8.Text = "TileVertical"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(424, 286)
        Me.Menu = Me.MainMenu1
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region
    Dim myMDIChild3 As New Form2
    Dim myMDIChild2 As New Form2
    Dim myMDIChild1 As New Form2
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'pääformi määritellään sisältäväksi aliformeja
        Me.IsMdiContainer = True
        'Window menu laitetaan näyttämään lapsi-formeja
        MainMenu1.MenuItems(2).MdiList = True 'Window menu
        ' Lapsi-formit  maksimikokoon
        myMDIChild1.WindowState = FormWindowState.Maximized
        myMDIChild2.WindowState = FormWindowState.Maximized
        myMDIChild3.WindowState = FormWindowState.Maximized
        'Kerrotaan lapselle kuka on äitinsä
        myMDIChild1.MdiParent = Me
        myMDIChild2.MdiParent = Me
        myMDIChild3.MdiParent = Me
        'Kirjoitetaan niihin
        myMDIChild1.RichTextBox1.Text = "Tämä on lapsi-ikkuna 1"
        myMDIChild2.RichTextBox1.Text = "Tämä on lapsi-ikkuna 2"
        myMDIChild3.RichTextBox1.Text = "Tämä on lapsi-ikkuna 3"
        'Lopuksi näytetään ne
        myMDIChild3.Show()
        myMDIChild2.Show()
        myMDIChild1.Show()
    End Sub

    Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
        'kopioi leikepöydälle
        Dim ActiveRtf As RichTextBox = Me.ActiveMdiChild.ActiveControl
        Clipboard.SetDataObject(ActiveRtf.SelectedText)
    End Sub

    Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem5.Click
        'Liitä leikepöydältä aktiiviseen lapsi-ikkunaan
        Dim ActiveRtf As RichTextBox = Me.ActiveMdiChild.ActiveControl
        Dim data As IDataObject = Clipboard.GetDataObject()
        If (Data.GetDataPresent(DataFormats.Text)) Then
            ActiveRtf.SelectedText = data.GetData(DataFormats.Text).ToString()
        End If

    End Sub

    Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click
        Me.LayoutMdi(System.Windows.Forms.MdiLayout.Cascade)
    End Sub

    Private Sub MenuItem7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem7.Click
        Me.LayoutMdi(System.Windows.Forms.MdiLayout.TileHorizontal)
    End Sub

    Private Sub MenuItem8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem8.Click
        Me.LayoutMdi(System.Windows.Forms.MdiLayout.TileVertical)
    End Sub
End Class

Form2

Public Class Form2
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.
    Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
        Me.SuspendLayout()
        '
        'RichTextBox1
        '
        Me.RichTextBox1.Location = New System.Drawing.Point(0, 0)
        Me.RichTextBox1.Name = "RichTextBox1"
        Me.RichTextBox1.Size = New System.Drawing.Size(288, 264)
        Me.RichTextBox1.TabIndex = 0
        Me.RichTextBox1.Text = "RichTextBox1"
        '
        'Form2
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.RichTextBox1)
        Me.Name = "Form2"
        Me.Text = "Form2"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form2_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        Me.RichTextBox1.Width = Me.Width - 10
        Me.RichTextBox1.Width = Me.Width - 10
        Me.RichTextBox1.Width = Me.Width - 10
        Me.RichTextBox1.Height = Me.Height - 35
        Me.RichTextBox1.Height = Me.Height - 35
        Me.RichTextBox1.Height = Me.Height - 35
    End Sub
End Class

sooda [24.03.2004 13:27:16]

#

Näyttää vähän vaikeammalta kuin normaalissa vb:ssä :D

Monkkats [24.03.2004 15:28:27]

#

Todella vaikeammalta!!!

peki [24.03.2004 15:40:21]

#

No ei se nyt kamalan paljon vaikeampaa ole.

Nuo "Windows Form Designer Generated Code" alueet
luodaan automaattisesti.

johan123 [29.12.2004 11:33:47]

#

Vaikeeta ja vaikeeta, melkein kaiken olis voinut ttehdä properties valikosta
-Is MDI container
ja sen jälkeen vain asettaa mdiparent ominaisuus lapsi formeille, ihan hyvin toimii ja sästyy paljon vaivaa.

Vastaus

Aihe on jo aika vanha, joten et voi enää vastata siihen.

Tietoa sivustosta