Form can't see event in usercontrol

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Henrik
    New Member
    • Aug 2010
    • 3

    Form can't see event in usercontrol

    Hello,

    I'm trying to make an event that will close a usercontrol and open another one. The first user control is for login and when the password have been validated, the event should fire and the event/form would close the usercontrol. The problem is that the form can't see the event with the error "are you missing a using directive or an assembly reference?"

    The form code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.Odbc;
    
    namespace økonomi
    {
        
        public partial class MainForm : Form
        {
            UserControl LoginControl = new LoginControl();
            public MainForm()
            {
                InitializeComponent();
                InitControl();
            }
    
            void InitControl()
            {
    
                LoginControl.OnMyEvent += new LoginControl.onMyEventHandler(catchevent);
                panel1.Controls.Add(LoginControl);
            }
    
            static void catchevent(object sender, EventArgs e)
            {
                MessageBox.Show("Hello");
            }
        }
    }
    It's line 26 that gives me the error (OnMyEvent).

    And the usercontrol:
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace økonomi
    {
        
        public partial class LoginControl : UserControl
        {
            public delegate void onMyEventHandler(object sender, EventArgs e);
            public event onMyEventHandler OnMyEvent;
            
            public LoginControl()
            {
                InitializeComponent();
            }
            
            public void btnLogin_Click(object sender, EventArgs e)
            {
                if (txtPwd.ToString() == "1234")
                {
                    OnMyEvent(this, e);
                }
                
            }
        }
    }
  • fastestindian
    New Member
    • Aug 2009
    • 74

    #2
    The cause for this error are:


    1.You do not have class with that name in your project
    2. You add the class later on and run the project without compiling
    3. You have the class but you do not specify the namespace via Imports or using keyword

    Even though you have the dll added to your reference, use Imports statement.

    For example if we have a tiered app, in which there is a namespace called BusinessDataObj ects, in which I write all my classes, to reference this in my project I add the dll to this namespace, but then in my code in the presentation layer to use this I also write using BusinessDataObj ects. in your case it will be Imports EmailMessage.

    Hope this works

    Comment

    • Henrik
      New Member
      • Aug 2010
      • 3

      #3
      I'm not sure i understand what dll am i suppose to import where? I think it's you point 3 that is the problem in my case...

      Comment

      • Christian Binder
        Recognized Expert New Member
        • Jan 2008
        • 218

        #4
        Why namespace EmailMessage? ("in your case it will be Imports EmailMessage.") This namespace isn't used here?

        Do you have multiple projects or are LoginControl and MainForm in the same project/assembly?

        Comment

        • Henrik
          New Member
          • Aug 2010
          • 3

          #5
          Yes they are in the same project.

          Comment

          • Christian Binder
            Recognized Expert New Member
            • Jan 2008
            • 218

            #6
            Oh, I think, I figured out, what's wrong:
            Change line 16 from
            UserControl LoginControl = new LoginControl();
            to
            LoginControl LoginControl = new LoginControl();
            [/CODE]

            Comment

            Working...