Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 同一类中的访问控件及其事件_C#_Controls - Fatal编程技术网

C# 同一类中的访问控件及其事件

C# 同一类中的访问控件及其事件,c#,controls,C#,Controls,我写了一个自定义控件。这个控件在双击鼠标时创建一个窗体。我还添加了其他控件(按钮和标签等)。但我在函数外创建了textbox1和textbox2。我编写了此控件的事件,但这不起作用。伙计们,我编写了textbox\u press事件。由于此事件,我只能编写数字或字母,但我运行此程序并单击了控件的新窗体显示,但此事件不起作用 namespace Deneme { public partial class Direnc : Control { public Diren

我写了一个自定义控件。这个控件在双击鼠标时创建一个窗体。我还添加了其他控件(按钮和标签等)。但我在函数外创建了textbox1和textbox2。我编写了此控件的事件,但这不起作用。伙计们,我编写了textbox\u press事件。由于此事件,我只能编写数字或字母,但我运行此程序并单击了控件的新窗体显示,但此事件不起作用

namespace Deneme
{
    public partial class Direnc : Control
    {
        public Direnc()
        {
            InitializeComponent();
        }


        private string res_name;
        private int res_value;
        Form form = new Form();
        TextBox textBox1 = new TextBox();
        TextBox textBox2 = new TextBox();


        protected override void OnDoubleClick(EventArgs e)
        {
            base.OnDoubleClick(e);

            // 
            // label1
            // 
            Label label1 = new Label();
            AutoSize = true;
            label1.Location = new System.Drawing.Point(27, 35);
            label1.Name = "label1";
            label1.Size = new System.Drawing.Size(27, 13);
            label1.TabIndex = 0;
            label1.Text = "İsmi";
            // 
            // label2
            // 
            Label label2 = new Label();
            AutoSize = true;
            label2.Location = new System.Drawing.Point(13, 89);
            label2.Name = "label2";
            label2.Size = new System.Drawing.Size(41, 13);
            label2.TabIndex = 1;
            label2.Text = "Değeri";
            // 
            // textBox1
            // 

            textBox1.Location = new System.Drawing.Point(58, 32);
            textBox1.Name = "textBox1";
            textBox1.Size = new System.Drawing.Size(100, 22);
            textBox1.TabIndex = 2;
            // 
            // textBox2
            // 

            textBox2.Location = new System.Drawing.Point(58, 86);
            textBox2.Name = "textBox2";
            textBox2.Size = new System.Drawing.Size(100, 22);
            textBox2.TabIndex = 3;
            // 
            // button1
            // 
            Button button1 = new Button();
            button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            button1.Location = new System.Drawing.Point(64, 145);
            button1.Name = "button1";
            button1.Size = new System.Drawing.Size(75, 48);
            button1.TabIndex = 4;
            button1.Text = "Kaydet";
            button1.UseVisualStyleBackColor = true;

            // 
            // form
            // 
            form.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            form.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            form.BackColor = System.Drawing.Color.RoyalBlue;
            form.ClientSize = new System.Drawing.Size(176, 205);
            form.Controls.Add(button1);
            form.Controls.Add(textBox2);
            form.Controls.Add(textBox1);
            form.Controls.Add(label2);
            form.Controls.Add(label1);
            form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            form.MaximizeBox = false;
            form.MinimizeBox = false;
            form.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            form.Text = "Direnç";
            form.TopMost = true;
            form.ResumeLayout(false);
            form.PerformLayout();
            form.ShowDialog();

            button1.Click += new EventHandler(button1_Click);
            textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
            textBox2.KeyPress += new KeyPressEventHandler(textBox2_KeyPress);
        }

        void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsLetter(e.KeyChar) && !char.IsControl(e.KeyChar)
                && !char.IsSeparator(e.KeyChar);
        }

        void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(char.IsLetter(e.KeyChar) && char.IsControl(e.KeyChar))
            e.Handled = true;

        }

        void button1_Click(object sender, EventArgs e)
        {
            res_name = textBox1.Text;
            res_value = Convert.ToInt32(textBox2.Text);
            MessageBox.Show(res_name + res_value.ToString());
        }
    }

根据你的描述,我想这就是你想要的。如果不是的话,你需要给我们更多关于什么不起作用的信息

我认为您所面临的问题是,在调用ShowDialog()之后会分配事件,这是模态。这意味着该方法将阻止当前线程,并且在关闭窗体之前,它之后的任何代码都不会执行。下面的解决方案未经测试,但有望帮助您解决问题。同样,正如我在上面的评论中所说的,从维护的角度来看,使用所有必需的控件向项目中添加实际表单更安全。动态控件可能会在您最意想不到的时候出现意外

protected override void OnDoubleClick(EventArgs e)
{
    base.OnDoubleClick(e);

    // The rest of you code here
    // The rest of you code here
    // The rest of you code here

    button1.Click += delegate
    {
        res_name = textBox1.Text;
        res_value = Convert.ToInt32(textBox2.Text);
        MessageBox.Show(res_name + res_value.ToString());
    };
    textBox1.KeyPress += delegate(object sender, KeyPressEventArgs ev)
    {
        ev.Handled = !char.IsLetter(ev.KeyChar) && !char.IsControl(ev.KeyChar) && !char.IsSeparator(ev.KeyChar);
    };
    textBox2.KeyPress += delegate(object sender, KeyPressEventArgs ev)
    {
        if (char.IsLetter(e.KeyChar) && char.IsControl(e.KeyChar))
            e.Handled = true;
    };

    form.ShowDialog(); // <----------- MODAL call, all the code is added BEFORE it
}
受保护的覆盖无效双击(事件参数e)
{
基础。双点击(e);
//剩下的人在这里编码
//剩下的人在这里编码
//剩下的人在这里编码
按钮1.单击+=委派
{
res_name=textBox1.Text;
res_值=转换为32(textBox2.Text);
Show(res_name+res_value.ToString());
};
textBox1.KeyPress+=委托(对象发送者,KeyPressEventArgs ev)
{
ev.Handled=!char.isleter(ev.KeyChar)和&!char.IsControl(ev.KeyChar)和&!char.IsSeparator(ev.KeyChar);
};
textBox2.KeyPress+=委托(对象发送者,KeyPressEventArgs ev)
{
if(char.isleter(e.KeyChar)和&char.IsControl(e.KeyChar))
e、 已处理=正确;
};

form.ShowDialog();//什么不起作用?如果没有更多详细信息,我们无法回答您的问题。除了
它不起作用之外,您是否可以提供多一点信息
还可以通过代码确定哪一行失败或未产生预期的结果…?我在单击自定义控件时会写入两个文本框按键事件和双击事件表单显示但事件不起作用例如,我无法写入数字文本框1但此代码不能挂起,这里有一些我不懂的内容…。为什么在调用后分配文本框/按钮事件form.ShowDialog()?这显然是您代码中的一个缺陷。我还认为,您可以更轻松地将表单添加到项目中,而不是动态创建表单。这将简化将来的维护。是的,这非常有效。我也知道我在哪里犯了错误。Ty bro