-
[RESOLVED] I need to non-modal form to act modally
All of my forms are MDI children. I have one form that is collecting input parameters for a report. One of the parameters needs to come from another form. For the life of me, I can't get it to work.
Code:
If Trim(Vendor) = "Y" Then
Else
'------------must select a vendor
glbPart = "GetAVendor"
frmVendorList.Show()
'-- we now have something in glbvendor
MsgBox(glbVendor)
End If
I have to message box code just to verify the selection. glbPart is just a hand off parameter to tell the frmVendorList code to come back to this form rather than where it was origianlly programmed to go.
I tried a do until glbVendor <> "", but that put me into a death loop and I never got to select a vendor from the vendor list form. Obviously, the easiest would be if I could make frmVendorList Modal, but I can't see how to do that once it's been created modally.
Any ideas?
-
Re: I need to non-modal form to act modally
Can you just add a vendor list drop down to the form gathering the parameters rather than having the parameter gathering form open up the vendor list form?
-
Re: I need to non-modal form to act modally
ShowDialog() displays a form modally. Use that instead of Show().
-
Re: I need to non-modal form to act modally
Hack: The code asks a series of questions. One of them is 'one vendor or all'. If they choose 'all', no problem. But, if the choose a specific vendor, I need to give them a form listing all the vendors. I have one (frmvendorlist). The problem is, I call it and the code in the calling form continues merrily along. I need to have it stop until it gets a value from the other form.
Sitten: I can't use ShowDialog() on a child form. Already tried it.
-
Re: I need to non-modal form to act modally
If they choose vendor why do you need to give them a form? All they are choosing is vendor name right? Can't you populate a dropdown with that?
-
Re: I need to non-modal form to act modally
Your non-modal form may not be able to show a modal form, but it CAN call a method on the MDI parent that shows a modal form and returns the response. It would be the same as having it call a method in a module that shows a modal form and returns something, except that by having the method be part of the parent form it would be more in keeping with the spirit of object orientation.
-
Re: I need to non-modal form to act modally
HACK: I am using input boxes
Code:
Vendor = InputBox("Select all vendors, [Y] or [N]?", , "Y")
If Trim(Vendor) = "" Then Exit Sub
Vendor = Vendor.ToUpper
Shaggy: I bow to your knowledge, as it far exceeds mine. I will try to understand what you just said. :-)
-
Re: I need to non-modal form to act modally
The fact that it didn't allow you to call ShowDialog on the form frmVendorList is because you're using the default form instance which has been set to be a mid child form. The solution is quite simple: create a new instance of the frmVendorList form and showdialog it.
Code:
If Trim(Vendor) = "Y" Then
Else
'------------must select a vendor
glbPart = "GetAVendor"
Dim vendorLst as New frmVendorList()
If vendorLst.ShowDialog() = Windows.Forms.DialogResult.OK Then
'-- we now have something in glbvendor
MsgBox(glbVendor)
End If
End If
-
Re: I need to non-modal form to act modally
I still get this error:
Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog.
-
Re: I need to non-modal form to act modally
Quote:
Originally Posted by
Pasvorto
I still get this error:
Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog.
Did you do as shown in my last post (creating a new instance of the form)?
-
Re: I need to non-modal form to act modally
-
Re: I need to non-modal form to act modally
If this was still in VB6 I would just make the form a non-child. I don't see how to do that in .NET
-
Re: I need to non-modal form to act modally
I bet there's some code that's running in the constructor that's attaching it to the mdi parent...
-tg
-
Re: I need to non-modal form to act modally
Do you set the form mdiParent property at form load? In other words, how do you display frmVendorList as an mid child form from the mdi parent form?
-
Re: I need to non-modal form to act modally
Quote:
Originally Posted by
techgnome
I bet there's some code that's running in the constructor that's attaching it to the mdi parent...
-tg
That's what I'm suspecting too.
-
Re: I need to non-modal form to act modally
I am not doing anything consciously. I think when I created it, it was created as a child.
-
Re: I need to non-modal form to act modally
Found this:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> Partial Class frmVendorList
#Region "Windows Form Designer generated code "
<System.Diagnostics.DebuggerNonUserCode()> Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'This form is an MDI child.
'This code simulates the VB6
' functionality of automatically
' loading and showing an MDI
' child's parent.
Me.MDIParent = VENDOR.MDIForm1
VENDOR.MDIForm1.Show
End Sub
-
Re: I need to non-modal form to act modally
I commented these out and it seems to work
Me.MDIParent = VENDOR.MDIForm1
VENDOR.MDIForm1.Show
Are there any traps to doing that?
-
Re: I need to non-modal form to act modally
Can you show the constructor of frmVendorList class and probably the frmVendorList_Load event hander too (if it has one)? What you described is not the normal, default behavior of forms in .Net. There must be code somewhere that change the default behavior to what it is now... We need to find out where that code is in order to find a way to work with it.
-
Re: I need to non-modal form to act modally
Code:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> Partial Class frmVendorList
#Region "Windows Form Designer generated code "
<System.Diagnostics.DebuggerNonUserCode()> Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'This form is an MDI child.
'This code simulates the VB6
' functionality of automatically
' loading and showing an MDI
' child's parent.
Me.MdiParent = VENDOR.MDIForm1
VENDOR.MDIForm1.Show()
End Sub
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> 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
Public ToolTip1 As System.Windows.Forms.ToolTip
Public WithEvents fGrid1 As AxMSFlexGridLib.AxMSFlexGrid
'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.components = New System.ComponentModel.Container
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(frmVendorList))
Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
Me.fGrid1 = New AxMSFlexGridLib.AxMSFlexGrid
CType(Me.fGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'fGrid1
'
Me.fGrid1.Location = New System.Drawing.Point(8, 5)
Me.fGrid1.Name = "fGrid1"
Me.fGrid1.OcxState = CType(resources.GetObject("fGrid1.OcxState"), System.Windows.Forms.AxHost.State)
Me.fGrid1.Size = New System.Drawing.Size(423, 219)
Me.fGrid1.TabIndex = 1
'
'frmVendorList
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 14.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.BackColor = System.Drawing.SystemColors.Control
Me.ClientSize = New System.Drawing.Size(441, 236)
Me.Controls.Add(Me.fGrid1)
Me.Cursor = System.Windows.Forms.Cursors.Default
Me.Font = New System.Drawing.Font("Arial", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.Location = New System.Drawing.Point(134, 104)
Me.MaximizeBox = False
Me.Name = "frmVendorList"
Me.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Vendor List"
CType(Me.fGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
End Class
-
Re: I need to non-modal form to act modally
Quote:
Originally Posted by
Pasvorto
I commented these out and it seems to work
Me.MDIParent = VENDOR.MDIForm1
VENDOR.MDIForm1.Show
Are there any traps to doing that?
There it is... Yes, you can and should take out those lines. Basically it set the mdiparent of the form as soon as an instance of the form is created. You don't want that. You can always set it manually when you want to, which affects only the current instance that you're working on.
Since you take that code out of the contructor, you will need to assign the mid parent to the form whenever you want to make it an mid child form.
-
Re: I need to non-modal form to act modally
Is this what you mean?
Code:
Private Sub frmVendorList_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Try
Me.Width = 447
Me.Height = 261
'set grid headings
fGrid1.Row = 0
fGrid1.Col = 0
fGrid1.Text = "Account #"
fGrid1.set_ColWidth(0, 1000)
fGrid1.set_ColAlignment(0, 1)
fGrid1.Col = 1
fGrid1.Text = "Vendor"
fGrid1.set_ColWidth(1, 3500)
fGrid1.Redraw = False
'
'now fill grid
'
Using cnxn As New SqlClient.SqlConnection(cnxnstring)
cnxn.Open()
Using sql As New SqlClient.SqlCommand("SELECT [ACCOUNT],[NAME] FROM VENDOR ORDER BY [NAME]")
sql.Connection = cnxn
sql.CommandType = CommandType.Text
Dim reader As SqlDataReader
reader = sql.ExecuteReader
Using reader
If reader.HasRows Then
fGrid1.Row = 1
While reader.Read
fGrid1.Col = 0
fGrid1.Text = reader("ACCOUNT") & ""
fGrid1.Col = 1
fGrid1.Text = reader("Name") & ""
If Trim(fGrid1.Text) <> "" Then
fGrid1.Rows = fGrid1.Rows + 1
fGrid1.Row = fGrid1.Row + 1
End If
End While
End If
End Using
End Using
End Using
'===============
If fGrid1.Rows > 2 Then fGrid1.Rows = fGrid1.Rows - 1
fGrid1.Redraw = True
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
Catch ex As Exception
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
MsgBox("An error occurred: [" & Err.Number & "] " & Err.Description & " - Source: (frmVendorList)Form_Load")
End Try
End Sub
-
Re: I need to non-modal form to act modally
Cool. Thanks guys. Someday, my VB6 head will get wrapped around this VB.Net code :-)
-
Re: [RESOLVED] I need to non-modal form to act modally
The frmVendorList_Load event handler looks fine. No problem there.
-
Re: [RESOLVED] I need to non-modal form to act modally
By the way, try to stay away from default form instances... Although they are convenient, they create more headaches than they're worth. Just create an instance of a form manually when you want to, it only takes 1 extra line of code yet you can avoid many potential problems in the long run, especially when your app is multi-threaded.
So in stead of doing this to show Form1
You should always create an instance of Form1 yourself and show it as below
Code:
Dim frm1 as New Form1
frm1.Show()
-
Re: [RESOLVED] I need to non-modal form to act modally
doing what? setting the mdi parent in the constructor? Yes... as you just found out... also you're using the default instance of the mdiparent...
I'd make two constructors... one parameter-less that calls initialize component, mybase and all that... and a second that takes a parameter of type form... calls mybase, initialize components, then sets the mdi parent property to the passed in form instance... and that's it... I wouldn't even let it show the parent form... it should already be showing.
-tg
edit - and that's what I get for leaving the window open too long before hitting reply....
-
Re: [RESOLVED] I need to non-modal form to act modally
So much to learn... so little grey matter to learn with :-P