Results 1 to 11 of 11

Thread: Have a modal form but be able to access another already open form

  1. #1

    Thread Starter
    Hyperactive Member Dasiths's Avatar
    Join Date
    Apr 2001
    Location
    Colombo, Sri Lanka
    Posts
    331

    Angry Have a modal form but be able to access another already open form

    Hi,

    I know this sound strange but here is my scenario.

    I have designed my app for multiple gridviews. When a user double clicks a row it opens up the data in a modal form and when they commit changes the row gets updated.

    While they have that modal window open they can not go back to the grid.

    But I have a alerts/messages window always running (like outlook tasks). As soon as I open the modal window I can not access that tasks window. (Default behaviour)

    How can I open the modal form and still let the user access/view the tasks window while they work on the modal window.

    (I tried creating it in a differant thread, but got a lot of cross thread errors so decided to abandon that approach.

    I thought that 2 modal form can run at the same time as long as they have differant parents. But aparently the main GUI thread pauses as soon as you open a modal window on it.

    p.s. I use LINQ-SQL entity objects.
    It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject admits, and not to seek exactness when only an approximation of the truth is possible.
    -Aristotle As quoted in Rapid Development, chapter 8, page 167.

  2. #2
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Have a modal form but be able to access another already open form

    Additional threads are the answer. The cross thread errors are normal. But instead of abandoning that approach just because you don't know how to use it, doesn't mean it's not the best method.

    Check the codebank for JMC's post on backgroundworkers and you'll find an appropriate solution.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3

    Thread Starter
    Hyperactive Member Dasiths's Avatar
    Join Date
    Apr 2001
    Location
    Colombo, Sri Lanka
    Posts
    331

    Unhappy Re: Have a modal form but be able to access another already open form

    Quote Originally Posted by weirddemon View Post
    Additional threads are the answer. The cross thread errors are normal. But instead of abandoning that approach just because you don't know how to use it, doesn't mean it's not the best method.

    Check the codebank for JMC's post on backgroundworkers and you'll find an appropriate solution.
    I've made modifications to the code so it does not throw cross-thread errors. But still running into some problems.

    I have a singleton class that creates the connection and provides the datacontext for my LINQ-SQL objects. It is called everytime I need a new datacontext. (For ex. When a new data entry form opens up and needs to populate itself with data)

    But as soon as it is accessed via a differant thread I'm getting strange behaviour.

    This is the code I have.

    Code:
    'GUITasksManager.showBusyCursor(False, Me)
    'I've commented out the above line because it causes cross thread operations
    
                db = AppTasksManager.GetSingleton.getDB 'cretate new context and return it = line 42
    
                cmbAssignedTo.Items.AddRange(db.Users.Where(Function(u) u.isInactive = False).ToArray)
                cmbAssignedTo.DisplayMember = "UserNameFull"
    This works fine when run on the same thread, but gives an error saying "Invalid Cast Opeation" on line 42 when running in a differant thread.

    the GetDB function looks like

    Code:
    Public Function getDB() As DataClassesDataContext
            Return New DataClassesDataContext(_connString)
        End Function
    Any idea why?
    Last edited by Dasiths; Jul 20th, 2010 at 07:33 PM. Reason: spell edit
    It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject admits, and not to seek exactness when only an approximation of the truth is possible.
    -Aristotle As quoted in Rapid Development, chapter 8, page 167.

  4. #4

    Thread Starter
    Hyperactive Member Dasiths's Avatar
    Join Date
    Apr 2001
    Location
    Colombo, Sri Lanka
    Posts
    331

    Re: Have a modal form but be able to access another already open form

    System.Threading.ThreadStateException was unhandled by user code
    Message="Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it."
    Source="System.Windows.Forms"
    StackTrace:
    at System.Windows.Forms.ComboBox.set_AutoCompleteMode(AutoCompleteMode value)
    at LuxtonPlant_AssetMan.frmCustomerTask.InitializeComponent() in F:\my work\Luxton Plant\LuxtonPlant_AssetMan\LuxtonPlant_AssetMan\Graphical User Interface\Data Entry Forms\Tasks\frmCustomerTask.Designer.vb:line 167
    at LuxtonPlant_AssetMan.frmCustomerTask..ctor()
    at LuxtonPlant_AssetMan.GUITasksManager.viewCustomerTask(LogEntry m, Boolean asDialog) in F:\my work\Luxton Plant\LuxtonPlant_AssetMan\LuxtonPlant_AssetMan\Application Control Classes\GUITasksManager.vb:line 1731
    at LuxtonPlant_AssetMan.frmMain.WorkerTask_DoWork(Object sender, DoWorkEventArgs e) in F:\my work\Luxton Plant\LuxtonPlant_AssetMan\LuxtonPlant_AssetMan\Graphical User Interface\Application Forms\frmMain.vb:line 180
    at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
    at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
    InnerException:


    I get that error when using the bakground worker to open a new form.
    It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject admits, and not to seek exactness when only an approximation of the truth is possible.
    -Aristotle As quoted in Rapid Development, chapter 8, page 167.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Have a modal form but be able to access another already open form

    It seems to me that, if you want this dialogue to be modal, it needs to be your alerts window that gets created on a different thread.

    You can't use a BackgroundWorker is you need an STA thread because the BackgroundWorker uses ThreadPool threads. You will need to create your own Thread explicitly, so that you can call its SetApartmentState method before calling its Start method.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Have a modal form but be able to access another already open form

    This sounds headachey. Why not create a single form with a split panel. One panel holds the grid, the other holds the edit form. By default the width of the grid panel is 100% and the edit panel is 0%. Double-clicking an entry 'brings up' the edit form by reversing the widths, like a slider. Only when the save or cancel button is pressed on the edit panel does the slider return to its original position. In this manner you are able to prevent returning to the gridview while a record is being edited, but without the complications of multiple message pumps.

  7. #7

    Thread Starter
    Hyperactive Member Dasiths's Avatar
    Join Date
    Apr 2001
    Location
    Colombo, Sri Lanka
    Posts
    331

    Re: Have a modal form but be able to access another already open form

    Quote Originally Posted by MaximilianMayrhofer View Post
    This sounds headachey. Why not create a single form with a split panel. One panel holds the grid, the other holds the edit form. By default the width of the grid panel is 100% and the edit panel is 0%. Double-clicking an entry 'brings up' the edit form by reversing the widths, like a slider. Only when the save or cancel button is pressed on the edit panel does the slider return to its original position. In this manner you are able to prevent returning to the gridview while a record is being edited, but without the complications of multiple message pumps.
    This is why.

    The alerts/tasks can be clicked and responses posted to them.

    Say the user gets an alert saying an invoice is overddue. The user clicks on the alert and brings up an window (modal) to add a response.

    While typing the response they need to go back to the main screen and browse around customer files and invoices.

    And opening a invoice will again bring up a modal window.

    See my problem?

    I'm developing a fairly complicated IS and the split panels wouldn't work in all scenarios.

    Is it worthwhile considering developing a seperate app for the alerts, or giving up letting the user go back to the main screen while they have a alert opened up?

    I'm really stuck here. All the dataentry scenarios (about 100) so far have been written in the form of (Click Object -> Open object in modal -> send response back to grid ->refresh grid as neccessary)
    It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject admits, and not to seek exactness when only an approximation of the truth is possible.
    -Aristotle As quoted in Rapid Development, chapter 8, page 167.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Have a modal form but be able to access another already open form

    As a very simple example, this worked for me:
    vb.net Code:
    1. Imports System.Threading
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         Dim t As New Thread(AddressOf ShowForm2)
    7.  
    8.         t.IsBackground = True
    9.         t.SetApartmentState(ApartmentState.STA)
    10.         t.Start()
    11.     End Sub
    12.  
    13.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    14.         Using f3 As New Form3
    15.             f3.ShowDialog()
    16.         End Using
    17.     End Sub
    18.  
    19.     Private Sub ShowForm2()
    20.         Application.Run(New Form2)
    21.     End Sub
    22.  
    23. End Class
    Obviously, any communication between Form2 and other forms would require delegation, so you might need to keep the appropriate reference(s) around to use for the purpose.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Hyperactive Member Dasiths's Avatar
    Join Date
    Apr 2001
    Location
    Colombo, Sri Lanka
    Posts
    331

    Re: Have a modal form but be able to access another already open form

    Quote Originally Posted by jmcilhinney View Post
    As a very simple example, this worked for me:
    vb.net Code:
    1. Imports System.Threading
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         Dim t As New Thread(AddressOf ShowForm2)
    7.  
    8.         t.IsBackground = True
    9.         t.SetApartmentState(ApartmentState.STA)
    10.         t.Start()
    11.     End Sub
    12.  
    13.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    14.         Using f3 As New Form3
    15.             f3.ShowDialog()
    16.         End Using
    17.     End Sub
    18.  
    19.     Private Sub ShowForm2()
    20.         Application.Run(New Form2)
    21.     End Sub
    22.  
    23. End Class
    Obviously, any communication between Form2 and other forms would require delegation, so you might need to keep the appropriate reference(s) around to use for the purpose.
    thanks for the reply.

    I tried this approach before, but was getting some errors (see my second post).. I have a heap of singleton classes that do the dataconnections.

    Is it a good design idea to have a singleton class that can be accessed by many threads?
    It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject admits, and not to seek exactness when only an approximation of the truth is possible.
    -Aristotle As quoted in Rapid Development, chapter 8, page 167.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Have a modal form but be able to access another already open form

    There's nothing specific about using a singleton in a multi-threaded environment that should be a problem. If you had issues then they were most likely cross-thread access of controls, which can be fixed using delegation, or synchronisation issues, which can be fixed using SyncLock blocks or the like.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Hyperactive Member Dasiths's Avatar
    Join Date
    Apr 2001
    Location
    Colombo, Sri Lanka
    Posts
    331

    Thumbs up Re: Have a modal form but be able to access another already open form

    Quote Originally Posted by jmcilhinney View Post
    There's nothing specific about using a singleton in a multi-threaded environment that should be a problem. If you had issues then they were most likely cross-thread access of controls, which can be fixed using delegation, or synchronisation issues, which can be fixed using SyncLock blocks or the like.
    thanks, that's what I thought.

    No errors so far, I wrapped the cross thread calls in delegate methods. Will give this a fair go.

    Thanks everyone.
    It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject admits, and not to seek exactness when only an approximation of the truth is possible.
    -Aristotle As quoted in Rapid Development, chapter 8, page 167.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width