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

时间:2014-12-04 15:59:54

标签: c# controls

我编写了一个自定义控件。这个控件在双击鼠标时创建一个Form。我还添加了其他控件(按钮和标签等)。但是我在function.Iut之外创建了textbox1和textbox2。我写了这个控件的事件但是这个没有work.Guys我写textbox_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());
        }
    }

1 个答案:

答案 0 :(得分:0)

根据您的描述,我认为这就是您想要的。如果不是,您需要向我们提供有关不起作用的更多信息。

我认为您遇到的问题是您在调用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
}