首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

为何combobox.text获取不了值

2013-04-22 
为什么combobox.text获取不了值using Systemusing System.Collections.Genericusing System.ComponentMo

为什么combobox.text获取不了值

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.SqlClient;

namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string str = "Data Source=192.168.1.1;Database=cbyy;Uid=his;Pwd=samanagger";
            SqlConnection conn = new SqlConnection(str);
            string Sql = "select mc  from zd_yp order by mc";
            DataSet Ds = new DataSet();
            SqlDataAdapter Da = new SqlDataAdapter(Sql, conn);
            Da.Fill(Ds, "zd_ry");
            comboBox1.DataSource = Ds.Tables["zd_ry"];
            comboBox1.DisplayMember = "mc";
            comboBox1.ValueMember = "mc";
            comboBox1.SelectedValue = "mc";
        }

        public void button1_Click(object sender, EventArgs e)
        {
            string d1 = this.textBox1.Text + "-" + this.textBox2.Text + "-" + this.textBox3.Text;
            string d2 = this.textBox4.Text + "-" + this.textBox5.Text + "-" + this.textBox6.Text;
            string sql = mysql(d1, d2);
            string str = "Data Source=192.168.1.1;Database=cbyy;Uid=his;Pwd=samanagger";
            SqlConnection conn = new SqlConnection(str);
            DataSet Ds = new DataSet();
            SqlDataAdapter Da = new SqlDataAdapter(sql, conn);
            Da.Fill(Ds, " ");
            dataGridView1.DataSource = Ds.Tables[" "];
            textBox7.Text = sql;

        }





        public void textBox1_TextChanged(object sender, EventArgs e)


        {
            int i;
            if (textBox1.TextLength == 4)
            {
                this.textBox2.Focus();
            }
            if (int.TryParse(textBox1.Text, out i) == false)
            {
                MessageBox.Show("请输入正确的日期");
                textBox1.Text = "1";
                textBox1.Focus();
            }

        }

        public void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (textBox2.TextLength == 2)
            {
                this.textBox3.Focus();
            }
            int i;
            if (int.TryParse(textBox2.Text, out i) == false || Convert.ToInt32(textBox2.Text) > 12)
            {
                MessageBox.Show("请输入正确的日期");
                textBox2.Text = "1";
                textBox2.Focus();
            }

        }

        public void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (textBox3.TextLength == 2)
            {
                this.textBox4.Focus();
            }
            int i;
            if (int.TryParse(textBox3.Text, out i) == false || Convert.ToInt32(textBox3.Text) > 31)
            {
                MessageBox.Show("请输入正确的日期");
                textBox3.Text = "1";
                textBox3.Focus();


            }


        }

        public void textBox4_TextChanged(object sender, EventArgs e)
        {
            if (textBox4.TextLength == 4)
            {
                this.textBox5.Focus();
            }
            int i;
            if (int.TryParse(textBox4.Text, out i) == false)
            {
                MessageBox.Show("请输入正确的日期");
                textBox4.Text = "1";
                textBox4.Focus();
            }


        }

        public void textBox5_TextChanged(object sender, EventArgs e)
        {
            if (textBox5.TextLength == 2)
            {
                this.textBox6.Focus();
            }
            int i;
            if (int.TryParse(textBox5.Text, out i) == false || Convert.ToInt32(textBox5.Text) > 12)
            {
                MessageBox.Show("请输入正确的日期");
                textBox5.Text = "1";
                textBox5.Focus();
            }



        }

        public void textBox6_TextChanged(object sender, EventArgs e)
        {
            int i;
            if (int.TryParse(textBox6.Text, out i) == false || Convert.ToInt32(textBox6.Text) > 31)
            {
                MessageBox.Show("请输入正确的日期");
                textBox6.Text = "1";
                textBox6.Focus();


            }
            if (int.TryParse(textBox6.Text, out i) == true || Convert.ToInt32(textBox6.Text) < 10)
            {
                this.textBox6.Text = textBox6.Text.PadLeft(2, '0');
            }

        }


            static string days(DateTime date)
            {
                DateTime starttime = Convert.ToDateTime("1899-12-30");
                DateTime endtime = date;
                TimeSpan day = endtime - starttime;
                string days = Convert.ToString(day.Days);
                return days;
            }

            public static string mysql(string date1, string date2)
            {
                DateTime starttime = Convert.ToDateTime(date1);
                DateTime endtime = Convert.ToDateTime(date2);
                Form1 f = new Form1();
                string sql = "select * from zy_bhjfyz" + Convert.ToString(starttime.Year) + starttime.Month.ToString("D2") + " where flag & 16=0 and flag & 2>0 and rq between " + days(Convert.ToDateTime(date1)) + " and " + days(Convert.ToDateTime(date2)) + " and yzmc like '%" +f.comboBox1.Text+"%'";
                int y, m, i;
                m = endtime.Month - starttime.Month;
                y = endtime.Year - starttime.Year;
                if (y == 0)
                {
                    for (i = starttime.Month; i <endtime.Month; i++)
                    {
                        sql = sql + " union all " + "select  * from zy_bhjfyz" + Convert.ToString(starttime.Year) + starttime.AddMonths(i).Month.ToString("D2") + " where flag & 16=0 and flag & 2>0 and rq between " + days(Convert.ToDateTime(date1)) + " and " + days(Convert.ToDateTime(date2)) + " and yzmc like '%" + f.comboBox1.Text + "%'";


                    }
                }
                else
                {
                    for (i = starttime.Month + 1; i <= 12; i++)
                    {
                        sql = sql + " union all " + "select  * from zy_bhjfyz" + Convert.ToString(starttime.Year) + i.ToString("D2") + " where flag & 16=0 and flag & 2>0 and rq between " + days(Convert.ToDateTime(date1)) + " and " + days(Convert.ToDateTime(date2)) + " and yzmc like '%" + f.comboBox1.Text + "%'";
                    }
                    for (i = starttime.Year + 1; i < endtime.Year; i++)
                    {
                        int s;
                        for (s = 1; s <= 12; s++)
                        {
                            sql = sql + " union all " + "select  * from zy_bhjfyz" + Convert.ToString(i) + s.ToString("D2") + " where flag & 16=0 and flag & 2>0 and rq between " + days(Convert.ToDateTime(date1)) + " and " + days(Convert.ToDateTime(date2)) + " and yzmc like '%" + f.comboBox1.Text + "%'";
                        }
                    }

                    for (i = 1; i < endtime.Month; i++)
                    {
                        sql = sql + " union all " + "select  * from zy_bhjfyz" + Convert.ToString(endtime.Year) + i.ToString("D2") + " where flag & 16=0 and flag & 2>0 and rq between " + days(Convert.ToDateTime(date1)) + "and " + days(Convert.ToDateTime(date2)) + " and yzmc like '%" + f.comboBox1.Text + "%'";


                    }


                }
                return sql;
            }



        }


     


    }



函数 mysql里面的SQL获取不了combobox.text的值,这个值总是为空,请问下是怎么回事,头都炸了。。
[解决办法]
Form1 f = new Form1();
...
f.comboBox1.Text

为什么要new一个Form1,难道不是用this.comboBox1.Text么?

热点排行