Results 1 to 8 of 8

Thread: [RESOLVED] Using Constructor Event Creates Errors...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2010
    Posts
    178

    Resolved [RESOLVED] Using Constructor Event Creates Errors...

    I created an even on a form called Sub New so I can handle some events and close the window down if there is an error.

    Upon doing that, I came across 3 errors that I don't know how to fix.

    vb.net Code:
    1. Me.Hide()
    2.  
    3.         frmEmail.SetReportDate = #1/23/2012#
    4.         frmEmail.Show()
    5.         frmEmail.Dispose()
    6.  
    7.         Me.Show()

    I get 3 errors, on lines 3, 4 and 5
    Reference to a non-shared member requires an object reference
    How can I fix this? Note: ShowDialog really isn't necessary I could just use .Show. I want to use ShowDialog because the line under those 3 shows the current form.


    Found the problem (I had private sub new) but now a new problem...
    How do I pass the date to the form?

    In case it's relavent, my code for sub new on frmEmail.
    vb.net Code:
    1. Public Sub New()
    2.         ' This call is required by the designer.
    3.         InitializeComponent()
    4.  
    5.         ' Add any initialization after the InitializeComponent() call.
    6.         Dim strHTML As String = String.Empty
    7.         Dim Subject As String = "Service Assignments for " & ReportDate.ToString(myDateFormat)
    8.         Dim oApp As Outlook._Application
    9.         Dim oMsg As Outlook._MailItem
    10.         Dim YesNo As DialogResult = Windows.Forms.DialogResult.Yes
    11.         Dim NoErrors As Boolean
    12.  
    13.         Me.Text = "Service Assignments for " & ReportDate.ToLongDateString
    14.  
    15.         setHeader()
    16.         NoErrors = setData()
    17.         setFooter()
    18.  
    19.         strHTML = reportHeader & reportData.ToString & reportFooter
    20.  
    21.         oApp = New Outlook.Application()
    22.         oMsg = CType(oApp.CreateItem(Outlook.OlItemType.olMailItem), Outlook._MailItem)
    23.  
    24.         oMsg.Subject = Subject
    25.         oMsg.HTMLBody = strHTML
    26.  
    27.         oMsg.To = "DTGOn-RentHelpDesk"
    28.         oMsg.CC = "[email protected]; [email protected]"
    29.  
    30.         oMsg.Importance = Outlook.OlImportance.olImportanceHigh
    31.  
    32.         oMsg.Display()
    33.  
    34.         YesNo = MessageBox.Show("Did the email show up?", Application.ProductName, _
    35.                                 MessageBoxButtons.YesNo, MessageBoxIcon.Question, _
    36.                                 MessageBoxDefaultButton.Button1)
    37.  
    38.         If Not NoErrors Then
    39.             Me.Close()
    40.         Else
    41.             If YesNo = Windows.Forms.DialogResult.No Then
    42.                 ieWeb.Document.Write(strHTML)
    43.                 Me.Show()
    44.             Else
    45.                 MessageBox.Show("Push Send in the email window to send!", "Send Email Alert", _
    46.                                 MessageBoxButtons.OK, MessageBoxIcon.Information)
    47.                 NoErrors = False
    48.                 oMsg2 = oMsg
    49.             End If
    50.         End If
    51.     End Sub
    Last edited by TrickyNick; Jan 27th, 2012 at 06:21 AM.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Using Constructor Event Creates Errors...

    The error speaks for itself really; you have a non-shared member, and you are trying to access it without using an object reference.
    You must create an instance of frmEmail, then you can use the non-shared members on that instance.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Using Constructor Event Creates Errors...

    About your date-passing question. You seem to be passing a date to the form already (provided that you also fix the thing I mention above), what is not working for you?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2010
    Posts
    178

    Re: Using Constructor Event Creates Errors...

    I am getting that error with the code you see...

    when I change to
    vb.net Code:
    1. Dim frm As frmEmail
    2.         Me.Hide()
    3.  
    4.         frm.SetReportDate = #1/23/2012#
    5.         frm.Show()
    6.         frm.Dispose()
    7.  
    8.         Me.Show()

    I get an error saying that I'm using before being assigned a value...
    if I define it as New frmEmail, then it'll call my code in the New Event but it won't have the date because it hasn't reached that line

    I guess my question is, how do I use this code

    Dim frm As New frmEmail

    and pass it the date?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2010
    Posts
    178

    Re: Using Constructor Event Creates Errors...

    The basic outline of the way I want them to be handled...

    Click on button to email...
    gets report data
    open an outlook email & put info there
    Ask user if outlook email opened,
    ...if yes, remind user to click on send, close window without ever showing up
    ...if no, show form with a webbrowser showing the info that way & user manually opens up outlook email


    My code is in Public Sub New on frmEmail, I don't want the form to briefly flicker if everything works right. It was working before perfectly, but the form flickered. I'm dealing with non-computer people and they will think something went wrong.

  6. #6
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    259

    Re: Using Constructor Event Creates Errors...

    You can make another constructor that allows you to pass the date.
    Code:
    Dim frm As New frmEmail(Date.Today)
    
    Public Sub New frmEmail(ByVal reportDate As Date)
    '....
    End Sub

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 2010
    Posts
    178

    Re: Using Constructor Event Creates Errors...

    Thanks, I tried that and it still made my screen flicker thought maybe I did something wrong.

    Attempted it again, still flickered, but with a little modification, I set the form's DialogResult
    OK (or Abort...if error reading db) so form wouldn't show
    Cancel to show from but with the email data on screen to manually email it.

    Thank you. This 4 hour headache is finally complete.

  8. #8
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    259

    Re: [RESOLVED] Using Constructor Event Creates Errors...

    You should check if the form is double buffered and make sure it is "True"

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