如何将值从一种形式传递到另一种形式(具有数据库连接性) [英] how to pass a value from one form to another(with database connectivity)

查看:73
本文介绍了如何将值从一种形式传递到另一种形式(具有数据库连接性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将值从一个文本框传递到新表单.详细地说,我的意思是我有一个文本框,我想将该文本框的文本传递给新表单,该表单将执行类似将标签文本显示为sql server列的操作,我已经使用很多搜索进行了编码这个社区帮助我将列的数据绑定到标签中.但是我只想将文本框值传递给另一种形式,就会有一个标签,该标签将以列的名称开始,该名称以先前的形式给出
我的代码是一种形式,我想将其分为两种形式

How to pass value from one text box to a new form. in detailed i mean i have a textbox i want to pass that textbox''s text to a new form that will do action like displaying a label''s text as a sql server''s column i have made that code with many search and this community have helped me to bind a column''s data into a label. but i just want to pass a text box value to another form there will be label which will initiated with column''s name which is given to it in prevous form
my code is like this which is of one form.i want to divide it in two form

private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\aquib\My Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Database1.mdf;Integrated Security=True;User Instance=True";
            string q = ("select SEC_QUESTION from Table1 where USERNAME='" + textBox1.Text + "'");
            con.Open();
            SqlCommand cmd = new SqlCommand(q, con);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows ==true )
            {
                while (dr.Read())
                {
                   label1.Text = dr["SEC_QUESTION"].ToString();
                }
              dr.Close();
                con.Close();
            }
        }



首先将有texbox,然后通过提交按钮传递第二个表单,该表单将标签的文本设置为列.只想知道如何分成两种形式
会发生这种情况吗?



first the texbox will be there then passing by submit button a second form will open which will set the label''s text as column. just want some idea how to divide in two forms
can this be happen? we can do such thing?

推荐答案

您可以通过将修饰符状态更改为public来访问任何表单的控件.
然后,您可以创建一个对象.然后使用该控件.
You can access any form''s control by changing the modifiers state as public.
Then you can create an object. Then use that control.
public partial class Form1 : Form
{
public static Form2 f2=null;
Private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\aquib\My Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Database1.mdf;Integrated Security=True;User Instance=True";
            string q = ("select SEC_QUESTION,col2,col3 from Table1 where USERNAME='" + textBox1.Text + "'");
            con.Open();
            SqlCommand cmd = new SqlCommand(q, con);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows ==true )
            {
                f2=new Form2();
                while (dr.Read())
                {
                   label1.Text = dr["SEC_QUESTION"].ToString();
                   f2.textBox1.Text=dr["col2"].ToString();
                }
                dr.Close();
                con.Close();
                f2.Show();
            }
              
        }


现在是您的代码.
试试吧.


now its your code.
try this.




首先创建表单的对象.

frmTest f =新的frmTest();
f.Tag = txtTextbox.text;
f.Show();

现在在第二页的页面加载中.您已经编写了以下代码.
假设我在标签中显示值;

label1.Text = this.Tag.ToString();

它的100%的工作..

谢谢,
Viprat Shah


first create object of your form.

frmTest f = new frmTest();
f.Tag = txtTextbox.text;
f.Show();

Now on the page load of second page. you have write follwing code.
suppose i am displaying value in lable;

label1.Text = this.Tag.ToString();

Its 100% work..

Thanks,
Viprat Shah


假设Class1创建一些静态类文件
在其中声明静态变量.
公共静态字符串strColumn;

第一种形式使用静态变量.

Create some static class file suppose Class1
Declare static variable in that.
Public static string strColumn;

In first form use static variable.

Private void button1_Click(object sender, EventArgs e)
{
// If class file is not static then create object of class file & use object to //access variable
//Class1 obj = new Class(); 
//obj.strColumn = txtColumn.Text;
 
Class1.strColumn  = txtColumn.Text;
Form2.Show();
}



第二种形式的编码



Second form Coding

Private void Page_load((object sender, EventArgs e))
{

  string q = ("select SEC_QUESTION from Table1 where USERNAME='" + Class1.strColumn + "'");
            
}


这篇关于如何将值从一种形式传递到另一种形式(具有数据库连接性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