first i'm thai. my english is not good and i'm beginner.

here full project only 83.1 kb
https://drive.google.com/file/d/0ByoqOTgG3AGPM2hKYmlnZWJpOXM/edit?usp=sharing
or
http://www.4shared.com/zip/M8Myl3VZ/NcExpressServiceiConfigWin.html

when i push buttun 1 to call sub add in UserControl1 but error i don't know how to fix it.

property in mainform

internal Button OpenButton { get { return button1; } }

in class MenuController

void InitButtonActions() {
    parent.OpenButton.Click += new EventHandler(delegate { CurrentControl.Add(); }); 
}


public TutorialControl CurrentControl {
    get { return currentcontrol; }
    set {
        if (currentcontrol == value)
            return;
        currentcontrol = value;
    }
}

error at CurrentControl.Add(); help me please ..... ^_^

Edited 2 Years Ago by NAWIN_1: add more information

Please copy and paste the error you're getting.

Comments
good question and meant for all when asking questions like this

Hi, it seems like your CurrentControl is of type TutorialControl and then you are calling a method Add() from this class but we don't have any idea what is the error you are getting in CurrentControl.Add(); and moreover the members of that class are not visible here.
So, it would be better for us to help you out if could possibly paste your code and so that someone could take a closer look on the same.

Comments
good suggestion

here the error

System.NullReferenceException was unhandled
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=NcExpress.Service.iConfig.Win
  StackTrace:
       at NcExpress.Service.iConfig.Win.MenuController.<InitButtonActions>b__0(Object , EventArgs ) in C:\Users\PANDA\documents\visual studio 2010\Projects\NcExpress.Service.iConfig.Win\NcExpress.Service.iConfig.Win\AppsRegistration.cs:line 44
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at NcExpress.Service.iConfig.Win.Program.Main() in c:\users\panda\documents\visual studio 2010\Projects\NcExpress.Service.iConfig.Win\NcExpress.Service.iConfig.Win\Program.cs:line 15
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

this is tutorialcontrol class

public class TutorialControl : TutorialControlBase {
                    internal void ForceHide() { DoHide(); }
                    protected override void DoHide() {
                              base.DoHide();
                    }
                    public TutorialControl() { }
                    public void SetParent(Form parent) {
                              if (this.ParentFormMain == parent)
                                        return;
                              this.ParentFormMain = parent as CSMainForm;
                              if (parent != null) {

                              }
                    }
                    public virtual void Add() { }
                    public virtual void Edit() { }
          }
          [ToolboxItem(false)]
          public class TutorialControlBase : UserControl {
                    CSMainForm parent = null;
                    private string name = string.Empty;

                    protected virtual void DoHide() { }
                    public TutorialControlBase() {
                              SetStyle(ControlStyles.SupportsTransparentBackColor, true);
                    }

                    internal bool AllowDispose { get { return DisposeWhenHide; } }
                    protected virtual bool DisposeWhenHide { get { return false; } }
                    public CSMainForm ParentFormMain {
                              get { return parent; }
                              set {
                                        if (parent != null)
                                                  return;
                                        parent = value;
                              }
                    }
                    public string TutorialName { get { return name; } set { name = value; } }
          }

this is usercontrol1

public partial class UserControl1 : TutorialControl {
                    protected override void OnLoad(EventArgs e) {
                              base.OnLoad(e);
                              foreach (Control ctrl in this.Controls) {
                                        if (ctrl is TextBox)
                                                  ((TextBox)ctrl).Clear();
                                        ctrl.Font = new Font("Segoe UI", 12, ctrl.Font.Style);
                              }
                    }
                    public UserControl1() {
                              InitializeComponent();
                    }
                    public override void Add() {
                              base.Add();
                              textBox1.Text = "Test";
                    }
          }

this is mainform

public partial class CSMainForm : CSMainBase {
                    MenuController menucontroller = null;

                    protected override void OnClosing(CancelEventArgs e) {
                              base.OnClosing(e);
                              TutorialControl control = AppsInfo.CurrentModule as TutorialControl;
                              if (control != null)
                                        control.ForceHide();
                    }
                    protected override void OnLoad(EventArgs e) {
                              base.OnLoad(e);

                    }
                    protected override void RegisterTutorials() {
                              NcExpress.Service.iConfig.Win.RegisterTutorials.Register();
                    }
                    protected override void ShowModule(string name, Panel parent) {
                              AppsInfo.ShowModule(this, name, parent);
                    }
                    public CSMainForm() {
                              InitializeComponent();
                              menucontroller = new MenuController(this);
                    }

                    internal Button OpenButton { get { return button1; } }
                    internal Button CloseButton { get { return button2; } }
          }

and this registertutorials

public class RegisterTutorials {
                    public static void Register() {
                              AppsInfo.Add("UserControl1", typeof(NcExpress.Service.iConfig.Win.Modules.UserControl1), "General");
                    }
          }

it looks lik eyou aren't setting CurrentControl to any value and therefore contains null. When your event handler for Click gets called, CurrentControl.Add() doesn't actually have a value.

Instead of writing the following :-

void InitButtonActions() {
    parent.OpenButton.Click += new EventHandler(delegate { CurrentControl.Add(); }); 
}


public TutorialControl CurrentControl {
    get { return currentcontrol; }
    set {
        if (currentcontrol == value)
            return;
        currentcontrol = value;
    }
}

Try swapping both and write as :-

public TutorialControl CurrentControl {
    get { return currentcontrol; }
    set {
        if (currentcontrol == value)
            return;
        currentcontrol = value;
    }
}


void InitButtonActions() {
    parent.OpenButton.Click += new EventHandler(delegate { CurrentControl.Add(); }); 
}

It may help you because now the CurrentControl has been initialized and it may not throw any error if you do CurrentControl.Add()

Edited 2 Years Ago by KM499

I already tried swap code, it does not work. thank you so much

KM499

and

Ketsuekiame

for kind. I will wait. And i tried to fix it every day but i can't fix it.

Thank KM499 and Ketsuekiame Now i can fix it.

i add this to TuterialControlBase

protected override void OnVisibleChanged(EventArgs e) {
                              if (DesignMode)
                                        return;
                              base.OnVisibleChanged(e);
                              if (this.Visible)
                                        DoShow();
                              else
                                        DoHide();
                    }
                    protected virtual void DoHide() { }
                    protected virtual void DoShow() { }

                    public MenuController MenuController {
                          get {
                                    if (parent != null)
                                              return parent.MenuController;
                                    return null;
                          }
                }

and this in TuterialControl

protected override void DoShow() {
                              base.DoShow();
                              MenuController.CurrentControl = this;
                    }

and this in CSMainForm

public MenuController MenuController { get { return menucontroller; } }
This question has already been answered. Start a new discussion instead.