Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 30th, 2007, 02:37 PM
Registered User
 
Join Date: Dec 2007
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default enum 2 names

Hi, I'm want to have a enumuration with all the forms that i can launch.
they are named frm_1, frm_34 and frm_023
But I want my enum to contain name like

Public Enum enumForm
    Players
        Matches
End Enum

and then I want check the Enum names for a form name like frm_1
So I think I need a Enum with a name and a custom name added to it
so .tostring returns the form name.

can this be done?

Reply With Quote
  #2 (permalink)  
Old December 30th, 2007, 09:56 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Can you explain more about what you are planning on doing with this? This doesn't sound quite like the best way to use an enum. Perhaps there is a better solution.

-Peter
Reply With Quote
  #3 (permalink)  
Old December 31st, 2007, 06:49 PM
Registered User
 
Join Date: Dec 2007
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, well I only want 1 form open in my main form at anytime. using an mdi architecture. I solved it by looping through the child forms of the main form but i will come back to this function later.

Now When I click a button on the main form it should open one child form using the parameter as enumForm but first look if the form isn't there already.

so i call the function getChildForm(enumform as enumForms)
and I compare the NAME of the childform wich is frm_lol to the enumform.toString wich could be lol.

I could remove the frm_ prefix from the childform name OR
I could add the frm_ prefix to the enum
BUT that looks very ugly what if i want to rename a form...

That's my real question i gues :D

What is the best way to check if a form is already loaded using an enum
Code:
    Private Function getChildForm(ByVal enumForm As enumForm) As Form
        Dim strFrmName As String = enumForm.ToString
        Dim frm_child As Form = Nothing

        If IsFormAlreadyLoaded(strFrmName) Then
            MsgBox("Formulier " & strFrmName & " is open.")
            Return getForm(strFrmName)
        Else
            Select Case enumForm
                Case enumForm.frm_Dokter : frm_child = New frm_Dokter()
                Case enumForm.frm_Patient : frm_child = New frm_Patient()
                Case enumForm.frm_Over : frm_child = New frm_Over()
                Case Else : MsgBox("Form " & enumForm.ToString & " doesn't excist.")
            End Select
        End If
        Try
            frm_child.MdiParent = Me
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error in " & Me.Name & " getChildForm")
        End Try
        Return frm_child
    End Function
Code:
    Private Function IsFormAlreadyLoaded(ByVal formToLoadName As String) As Boolean
        Dim frmChild As Form
        For Each frmChild In Me.MdiChildren
            If frmChild.Name = formToLoadName Then Return True
        Next
    End Function
Code:
    Private Function getForm(ByVal formToLoadName As String) As Form
        Dim frmChild As Form
        For Each frmChild In Me.MdiChildren
            If frmChild.Name = formToLoadName Then Return frmChild
        Next
        Return Nothing
    End Function


This is what I got, but I'm sure you have comments on it:D
is this the best way to do it?
Reply With Quote
  #4 (permalink)  
Old January 1st, 2008, 08:20 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Let's review your criteria: "I only want 1 form open in my main form at anytime."

It appears to me you are hitting a nail with a sledge hammer. If your criteria to only have 1 child form (MDI) open at a single time within the parent form, then the solution seems pretty simple to me. Just store a reference to the child form in the parent form.
Code:
Class <ParentForm>
    Private _childForm As System.Windows.Forms.Form
    ...
End Class
When an action happens on the parent form (button clicked, menu item clicked, etc.) you check the variable holding the child form reference. So the code inside some action handler would be:
Code:
    If Not _childForm Is Nothing Then
        'child form is open, do something here
    End If
If the rules are to have only 1 child open at a time you then need to decide how to handle this action. I imagine you would want to check the type of the requested and open form to determine if we need to open a new one at all. E.g. In your example, if the user wants to open frm_Dokter and that's already open there's no need to continue with the close/open process. You can perform this check like this:
Code:
    If TypeOf _childForm Is frm_Dokter Then
        'form requested is already open, handle accordingly
    Else
        'form requested is NOT already open, handle accordingly
    End If
When you have a form already open and the user is requesting a different one you need to take further action aside from opening the new form. Possible options I can think of are:

