以另一种形式获取方法的价值

时间:2015-08-22 19:25:13

标签: c# winforms

我正在尝试开发一个小型应用程序,它有两种形式form1authentication形式。

因此,当用户输入要连接的SQL实例时,我验证该实例并成功,我将用户重定向到另一个表单。

以下是身份验证方法代码:

public string authenticate()
{

     if ((textBox1.Text == null) )
     {
           MessageBox.Show("Please Enter All Fields!");
           return null;    
     }

     else if (radioButton1.Checked == true)
     {
          connnectionstring = "data source=" + textBox1.Text + " ;Initial Catalog =master;Integrated Security = SSPI";
          return connnectionstring;    
     }

     else if (radioButton2.Checked == true)
     {
          connnectionstring = "data source=" + textBox1.Text + ";Initial Catalog =master;userid=" +textBox2.Text + ";password" +textBox3.Text;
          return connnectionstring;    
     }
     return null;
}

上面的方法给了我连接字符串,所以在form1我试图使用连接字符串来获取该实例中的所有数据库。

我只是试图在检查所有数据库之前打印返回的内容,但我总是得到空值。

以下是我在authenticate中调用form1方法的方式。

private void checkRecommendedSettingsToolStripMenuItem_Click(object sender, EventArgs e)
{
     Authnetication auth = new Authnetication();
     MessageBox.Show(auth.authenticate());
}

但我总是得到null值,我认为这是因为form1被调用时authentication形式TextBoxes因为超出范围而具有空值。但到目前为止,我无法找到获得价值的方法

更新: 以下变化帮助我解决了这个问题

身份验证表单

 Form1 f = new Form1(authenticate());
form1中的

 public string constring { get; set; }


        public Form1(string value)
        {
            InitializeComponent();
            constring = value;



        }

2 个答案:

答案 0 :(得分:1)

不确定但如果您在单独的表单中调用Authenticate方法,那么您是对的,那些表单字段值将不会出现。相反,您可以将这些值传递到Form1构造函数中,然后将这些参数传递给Authenticate方法。以下是我想说的话

Public Form1(string textbox1, string textBox2, string textBox3, bool radio1checked, bool radio2checked)
{
  //do somekind of initialization
}

调用Authenticate方法传递参数,如

private void checkRecommendedSettingsToolStripMenuItem_Click(object sender, EventArgs e)
 {
    Authnetication auth = new Authnetication();
    MessageBox.Show(auth.authenticate(textbox1, textbox2,textbox3,radio1checked, radio2checked));
 }

您需要相应地更改Authenticate方法签名,例如

public string authenticate(string textBox1, string textBox2, string textBox3, bool radio1checked, bool radio2checked)
        {

            if (string.IsNullOrEmpty(textBox1))
            {
                MessageBox.Show("Please Enter All Fields!");
                return null;

            }
            else if (radio1checked)
            {
                connnectionstring = "data source=" + textBox1 + " ;Initial Catalog =master;Integrated Security = SSPI";
                return connnectionstring;

            }
            else if (radio2checked)
            {
                connnectionstring = "data source=" + textBox1 + ";Initial Catalog =master;userid =" +textBox2 + ";password = " +textBox3;
                return connnectionstring;

            }
            return null;
        }

因此,将Form1实例化时,将那些表单字段数据传递给构造函数,如

Form1 frm1 = new Form1(textBox1.Text, textBox2.Text, textBox3.Text, radioButton1.Checked, radioButton2.Checked);
frm1.ShowDialog();

答案 1 :(得分:1)

有很多方法可以实现它。以下是以下方法

1)构造函数重载

//Declaration of Constructor Overloading
public class Form1 : Form
{
    public Form1(string value)
    {

    }
}

//Code for using constructor overloading
Form1 frm = new Form1("text to be passed to the next Form");
frm.Show();

2)公共变量
   这些变量在类的初始化时初始化,可以通过创建类

的对象来访问
public class Form1 : Form
{
    //Declaration of Static Variable
    public string Value = "";
    public Form1()
    {

    }
}

//Code for using static variable
Form1 frm = new Form1();
frm.Value = "text to be passed to the next Form";
frm.Show();

3)静态变量 (不推荐使用此方法,但仍然是实现目标的方法)

   这些变量在项目开始时初始化,可以通过编写父类名称来访问项目中的任何位置

public class Form1 : Form
{
    //Declaration of Static Variable
    static string Value = "";
    public Form1()
    {

    }
}

//Code for using static variable
Form1.Value = "text to be passed to the next Form";

Form1 frm = new Form1();
frm.Show();

希望有所帮助

相关问题