افزودن inputbox در سی شارپ #C

کد با پلتفرم بوی - کد سی شارپ
کد با پلتفرم بوی – کد سی شارپ

روش اول

اگر با زبان vb.net کار کرده باشید به احتمال زیاد با inputbox های پیش فرض آن آشنا هستید .کد زیر استفاده از یک روش استاتیک تعریف InputBox را نشان می دهد که InputBox شناخته شده از VB و VB.NET را شبیه سازی می کند. ما InputBox رادر یک کلاس سفارشی Tmp تعریف کرده ایم .

پلتفرم بوی اجرا شدن خروجی صحیح قطعه کد زیر را تایید می کند

قطعه کدی که در زیر مشاهده می کنید توسط برنامه کامپایلر به جهت تست آزمایش شده و خروجی صحیح مدنظر را بدست آورد.

string value = "Document 1";
if (Tmp.InputBox("New document", "New document name:", ref value) == DialogResult.OK)
{
  myDocument.Name = value;
}

این قطعه که به صورت عمل می کند :

در این روش قطعه کد ما سه پارامتر می گیرد: یک عنوان ، یک متن سریع و مقدار پیش فرض جعبه متن. و یک DialogResult را بازمی گرداند تا مشخص شود که اگر OK یا دکمه لغو کلیک شده باشد. مقدار ورودی را می توان از مقدار پارامتر ورودی / خروجی بدست آورد.

برای کد کلاس Inputbox هم از قطعه کد زیر استفاده کنید :

using System.Windows.Forms;
using System.Drawing;
public static DialogResult InputBox(string title, string promptText, ref string value)
{
  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 = "OK";
  buttonCancel.Text = "Cancel";
  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;
  DialogResult dialogResult = form.ShowDialog();
  value = textBox.Text;
  return dialogResult;
}

روش دوم

می توانید از کلاس های کوچیکتر و قطعه کدهای بهتری مثل زیر هم استفاده کنید :

در فرم اصلی خود ، یک رویداد اضافه کنید که با کلیک بر روی دکمه افزودن در کنترل کننده رویداد ، کاری مشابه کد زیر انجام دهید:

private string value;
private void buttonClicked(object sender, EventArgs e)
{
    using(MainForm form = new MainForm())
    {
        if (form.ShowDialog() == DialogResult.OK)
        {
                      value = form.myvalue;
        }
    }
}
public class Input : Form
{
    public string IPAddress
    {
        get { return txtInput.Text; }
    }

    private void btnInput_Click(object sender, EventArgs e)
    {
        //Do some validation for the text in txtInput to be sure the ip is well-formated.

        if(ip_well_formated)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}

روش سوم

می توانید جعبه نمایش پیام مخصوص خود را ایجاد کنید. ما جعبه پیام یا همان inputbox خود را برای دریافت اطلاعات پایگاه داده مانند قطعه کد زیر ایجاد کرده ایم . هنگامی که جعبه پیام باز می شود ،با کلیک کردن روی هر دکمه ای در جعبه پیام مربوطه ، برنامه متوقف می شود.

بنابر خواسته خودتان قطعه کد را ویرایش کنید :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;

namespace Palmaris_Installation
{
    public class efexBox
    {
        public static string ShowDialog()
        {
            PopUpDatabase form = new PopUpDatabase();
            DialogResult result = form.ShowDialog();
            if (result == DialogResult.Cancel)
                return null;
            else
            {
                if (form.ValueAuthentication == "SQL Server Authentication")
                return form.Valueservername + "?" + form.ValueAuthentication + "?" + form.ValueUsername + "?" + form.ValuePassword;
                else
                    return form.Valueservername + "?" + form.ValueAuthentication + "?" + "" + "?" + "";
            }
        }

        public partial class PopUpDatabase : Form
        {
            public PopUpDatabase()
            {
                InitializeComponent();

                SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
                DataTable table = instance.GetDataSources();

                foreach (DataRow row in table.Rows)
                {
                    cmbServerName.Items.Add(row[0] + "\\" + row[1]);
                }
                cmbAuthentication.Items.Add("Windows Authentication");
                cmbAuthentication.Items.Add("SQL Server Authentication");
                return;
            }

            private void InitializeComponent()
            {
                this.groupBox1 = new System.Windows.Forms.GroupBox();
                this.label1 = new System.Windows.Forms.Label();
                this.label2 = new System.Windows.Forms.Label();
                this.label3 = new System.Windows.Forms.Label();
                this.label4 = new System.Windows.Forms.Label();
                this.cmbServerName = new System.Windows.Forms.ComboBox();
                this.cmbAuthentication = new System.Windows.Forms.ComboBox();
                this.txtUserName = new System.Windows.Forms.TextBox();
                this.txtPassword = new System.Windows.Forms.TextBox();
                this.btnCancel = new System.Windows.Forms.Button();
                this.btnConnect = new System.Windows.Forms.Button();
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
                this.MaximizeBox = false;
                this.groupBox1.SuspendLayout();
                this.SuspendLayout();

                // 
                // groupBox1
                // 
                this.groupBox1.Controls.Add(this.btnConnect);
                this.groupBox1.Controls.Add(this.btnCancel);
                this.groupBox1.Controls.Add(this.txtPassword);
                this.groupBox1.Controls.Add(this.txtUserName);
                this.groupBox1.Controls.Add(this.cmbAuthentication);
                this.groupBox1.Controls.Add(this.cmbServerName);
                this.groupBox1.Controls.Add(this.label4);
                this.groupBox1.Controls.Add(this.label3);
                this.groupBox1.Controls.Add(this.label2);
                this.groupBox1.Controls.Add(this.label1);
                this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
                this.groupBox1.Location = new System.Drawing.Point(0, 0);
                this.groupBox1.Name = "groupBox1";
                this.groupBox1.Size = new System.Drawing.Size(348, 198);
                this.groupBox1.TabIndex = 0;
                this.groupBox1.TabStop = false;
                this.groupBox1.Text = "Database Configration";
                this.groupBox1.BackColor = Color.Gray;
                // 
                // label1
                // 
                this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(50, 46);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(69, 13);
                this.label1.TabIndex = 0;
                this.label1.Text = "Server Name";
                // 
                // label2
                // 
                this.label2.AutoSize = true;
                this.label2.Location = new System.Drawing.Point(50, 73);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(75, 13);
                this.label2.TabIndex = 0;
                this.label2.Text = "Authentication";
                // 
                // label3
                // 
                this.label3.AutoSize = true;
                this.label3.Location = new System.Drawing.Point(50, 101);
                this.label3.Name = "label3";
                this.label3.Size = new System.Drawing.Size(60, 13);
                this.label3.TabIndex = 0;
                this.label3.Text = "User Name";
                // 
                // label4
                // 
                this.label4.AutoSize = true;
                this.label4.Location = new System.Drawing.Point(50, 127);
                this.label4.Name = "label4";
                this.label4.Size = new System.Drawing.Size(53, 13);
                this.label4.TabIndex = 0;
                this.label4.Text = "Password";
                // 
                // cmbServerName
                // 
                this.cmbServerName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
                this.cmbServerName.FormattingEnabled = true;
                this.cmbServerName.Location = new System.Drawing.Point(140, 43);
                this.cmbServerName.Name = "cmbServerName";
                this.cmbServerName.Size = new System.Drawing.Size(185, 21);
                this.cmbServerName.TabIndex = 1;
                // 
                // cmbAuthentication
                // 
                this.cmbAuthentication.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
                this.cmbAuthentication.FormattingEnabled = true;
                this.cmbAuthentication.Location = new System.Drawing.Point(140, 70);
                this.cmbAuthentication.Name = "cmbAuthentication";
                this.cmbAuthentication.Size = new System.Drawing.Size(185, 21);
                this.cmbAuthentication.TabIndex = 1;
                this.cmbAuthentication.SelectedIndexChanged += new System.EventHandler(this.cmbAuthentication_SelectedIndexChanged);
                // 
                // txtUserName
                // 
                this.txtUserName.Location = new System.Drawing.Point(140, 98);
                this.txtUserName.Name = "txtUserName";
                this.txtUserName.Size = new System.Drawing.Size(185, 20);
                this.txtUserName.TabIndex = 2;
                // 
                // txtPassword
                // 
                this.txtPassword.Location = new System.Drawing.Point(140, 124);
                this.txtPassword.Name = "txtPassword";
                this.txtPassword.Size = new System.Drawing.Size(185, 20);
                this.txtPassword.TabIndex = 2;
                // 
                // btnCancel
                // 
                this.btnCancel.Location = new System.Drawing.Point(250, 163);
                this.btnCancel.Name = "btnCancel";
                this.btnCancel.Size = new System.Drawing.Size(75, 23);
                this.btnCancel.TabIndex = 3;
                this.btnCancel.Text = "Cancel";
                this.btnCancel.UseVisualStyleBackColor = true;
                this.btnCancel.DialogResult = DialogResult.Cancel;
                // 
                // btnConnect
                // 
                this.btnConnect.Location = new System.Drawing.Point(140, 163);
                this.btnConnect.Name = "btnConnect";
                this.btnConnect.Size = new System.Drawing.Size(75, 23);
                this.btnConnect.TabIndex = 3;
                this.btnConnect.Text = "Connect";
                this.btnConnect.UseVisualStyleBackColor = true;
                this.btnConnect.DialogResult = DialogResult.OK;
                // 
                // PopUpDatabase
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(348, 198);
                this.Controls.Add(this.groupBox1);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
                this.Name = "PopUpDatabase";
                this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                this.Text = "::: Database Configration :::";
                this.groupBox1.ResumeLayout(false);
                this.groupBox1.PerformLayout();
                this.ResumeLayout(false);

            }

            private System.Windows.Forms.GroupBox groupBox1;
            private System.Windows.Forms.Label label4;
            private System.Windows.Forms.Label label3;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.TextBox txtPassword;
            private System.Windows.Forms.TextBox txtUserName;
            private System.Windows.Forms.ComboBox cmbAuthentication;
            private System.Windows.Forms.ComboBox cmbServerName;
            private System.Windows.Forms.Button btnConnect;
            private System.Windows.Forms.Button btnCancel;

            public string ValueUsername { get { return txtUserName.Text; } }
            public string ValuePassword { get { return txtPassword.Text; } }
            public string Valueservername { get { return cmbServerName.SelectedItem.ToString(); } }
            public string ValueAuthentication { get { return cmbAuthentication.SelectedItem.ToString(); } }

            private void cmbAuthentication_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (cmbAuthentication.SelectedIndex == 1)
                {
                    txtUserName.Enabled = true;
                    txtPassword.Enabled = true;
                }
                else
                {
                    txtUserName.Enabled = false;
                    txtPassword.Enabled = false;
                }
            }
        }
    }
}
string[] strPopUp = efexBox.ShowDialog().Split('?');

روش چهارم

می توانید از کتابخانه Microsoft.VisualBasic.Interaction.InputBox استفاده کنید :

پلتفرم بوی اجرا شدن خروجی صحیح قطعه کد زیر را تایید می کند

قطعه کدی که در زیر مشاهده می کنید توسط برنامه کامپایلر به جهت تست آزمایش شده و خروجی صحیح مدنظر را بدست آورد.

private void btnLogIn_Click(object sender, EventArgs e)
{
    string input = Microsoft.VisualBasic.Interaction.InputBox("Prompt", "Title",Name, 0, 0);
    MessageBox.Show(Name);
}

مایکروسافت نمی تواند برای همه موارد ابزارهای از پیش ساخته شده را به شما تحویل دهد. در برخی از مواقع باید آستین ها را بالا بزنید و یک فرم سفارشی ایجاد کنید و آن را با استفاده از Form.ShowDialog نشان دهید .با ساخت یک inputbox سفارشی شده  به شما این امکان را می دهد که بسیاری از تنظیمات مانند نمایش جعبه ورودی به عنوان یک متن ساده و یا به عنوان رمز عبور یا انجام برخی از تأییدهای اساسی (کدام فیلد ها خالی باشند یا نباشند) و غیره را توسط خودتان تغییر دهید . بهترین روش برای استفاده از فرم inputbox این است که خودتان آن را بسازید .

کدهای بیشتر - سی شارپ

آیا این مطلب برای شما مفید بود؟

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *