NullReferenceException in C1TextBox.OnValidating When Hosted in ToolStripContro

Posted by: dung.itcdt on 16 July 2026, 9:29 pm EST

  • Posted 16 July 2026, 9:29 pm EST

    I am implementing a feature that embeds my UserControl inside a ToolStripControlHost.

    My UserControl contains a C1TextBox control. However, when the textbox loses focus, a null reference exception occurs.

    The root cause is that when the control is hosted inside a ToolStripControlHost,

    FindForm()
    cannot locate a parent Form and returns
    null
    .

    The following code is causing the issue:

    protected override void OnValidating(CancelEventArgs e)
    {
    	if (this.ef)
    		return;
    	object dr = this.dr;
    	if (!e.Cancel && this.cs && this.dl && (!0.Equals(this.dr) || !(base.Text == "-")) && !this.UpdateValueWithCurrentText())
    	{
    		this.gh = true;
    		e.Cancel = true;
    	}
    	bool cancel = e.Cancel;
    	base.OnValidating(e);
    	bool flag = false;
    	object parsedValue = this.Value;
    	ErrorInfo errorInfo = this.ErrorInfo.a();
    	if (e.Cancel && !cancel)
    		flag = this.CheckValidationResult(ErrorReasonEnum.PreValidationError, ref parsedValue, errorInfo);
    	if (!e.Cancel)
    		return;
    	if (errorInfo.CanLoseFocus)
    	{
    		e.Cancel = !flag;
    		this.Value = parsedValue;
    	}
    	this.dr = dr;
    	this.dv = false;
    	Form form = this.FindForm();
    	C1TextBox activeControl = form.ActiveControl as C1TextBox;
    	if (form == null || activeControl == null || !activeControl.cs || activeControl == this)
    		return;
    	activeControl.ai();
    }
    

    As you can see, the problematic section is:

    Form form = this.FindForm();
    C1TextBox activeControl = form.ActiveControl as C1TextBox;
    if (form == null || activeControl == null || !activeControl.cs || activeControl == this)
    	return;
    

    The code attempts to access

    form.ActiveControl
    before checking whether
    form
    is
    null
    . When the control is hosted inside a
    ToolStripControlHost
    ,
    FindForm()
    may return
    null
    , resulting in a null reference exception before the null check is reached.

  • Posted 17 July 2026, 6:03 am EST

    Hello,

    We tested the behavior using C1.Win.Input version 10.0.20261.796 but were unable to reproduce the issue. The behavior works as expected on our end. Please refer to the attached video and sample. (see behavior.zip and Input_TextBoxSample.zip)

    Could you please let us know if we are missing anything? Please update the attached sample to reproduce the error or share a stripped-down version of your project for us to investigate further.

    Also, please test your project with latest versions of the C1TextBox and let us know if the issue persists.

    Regards,

    Uttkarsh.

  • Posted 23 July 2026, 9:24 pm EST

    Sorry, I didn’t describe the issue completely. The error occurs when a TextBox is added inside a ToolStripControlHost, and that host is placed inside a ToolStripDropDown.

    Here’s the code I modified based on your sample project:

    [code]public Form1()

    {

    InitializeComponent();

    var _dropdown = new ToolStripDropDown();
    var host = new ToolStripControlHost(new MyUserControl(), "UserControl");
    _dropdown.Items.Add(host);
    
    var _button = new ToolStripDropDownButton("Button");
    _button.DropDown = _dropdown;
    toolStrip1.Items.Add(_button);
    

    }[/code]

  • Posted 24 July 2026, 6:07 am EST

    Hello,

    We’re sorry but even after using the code you provided, we could not replicate the behavior at our end. Please refer to the attached video to see the behavior at our end. (see behavior2.zip)

    Could you please confirm whether you were able to reproduce the behavior using our sample application without changing the C1Input version? If you did change the version, could you let us know which version you used?

    Additionally, could you please share a video demonstrating the issue along with the exact steps you followed to reproduce it?

    Regards,

    Uttkarsh.

  • Posted 24 July 2026, 10:18 pm EST

    After inspecting the code via reflection, I found that the issue occurs when e.Cancel is set to true.

    Could you please take a look at the video I’ve attached below?

    Recording 2026-07-25 091653.zip

  • Posted 27 July 2026, 9:55 am EST

    Hello,

    We’re not exactly sure what differs in our setup, we still could not replicate the behavior at our end. Please refer to the attached video: behavior3.zip

    Could you please run our sample once more to reproduce the issue and share the complete sample project, including the bin and obj folders?

    Since the resulting package may be quite large, we recommend uploading it to a cloud storage service (such as OneDrive, Google Drive, or Dropbox) and sharing the download link with us.

    Regards,

    Uttkarsh.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels