2009-07-18 15 views
6

हैलो मैं वर्तमान में एक एप्लिकेशन बना रहा हूं जिसमें सर्वर आईपी पते जोड़ने की आवश्यकता है, क्योंकि सी # में कोई इनपुटबॉक्स फ़ंक्शन नहीं है, मैं इसे फ़ॉर्म का उपयोग करके पूरा करने की कोशिश कर रहा हूं, लेकिन मैं हूं भाषा के लिए बहुत नया है इसलिए मुझे 100% नहीं करना चाहिए।फॉर्मों का उपयोग करके सी # में एक इनपुटबॉक्स बनाना

इस समय मेरे पास मेरा मुख्य रूप और एक रूप है जो मेरे इनपुटबॉक्स के रूप में कार्य करेगा जो लोड पर छिपाना चाहता है। फिर जब उपयोगकर्ता मुख्य रूप में ऐड आईपी पते पर क्लिक करता है तो मैं द्वितीयक रूप खोलना चाहता हूं और द्वितीयक रूप पर एक टेक्स्ट बॉक्स में दर्ज आईपी पता वापस भेजना चाहता हूं।

तो मैं यह करने के बारे में कैसे जाऊं? या क्या इसी तरह के परिणाम प्राप्त करने के लिए कोई बेहतर तरीका है?

+0

कि मूर्ख है, लेकिन सच नहीं है। – kenny

उत्तर

8

आपका मुख्य रूप में (सबसे आसान तरीका सार्वजनिक रूप क्षेत्र घोषित करने के लिए है) घटना के लिए बटन के क्लिक एक घटना हैंडलर जोड़ने आईपी ​​पता जोड़ें।

private string m_ipAddress; 
private void OnAddIPAddressClicked(object sender, EventArgs e) 
{ 
    using(SetIPAddressForm form = new SetIPAddressForm()) 
    { 
     if (form.ShowDialog() == DialogResult.OK) 
     { 
      //Create a property in SetIPAddressForm to return the input of user. 
      m_ipAddress = form.IPAddress; 
     } 
    } 
} 

संपादित: ईवेंट हैंडलर में, नीचे दिए गए कोड के रूप में ऐसी ही कुछ करना एक और उदाहरण manemawanna टिप्पणी के साथ फिट करने के लिए जोड़ें।

private void btnAddServer_Click(object sender, EventArgs e) 
{ 
    string ipAdd; 
    using(Input form = new Input()) 
    { 
     if (form.ShowDialog() == DialogResult.OK) 
     { 
      //Create a property in SetIPAddressForm to return the input of user. 
      ipAdd = form.IPAddress; 
     } 
    } 
} 

अपने इनपुट रूप में, एक संपत्ति जोड़ें:

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(); 
     } 
    } 
} 
+0

आपको बेहतर() कथन का उपयोग करना चाहिए। :) –

+0

ठीक है, हम हर दिन सामान सीखते हैं :), मैंने सबकुछ (पेन, एसक्यूएलकनेक्शन, स्ट्रीम इत्यादि) पर अपने कोड में कीवर्ड का उपयोग करके आवेदन किया लेकिन मैंने कभी इसे एक फॉर्म पर नहीं किया। शायद क्योंकि मैंने कभी भी एक पुस्तक में आवेदन करने के लिए एक उदाहरण नहीं देखा ... यहां तक ​​कि एक पुस्तक में भी। –

+0

यह वही है जो मेरे पास वर्तमान में निजी शून्य btnAddServer_Click (ऑब्जेक्ट प्रेषक, EventArgs ई) { स्ट्रिंग ipAdd; } मैं अपने अन्य रूप को नहीं जानता जो मैं उपयोग करना चाहता हूं इनपुट नाम दिया गया है, इसमें txtInput नामक एक टेक्स्ट बॉक्स और btnInput नामक एक बटन है। तो यह आपके द्वारा जवाब के रूप में दिया गया मिलान कैसे करेगा? क्षमा करें, लेकिन मैं ये सब नया हूं। – manemawanna

1

मुख्य रूप में एक बटन जोड़ें।

आईपी पते के लिए टेक्स्टबॉक्स के साथ एक फॉर्म बनाएं। (मान लें कि यह IPAddressForm)

उस बटन के लिए ईवेंट ईवेंट हैंडलर जोड़ें।

ईवेंट हैंडलर में, IPAddressForm का एक उदाहरण बनाएं और IPAddressForm की showdialog विधि को कॉल करें।

कुछ कक्षा चर में आईपी पता स्टोर करें।

showdialog परिणाम ठीक है, तो, मुख्य रूप से वर्ग चर पढ़ा

1

लगता फ्रांसिस की तरह सही विचार जो मैं क्या सुझाव दिया है | है। हालांकि, बस इसमें जोड़ने के लिए मैं शायद मूल टेक्स्टबॉक्स के बजाय मास्कड टेक्स्टबॉक्स का उपयोग करने का सुझाव दूंगा और मास्क के रूप में आईपी पता प्रारूप जोड़ूंगा।

1

