简体   繁体   中英

Activating KeyDown Events programmatically in C#

I have two Classes. MyClass.cs and another Class.cs

MyClass.cs contains textbox1 , and KeyDown event " textbox1_Keydown(object sender, KeyEventArgs e) ".

My question is, how do I activate the Keydown event of textbox1 from Another Class.cs ?

There are ways to do it, but it will lead to code that is hard to maintain.

I assume that you don't really want to trigger the event, but that you want to get the same behaviour as if you triggered the event.

My suggestion is to move the code in textbox1_Keydown() to a method in another class and call that method from both textbox1_Keydown() and Class.cs .

You can do that in your aspx code behind c# code. However if you want to use controllers in new c# class you can follow these steps.

<%@ Page Title="" Language="C#" MasterPageFile="~/CRM/CRM.Master" AutoEventWireup="true" CodeBehind="CRMTRN02.aspx.cs" Inherits="CRM.CRMTRN02" %>
<%@ Page Title="" Language="C#" MasterPageFile="~/CRM/CRM.Master" AutoEventWireup="true" CodeBehind="Class1.cs" Inherits="CRM.C" %>

Add like these if you use master page.First line is already added when page created. You can add second line like above.
Then in your Class1.cs

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;

namespace XONTCRM
{
public partial class C
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
}

Now you can access your controllers in aspx page in your Class1.cs file. you can enable disabel and do whatever you think.....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM