Değer Doğrulama ile Giriş Kutusu


Bu örnek, basit statik yöntemi kullanarak C # InputBox göstermek için nasıl bir önceki yazı uzanır. Bu örnekte ben statik yöntem Show Inputbox adlı basit bir sınıfı kullanmak (MessageBox.Show benzer olması). Bu uygulamalar aynı zamanda değer doğrulama getiriyor.
Aşağıdaki kod geçerli e-posta adresini almak için giriş Box kullanımını gösterir. Bu iki farklı hata iletileri, boş değer diğeri geçersiz e-posta adresi için başka gösterebilir.
InputBoxValidation validation = delegate(string val) {
if (val == "")
return "Value cannot be empty.";
if (!(new Regex(@"^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,}$")).IsMatch(val))
return "Email address is not valid.";
return "";
};

string value = "info@example.com";
if (InputBox.Show("Enter your email address", "Email address:", ref value, validation) == DialogResult.OK)
{
MessageBox.Show(value);
}

Bu kod Inputbox sınıf uygulamasını gösterir. bir iletişim başlığı, bir istemi metin,
varsayılan değer ve isteğe bağlı olarak bir doğrulama temsilci: Bu Paramaters aşağıdaki
alır statik yöntem Göster aşırı etti. Tamam ya da İptal düğmesine tıklandığında her türlü
hava şartlarına tespit etmek için bir DialogResult döndürür. değeri giriş / çıkış parametre
değeri elde edilebilir.

using System;
using System.Drawing;
using System.Windows.Forms;

public class InputBox
{
public static DialogResult Show(string title, string promptText, ref string value)
{
return Show(title, promptText, ref value, null);
}

public static DialogResult Show(string title, string promptText, ref string value,
InputBoxValidation validation)
{
Form form = new Form();
Label label = new Label();
TextBox textBox = new TextBox();
Button buttonOk = new Button();
Button buttonCancel = new Button();

form.Text = title;
label.Text = promptText;
textBox.Text = value;

buttonOk.Text = "Tamam";
buttonCancel.Text = "Çıkış";
buttonOk.DialogResult = DialogResult.OK;
buttonCancel.DialogResult = DialogResult.Cancel;

label.SetBounds(9, 20, 372, 13);
textBox.SetBounds(12, 36, 372, 20);
buttonOk.SetBounds(228, 72, 75, 23);
buttonCancel.SetBounds(309, 72, 75, 23);

label.AutoSize = true;
textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

form.ClientSize = new Size(396, 107);
form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
form.ClientSize = new Size(Math.Max(300,label.Right+10), form.ClientSize.Height);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
form.MinimizeBox = false;
form.MaximizeBox = false;
form.AcceptButton = buttonOk;
form.CancelButton = buttonCancel;
if (validation != null) {
form.FormClosing += delegate(object sender, FormClosingEventArgs e) {
if (form.DialogResult == DialogResult.OK) {
string errorText = validation(textBox.Text);
if (e.Cancel = (errorText != "")) {
MessageBox.Show(form, errorText, "Sistem Hatası",
MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox.Focus();
}
}
};
}
DialogResult dialogResult = form.ShowDialog();
value = textBox.Text;
return dialogResult;
}
}
public delegate string InputBoxValidation(string errorMessage);

Google Plus ile Paylaş

Kısaca: seymanblog

Panelde şablon düzenle deyip, bu satırı aratarak buraya kısaca hakkımda yazısı yazabilirsiniz.
    BLOGGER YORUMLARI
    FACEBOOK YORUMLARI

0 yorum:

Yorum Gönder