A) Close the open form and open the new one requested.
B) Inform the user that they must close the open form before they can open another. This would be a good place to implement a child form super class from which all child forms are derived or an interface implemented by all child forms that provides the child form's check behavior. Use this to determine if a child form can be closed (operations completed, data saved, etc.). This is a bit more of an advanced topic but something worth thinking about.
C) Simply tell the user they can't open another form until they've closed the open one. From a usability standpoint I'd avoid this one. Better to use approach B. If the use CAN switch forms with no repercussions, then it should happen. Otherwise, tell the user what they need to complete or just give them the option. Again, if you implement some common interfaces (using a literal interface or a super class for child forms) then you can centralize the check operation.

So when you get to the point in your code where you are going to open the new form, you do something like this:
Code:
    Dim blnOpenForm As Boolean = False
    If _childForm.CanClose() Then
        blnOpenForm = True
    Else
        If _childForm.CanSave() Then
            Dim msgResult As DialogResult
            msgResult = MessageBox.Show(_childForm.GetCanSaveMessage())
            If msgResult = DialogResult.OK
                _childForm.Save()
                blnOpenForm = True
            End If
        Else
            MessageBox.Show(_childForm.GetCannotSaveMessage())
        End If
    End If
    If blnOpenForm Then
        'open the requested form
    End If
The above example is a little crude, but the general idea is there. What's happening here is that there are several methods that all child forms would implement (through either super class "MustOverride" methods or implemented interface methods). The methods are:
  • CanClose() - Tests the state of the child form to determine if it can be closed as-is (does data need to be saved, etc.)
  • CanSave() - Tests whether the data in the form can be saved in the current state. (E.g. The user may need to finish entering critical data first.)
  • GetCanSaveMessage() - Returns a message telling the user that the form can be saved in it's current state. (E.g. "Data for patient x will be saved.")
  • GetCannotSaveMessage() - Returns a message telling the user that the form can NOT be saved in it's current state. ("You must finish filling in x and y for patient z.")
  • Save() - Performs the child forms save actions.
Another possible and potentially useful addition to the list would be an Undo() method. Then you provide the user the ability to close the task they started on the current open form without saving it (by undoing it). (In some or many cases, the Undo() method simply doesn't do anything because a save to a database is just ignored which essentially is an Undo operation).

The end result of all of this is that when the user goes to open a new form, they either get the new form immediate, they get a message like this (message from GetCannotSaveMessage method):
Code:
+-----------------------------------------------------------------------+
|  The current view cannot be closed without completing the following:  |
|         You must finish filling in x and y for patient z.             |
|                                                                       |
|                          +-------------+                              |
|                          |     OK      |                              |
|                          +-------------+                              |
+-----------------------------------------------------------------------+
or they get a prompt such as:
Code:
+--------------------------------------------------------------------------+
|  The current view cannot be closed without taking the following action:  |
|                 Data for patient x will be saved.                        |
|           Would you like to save changes and continue?                   |
|                                                                          |
|         +-------------+   +-------------+   +-------------+              |
|         |     OK      |   |   Ignore    |   |   Cancel    |              |
|         +-------------+   +-------------+   +-------------+              |
+--------------------------------------------------------------------------+
Line 2 in the first message comes from the child form's GetCannotSaveMessage method. The rest comes from the parent form generic code.

Lines 1 and 3 of the prompt come from the generic code in the parent form. The 2nd line is what comes from the GetCanSaveMessage() that is implemented by each child form. Actions for the dialog buttons are:
OK - the child form's Save() method is called and switching forms continues
Ignore - child form's Undo() method is called and switching forms continues
Cancel - No child form action happens and switching forms stops.


I'm sure I've provided you with far more than you expected, but I'm feeling strangely inspired by the new year and a haze of insomnia.

Hope you can understand and make some use of my suggestions.

-Peter
Reply With Quote
  #5 (permalink)  
Old January 2nd, 2008, 02:50 PM
Registered User
 
Join Date: Dec 2007
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi mr planoie,

Thx for the long reply, It would be enough if you would say that I had to add the forms as private vars to my main form class lol. Neverless I read your reply 4 times to understand what you tried to telling me.
and i'm not ready yet for canSave and undo stuff, but mayby later on.

Altought I have a slight variant on my question, "there can only be one form open of some type at any time..." but the basic is there and it's up and running

on to the next 'problem'

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Enum gets initalized to first value mhshpk ASP.NET 2.0 Professional 2 September 4th, 2007 10:16 AM
enum toString() overloading kramis8 C# 0 November 21st, 2004 07:55 PM
Enum in VB saeta119 BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 2 June 7th, 2003 04:10 PM
Using Enum Type Mike2000 VB.NET 2002/2003 Basics 2 June 5th, 2003 05:20 AM



All times are GMT -4. The time now is 09:28 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.