PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 Opening the Default Instance of the Form-VBForums
Results 1 to 9 of 9

Thread: Opening the Default Instance of the Form

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,133

    Opening the Default Instance of the Form

    Hi.

    I have an form named Sales.
    I want that only one Instance of the form should be open at a given time.

    The form contains an Public Function named 'CheckShiftStatus' Which returns an boolean. This function has to be checked before opening of the form. If it returns True then the form is opened.

    I am facing the following problems
    1) I am not able to call Sales.CheckShiftStatus
    2) I also tried the following code, which returns an error as "Cannot access a disposed object" (@ SalesScreen.Show)

    vb Code:
    1. Dim SalesScreen as New Sales
    2.  
    3. Private Sub MnuSales_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuSales.Click
    4.         If AllowTranForCurrentDate Then
    5.  
    6.             If SalesScreen Is Nothing Then SalesScreen = New Sales
    7.  
    8.             If SalesScreen.CheckShiftsStatus Then
    9.                 SalesScreen.Show()
    10.                 SalesScreen.Activate()
    11.             End If
    12.         Else
    13.             MessageBox.Show("Transactions for Current date are Disabled", MsgboxHeader, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    14.         End If
    15.     End Sub

    plz help

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,146

    Re: Opening the Default Instance of the Form

    Quote Originally Posted by aashish_9601 View Post
    I am facing the following problems
    1) I am not able to call Sales.CheckShiftStatus
    2) I also tried the following code, which returns an error as "Cannot access a disposed object" (@ SalesScreen.Show)
    1) Why not? Are you getting some kind of error?

    2) It sounds like CheckShiftsStatus is disposing the form, we would need to see the code to help determine why that is.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,133

    Re: Opening the Default Instance of the Form

    1) No, rather the Intellisense is not showing the function in the List
    2) 'CheckShiftStatus' only checks for some values in the Db and returns True/False

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,146

    Re: Opening the Default Instance of the Form

    1) Is it declared with the keyword Public? (or Friend, etc)
    Are you sure you spelt the name correctly? (in this thread you have called it CheckShiftsStatus and CheckShiftStatus)
    Even tho it doesn't show in Intellisense, does it actually run?

    2) That is what you meant it to do, but it sounds like something else is happening too... showing us the code for that routine would be a good idea.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,133

    Re: Opening the Default Instance of the Form

    Well the problem i think is with the constructor

    I have declared it as following..
    vb Code:
    1. Public Sub New(Optional ByVal IsHidden As Boolean = False, Optional ByVal Sales_id As String = "")
    2.  
    3.         ' This call is required by the designer.
    4.         InitializeComponent()
    5.         Me.LoadSettings()
    6.         If IsHidden Then Me.SetEntryMode(True)
    7.         Me.SalesId = Sales_id
    8.     End Sub


    If i remove this constructor then i am able to access 'CheckShiftStatus' from default instance.

    If i am declaring another constructor (the default) as..

    vb Code:
    1. Public Sub New()
    2.  
    3.         ' This call is required by the designer.
    4.         InitializeComponent()
    5.  
    6.         ' Add any initialization after the InitializeComponent() call.
    7.  
    8.     End Sub

    The compiler is giving me an error as "cannot overload each other because they differ only in optional parameters"

  6. #6
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,703

    Re: Opening the Default Instance of the Form

    Quote Originally Posted by aashish_9601 View Post
    2) I also tried the following code, which returns an error as "Cannot access a disposed object" (@ SalesScreen.Show)

    vb Code:
    1. Dim SalesScreen as New Sales
    2.  
    3. Private Sub MnuSales_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuSales.Click
    4.         If AllowTranForCurrentDate Then
    5.  
    6.             If SalesScreen Is Nothing Then SalesScreen = New Sales
    7.  
    8.             If SalesScreen.CheckShiftsStatus Then
    9.                 SalesScreen.Show()
    10.                 SalesScreen.Activate()
    11.             End If
    12.         Else
    13.             MessageBox.Show("Transactions for Current date are Disabled", MsgboxHeader, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    14.         End If
    15.     End Sub


    plz help
    This is happening because when Sales closes after opening it once, it still exists in the disposed state. After that any subsequent button click will return false when checking to see if it is Nothing, but since it's disposed, it cannot be opened. To fix it, you either need to set sales=Nothing after it closes, or simply hide it in the Closing method.

    The compiler is giving me an error as "cannot overload each other because they differ only in optional parameters"
    Because you are not required to include optional parameters, it becomes ambiguous as to which constructor should be called when passing no parameters. Remove the Optional keyword and this error will go away.
    Last edited by kebo; Nov 17th, 2018 at 04:49 PM.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,056

    Re: Opening the Default Instance of the Form

    Kebo has the right of it. You are used to closing forms and them going away, but for what you are doing, you have to take a further step. When you close the form, set the variable to Nothing explicitly, or the variable will still hold the form, though it will be in a disposed state.

    One way you can do that is to handle the Closing event of the Sales form. The form that showed the form would be where you'd handle the Closing event, so you'd either need SaleScreen as WithEvents, or use AddHandler to hook up the event handler. In the Closing event you'd set SaleScreen = Nothing. If you use AddHandler to hook it up, you should also use RemoveHandler to unhook it, though it likely doesn't really matter much, since the disposed form won't be raising anymore events.
    My usual boring signature: Nothing

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,913

    Re: Opening the Default Instance of the Form

    Quote Originally Posted by Shaggy Hiker View Post
    Kebo has the right of it. You are used to closing forms and them going away, but for what you are doing, you have to take a further step. When you close the form, set the variable to Nothing explicitly, or the variable will still hold the form, though it will be in a disposed state.

    One way you can do that is to handle the Closing event of the Sales form. The form that showed the form would be where you'd handle the Closing event, so you'd either need SaleScreen as WithEvents, or use AddHandler to hook up the event handler. In the Closing event you'd set SaleScreen = Nothing. If you use AddHandler to hook it up, you should also use RemoveHandler to unhook it, though it likely doesn't really matter much, since the disposed form won't be raising anymore events.
    That's far more convoluted than is necessary. A form can tell you whether it's disposed so the solution is to do this:
    vb.net Code:
    1. If SalesScreen Is Nothing OrElse SalesScreem.IsDisposed Then SalesScreen = New Sales
    In fact, given that SalesScreen is initialised where it's declared, it can never be Nothing so that check is pointless:
    vb.net Code:
    1. If SalesScreem.IsDisposed Then SalesScreen = New Sales

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,056

    Re: Opening the Default Instance of the Form

    Yeah, that's true. I gave the solution I used, but I was dealing with more in the Closing handler than just setting the form to Nothing. If you need to do things when that form closes, then handling the closing handler is a good way to go about it, and if so, you can go ahead and set the variable to Nothing at that time.
    My usual boring signature: Nothing

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width