- PR -

[ShowDialog]フォームのオープン後、自分自身を閉じたい

投稿者投稿内容
Jitta
ぬし
会議室デビュー日: 2002/07/05
投稿数: 6267
お住まい・勤務地: 兵庫県・海手
投稿日時: 2004-11-11 22:41
 こういうときこそ、IDE上で考えるのではなく、紙の上で考えます。

 えっと、ざっとしか読んでいないのですが、

A:結果画面
B:入力画面

とすると、AからBを表示し、Bで入力後、Aに入力を反映させる、でいいですか?
 これでいいとして話を進めます。

 Aが表示されるときの処理を考えると、「画面上のアイテムを、初期値で描画する」という処理が必要。Bから復帰したときには、「画面上のアイテムを、入力で描画する」という処理が必要。この辺の動作を、ぶつ切りフローチャート(元々フローチャートは「イベント」という考え方がないので、イベントの発生を端とするフローチャートを描く)やUMLのステートチャート、シーケンス図あたりを使って描きます。すると、「描画する」するというルーチンが同じであることがわかります。なので、このルーチンを抜き出し、別のメソッドとして定義します。Togetherが入っているならメソッド抜き出しをすればいいし、入っていないならメソッドを定義して、カット&ペースとします。

 ということで、紙ベースで考えることも必要、ということでした。
  
_________________
masa
常連さん
会議室デビュー日: 2003/04/16
投稿数: 38
投稿日時: 2004-11-12 00:22
よく読んでいないのでもしかしたら見当外れかもしれませんが。
以下のようなコードで、メインフォームとサブフォームを切り替えることができます。
ご参考まで。
コード:
Public Class MyApp
    Public Shared context As MyAppContext = New MyAppContext

    Shared Sub Main()
        context.AddForm(New MainForm)
        Application.Run(context)
    End Sub
End Class

Public Class MyAppContext
    Inherits ApplicationContext
    Private myForms As ArrayList

    Public Sub New()
        myForms = New ArrayList
    End Sub

    Public Sub AddForm(ByVal form As Form)
        If myForms.Contains(form) Then
            Return
        End If
        myForms.Add(form)
        If myForms.Count = 1 Then
            MyBase.MainForm = form
        End If
        AddHandler form.Activated, AddressOf Context_Activated
        AddHandler form.Closed, AddressOf Context_Closed
    End Sub

    Private Sub Context_Activated(ByVal sender As Object, ByVal e As EventArgs)
        MyBase.MainForm = CType(sender, Form)
    End Sub

    Private Sub Context_Closed(ByVal sender As Object, ByVal e As EventArgs)
        myForms.Remove(sender)
        If (CType(sender, Form) Is MyBase.MainForm) AndAlso (Me.myForms.Count > 0) Then
            Me.MainForm = CType(myForms(0), Form)
        End If
    End Sub
End Class

Public Class MainForm
    Inherits System.Windows.Forms.Form
    Private button1 As System.Windows.Forms.Button
    Private components As System.ComponentModel.Container = Nothing

    Public Sub New()
        InitializeComponent()
        MyApp.context.AddForm(Me)
    End Sub

    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

    Private Sub InitializeComponent()
        Me.button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        Me.button1.Location = New System.Drawing.Point(88, 104)
        Me.button1.Name = "button1"
        Me.button1.TabIndex = 0
        Me.button1.Text = "SubFormを表示"
        AddHandler Me.button1.Click, AddressOf Me.button1_Click
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.button1)
        Me.Name = "MainForm"
        Me.Text = "MainForm"
        Me.ResumeLayout(False)
    End Sub

    Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim newSubForm As Form = New SubForm
        newSubForm.Show()
        Me.Close()
    End Sub
End Class

Public Class SubForm
    Inherits System.Windows.Forms.Form
    Private button1 As System.Windows.Forms.Button
    Private components As System.ComponentModel.Container = Nothing
    Private Shared counter As Integer = 0

    Public Sub New()
        InitializeComponent()
        MyApp.context.AddForm(Me)
    End Sub

    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

    Private Sub InitializeComponent()
        Me.button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        Me.button1.Location = New System.Drawing.Point(88, 104)
        Me.button1.Name = "button1"
        Me.button1.TabIndex = 0
        Me.button1.Text = "MainFormを表示"
        AddHandler Me.button1.Click, AddressOf Me.button1_Click
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.button1)
        Me.Name = "SubForm"
        Me.Text = "SubForm"
        Me.ResumeLayout(False)
    End Sub

    Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim newMainForm As Form = New MainForm
        newMainForm.Show()
        Me.Close()
    End Sub
End Class

まっちー
会議室デビュー日: 2004/10/22
投稿数: 15
お住まい・勤務地: 浜松町
投稿日時: 2004-11-12 09:54
Jittaさん、ありがとうございます。
私もSEのはしくれですので、しっかりフローにまとめてから実装にはいるよう心がけます。

masaさん、ありがとうございました。
「ShowDialog」ではなく、「Show」を使えばよいということですね?
微調整は必要ですが、うまくいきそうです。
MSDNなどで調べてみましたが、モーダルとモードレスをうまく使い分けられるようにしたいと思います。

みなさま、いろいろとありがとうございました。

_________________

スキルアップ/キャリアアップ(JOB@IT)