तुम बस वीबी InputBox इस्तेमाल कर सकते हैं ...

  1. Microsoft.VisualBasic के संदर्भ में जोड़े
  2. स्ट्रिंग परिणाम = Microsoft.VisualBasic.Interaction.InputBox ("शीर्षक", "पाठ", "" , 10, 20);
2

मुझे भी इस सुविधा की आवश्यकता है। मेरा कोड यहाँ है; यह ऑटो-सेंटर और आकार को तत्काल फिट करने के लिए। सार्वजनिक विधि एक संवाद बनाता है और उपयोगकर्ता के इनपुट को लौटाता है, या null अगर वे रद्द करते हैं।

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

namespace Utilities 
{ 
public class InputBox 
    { 
    #region Interface 
    public static string ShowDialog(string prompt, string title, string defaultValue = null, int? xPos = null, int? yPos = null) 
     { 
     InputBoxDialog form = new InputBoxDialog(prompt, title, defaultValue, xPos, yPos); 
     DialogResult result = form.ShowDialog(); 
     if (result == DialogResult.Cancel) 
      return null; 
     else 
      return form.Value; 
     } 
    #endregion 

    #region Auxiliary class 
    private class InputBoxDialog: Form 
     { 
     public string Value { get { return _txtInput.Text; } } 

     private Label _lblPrompt; 
     private TextBox _txtInput; 
     private Button _btnOk; 
     private Button _btnCancel; 

     #region Constructor 
     public InputBoxDialog(string prompt, string title, string defaultValue = null, int? xPos = null, int? yPos = null) 
      { 
      if (xPos == null && yPos == null) 
       { 
       StartPosition = FormStartPosition.CenterParent; 
       } 
      else 
       { 
       StartPosition = FormStartPosition.Manual; 

       if (xPos == null) xPos = (Screen.PrimaryScreen.WorkingArea.Width - Width) >> 1; 
       if (yPos == null) yPos = (Screen.PrimaryScreen.WorkingArea.Height - Height) >> 1; 

       Location = new Point(xPos.Value, yPos.Value); 
       } 

      InitializeComponent(); 

      if (title == null) title = Application.ProductName; 
      Text = title; 

      _lblPrompt.Text = prompt; 
      Graphics graphics = CreateGraphics(); 
      _lblPrompt.Size = graphics.MeasureString(prompt, _lblPrompt.Font).ToSize(); 
      int promptWidth = _lblPrompt.Size.Width; 
      int promptHeight = _lblPrompt.Size.Height; 

      _txtInput.Location = new Point(8, 30 + promptHeight); 
      int inputWidth = promptWidth < 206 ? 206 : promptWidth; 
      _txtInput.Size  = new Size(inputWidth, 21); 
      _txtInput.Text  = defaultValue; 
      _txtInput.SelectAll(); 
      _txtInput.Focus(); 

      Height = 125 + promptHeight; 
      Width = inputWidth + 23; 

      _btnOk.Location = new Point(8, 60 + promptHeight); 
      _btnOk.Size  = new Size(100, 26); 

      _btnCancel.Location = new Point(114, 60 + promptHeight); 
      _btnCancel.Size  = new Size(100, 26); 

      return; 
      } 
     #endregion 

     #region Methods 
     protected void InitializeComponent() 
      { 
      _lblPrompt   = new Label(); 
      _lblPrompt.Location = new Point(12, 9); 
      _lblPrompt.TabIndex = 0; 
      _lblPrompt.BackColor = Color.Transparent; 

      _txtInput   = new TextBox(); 
      _txtInput.Size  = new Size(156, 20); 
      _txtInput.TabIndex = 1; 

      _btnOk    = new Button(); 
      _btnOk.TabIndex  = 2; 
      _btnOk.Size   = new Size(75, 26); 
      _btnOk.Text   = "&OK"; 
      _btnOk.DialogResult = DialogResult.OK; 

      _btnCancel    = new Button(); 
      _btnCancel.TabIndex  = 3; 
      _btnCancel.Size   = new Size(75, 26); 
      _btnCancel.Text   = "&Cancel"; 
      _btnCancel.DialogResult = DialogResult.Cancel; 

      AcceptButton = _btnOk; 
      CancelButton = _btnCancel; 

      Controls.Add(_lblPrompt); 
      Controls.Add(_txtInput); 
      Controls.Add(_btnOk); 
      Controls.Add(_btnCancel); 

      FormBorderStyle = FormBorderStyle.FixedDialog; 
      MaximizeBox = false; 
      MinimizeBox = false; 

      return; 
      } 
     #endregion 
     } 
    #endregion 
    } 
} 
+0

आपको बहुत बहुत धन्यवाद। इसने काम कर दिया...... –

0

आप अपना विशेष संदेशबॉक्स बना सकते हैं। मैंने नीचे दिए गए डेटाबेस की जानकारी प्राप्त करने के लिए अपना संदेशबॉक्स बनाया है। और जब संदेशबॉक्स खुलता है, तो आपके दौरान एप्लिकेशन स्टॉप संबंधित संदेशबॉक्स में किसी भी बटन पर क्लिक करता है।

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('?'); 
संबंधित मुद्दे