同一类中的访问控制及其事件
本文关键字:访问控制 事件 一类 | 更新日期: 2023-09-27 18:33:17
我写了一个自定义控件。此控件在双击鼠标时创建一个 Form。我还添加了其他控件(按钮和标签等)。但是我在函数之外创建了文本框 1 和文本框 2。我编写了此控件的事件,但这不起作用。伙计们,我写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());
}
}
根据你的描述,我认为这就是你想要的。如果没有,您需要向我们提供有关不起作用的更多信息。
我认为您面临的问题是您的事件是在您调用 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
}