部分技术代码

来源:互联网 发布:二手机械进口报关seo 编辑:程序博客网 时间:2024/04/29 17:33

Cursor.Current = Cursors.WaitCursor;//鼠标状态图标

 if (jxjh_zdjh.ds.HasChanges()){}    else{MessageBox.Show("没有修改过的记录", "系统提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Cursor.Current = Cursors.Default;//鼠标状态图标  }

 private void dataGridViewX1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            //if (e.RowIndex >= 0)
            //{
            //    dataGridViewX1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightPink;
            //}

        }

        private void dataGridViewX1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
        {
            //if (e.RowIndex >= 0)
            //{
            //    dataGridViewX1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;
            //}

        }


  protected override bool ProcessCmdKey(ref Message msg, Keys keyData)//键盘离开自动跳到某行的某单元格技术
        {
            if (keyData == Keys.Enter)
            {
                if (dataGridViewX1.CurrentRow.Index < dataGridViewX1.Rows.Count - 1)
                {
                    dataGridViewX1.CurrentCell = dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index + 1].Cells[1];
                    up();
                    dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index-1].DefaultCellStyle.BackColor = Color.LightSeaGreen;

                }
                else { up(); dataGridViewX1.Rows[dataGridViewX1.CurrentRow.Index].DefaultCellStyle.BackColor = Color.LightSeaGreen; }
               return true;
            }
            else
            {
                return base.ProcessCmdKey(ref msg, keyData);
            }
        }

*********************************************************************************************************************

 


1、在 ChildFrm 中定义一个事件,

C# code public delegate void RefreshEventHandle();
public event RefreshEventHandle OnRefreshEvent; 2、在 MainFrm 中确定刷新的事件处理函数;
// 显示ChildFrm之前,加上如下代码 ChildFrm.OnRefreshEvent += new RefreshEventHandle

(this.RefreshData); 3、在 MainFrm 中添加回调函数: private void RefreshData()
{
// 关闭 ChildFrm 时,想做的任何事情
}
4、在 ChildFrm 中的 ChildFrm_FormClosed 事件(即:public void net_xjcjbm_skjsxx_FormClosed(object

sender, FormClosedEventArgs e) )中加入如下代码: if (null != OnRefreshEvent)
{
this.OnRefreshEvent();}

5、总结一下: 利用事件和委托完成,比较简单


***********************************************************************
----------------------------------------可移动的panel--------------------   

        Point ptJianZhuangYuan;
        private void panelJianZhuangYuan_MouseDown(object sender, MouseEventArgs e)
        {
            ptJianZhuangYuan = Cursor.Position;
            panelJianZhuangYuan.BringToFront();
        }

        private void panelJianZhuangYuan_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)//点击鼠标左键时
            {
                int px = Cursor.Position.X - ptJianZhuangYuan.X;//计算panel的位置
                int py = Cursor.Position.Y - ptJianZhuangYuan.Y;//计算panel的位置

                px = this.panelJianZhuangYuan.Location.X + px;//计算panel的位置
                py = this.panelJianZhuangYuan.Location.Y + py;//计算panel的位置

                if (px < 0) { px = 0; }//计算panel的位置
                if (px >= this.ClientSize.Width) { px = this.ClientSize.Width -

this.panelJianZhuangYuan.Width - 10; }//计算panel的位置
                if (py >= this.ClientSize.Height) { py = this.ClientSize.Height -

this.panelJianZhuangYuan.Height - 10; }//计算panel的位置
                if (py < 0) { py = 0; }//计算panel的位置

                this.panelJianZhuangYuan.Location = new Point(px, py);//设置panel的新位置

                ptJianZhuangYuan = Cursor.Position;//赋值为鼠标所在位置
            }
        }




父子窗体之间的传值---------------------
本次示例效果如下:
Form1为父窗体(包含textBox1、button1)
Form2为子窗体(包含textBox2、button2)

父窗体给子窗体传值
==================
1.点击Form1的button1 打开Form2
  父窗体给子窗体传值 可以调用重载子窗体的构造函数 直接传入相关数值

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2(this.textBox1.Text);
            frm2.Show();
        }
    }

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public Form2(string strTextBox1Text)
        {
            InitializeComponent();
            this.textBox2.Text = strTextBox1Text;
        }
    }

2.点击Form1的button1 打开Form2
  并调用子窗体Form2的公开属性或方法 将Form1的textBox1的值设置给Form2的textBox2
 
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.TextBox2Text = this.textBox1.Text;
            frm2.Show();
        }
    }

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public string TextBox2Text
        {
            set { this.textBox2.Text = value; }
            get { return this.textBox2.Text; }
        }     
    }
 
3.点击Form1的button1 打开Form2
  在Form2_Load调用父窗体Form1的公开属性或方法 将Form1的textBox1的值设置给Form2的textBox2

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public string TextBox1Text
        {
            set { this.textBox1.Text = value; }
            get { return this.textBox1.Text;  }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Show(this);//或 frm2.ShowDialog(this);

            ////或者
            //Form2 frm2 = new Form2();
            //frm2.Owner = this;
            //frm2.Show();//或 frm2.ShowDialog();
        }
    }
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            Form1 frm1 = (Form1)this.Owner;
            this.textBox2.Text = frm1.TextBox1Text;
        }
    }

子窗体给父窗体传值
==================
4.点击Form1的button1 打开Form2
  再点击Form2的button2
    在button2_Click事件中 通过this.Owner将Form2的textBox2的值设置给Form1的textBox1
    并关闭Form2

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Show(this);//或 frm2.ShowDialog(this);

            ////或者
            //Form2 frm2 = new Form2();
            //frm2.Owner = this;
            //frm2.Show();//或 frm2.ShowDialog();
        }
    }

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form1 frm1 = (Form1)this.Owner;
     //注意 如果textBox1是放在panel1中的 则先找panel1 再找textBox1
            ((TextBox)frm1.Controls["textBox1"]).Text = this.textBox2.Text;
            this.Close();
        }
    }

5.点击Form1的button1 打开Form2
  再点击Form2的button2
    在button2_Click事件中 通过this.Owner及调用父窗体Form1的公开属性或方法
                          将Form2的textBox2的值设置给Form1的textBox1
    并关闭Form2
 
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public string TextBox1Text
        {
            set { this.textBox1.Text = value; }
            get { return this.textBox1.Text;  }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Show(this);//或 frm2.ShowDialog(this);

            ////或者
            //Form2 frm2 = new Form2();
            //frm2.Owner = this;
            //frm2.Show();//或 frm2.ShowDialog();
        }
    }

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form1 frm1 = (Form1)this.Owner;
            frm1.TextBox1Text = this.textBox2.Text;
            this.Close();
        }
    }




动态创建选项卡------------------------

//动态加载方法(双击Windows窗体自动生成)
public void MainFrom_Load(object sender, EventArgs e)
        {
            //添加状态栏信息
      //tss1,和tss2是状态栏组件
      this.tss1.Text = "酒店管理系统!!!";
            this.tss2.Text = "当前操作员:";

            //添加下拉框
      this.cboxs.Items.Add("显示可供");
            this.cboxs.Items.Add("显示占用");
            this.cboxs.Items.Add("显示停用");
            this.cboxs.Items.Add("显示预订");
            this.cboxs.Items.Add("显示清理");
            this.cboxs.Items.Add("显示全部");
            this.cboxs.SelectedIndex = 5;

            //先放一个空的选项卡到(不是空的也可以)
            //清空选项卡
            this.tctkefang.TabPages.Clear();

            //添加全部客房
            DataTable dt = kftt.LeixingSelectData();//(查询数据库)

            //获得数据行
            int rs = dt.Rows.Count;
            if (rs == 0)
            {
                MessageBox.Show("系统内暂无客房类型,请尽快添加客房类型!!!");
            }
            else
            {
                for (int i = 0; i < rs; i++)
                {
                    //new 一个新的选项卡卡片
                    TabPage tgg = new TabPage();

                    //修改text
                    tgg.Text = dt.Rows["k_type"].ToString();

                   // 添加到选项卡中
                    this.tctkefang.TabPages.Add(tgg);

                    //当选项卡选定的卡片改变时的事件
                    tctkefang.Click+=new EventHandler(tctkefang_Click);

                                       //new一个新的ListView
                    ListView lv = new ListView();

                    //添加ListView的点击事件
                    lv.Click+=new EventHandler(lv_Click);

                    //鼠标移上事件
                    lv.MouseMove+=new MouseEventHandler(lv_MouseMove);

          //设置ListView的属性
          lv.Dock = DockStyle.Fill;
                    lv.View = View.LargeIcon;

                    //添加图片
                    lv.LargeImageList = imageList1;

                    //把ListView添加添加到选项卡中
                    tgg.Controls.Add(lv);

                    //添加ListView的内容(查询数据库)
                    DataTable dtt = kft.QSelectData(tgg.Text);
                   
                    for (int ii = 0; ii < dtt.Rows.Count;ii++ )
                    {
                       //新建ListView的新行,并添加内容
                        ListViewItem lvv = new ListViewItem();
                        lvv.SubItems[0].Text=dtt.Rows[ii][0].ToString();
                        lvv.SubItems.Add(dtt.Rows[ii][1].ToString());
                        lvv.SubItems.Add(dtt.Rows[ii][2].ToString());
                        lvv.SubItems.Add(dtt.Rows[ii][3].ToString());
                        lvv.SubItems.Add(dtt.Rows[ii][4].ToString());
                        lvv.SubItems.Add(dtt.Rows[ii][5].ToString());
                        DataTable dttt = null;
                        try
                        {
                            //查询数据库(以下代码是给相应的行添加不同图片)
                            dttt = kft.KlSelectData(dtt.Rows[ii][5].ToString());
                        }
                        catch
                        {
                        }
                        if (dtt.Rows[ii][3].ToString() == "空闲")
                        {
                            //设置相应的图片
                            lvv.ImageIndex = 0;
                        }
                        else if (dtt.Rows[ii][3].ToString() == "预订")
                        {
                            lvv.ImageIndex = 3;
                        }
                        else if (dtt.Rows[ii][3].ToString() == "停用")
                        {
                            lvv.ImageIndex = 4;
                        }
                        else if (dtt.Rows[ii][3].ToString() == "清理")
                        {
                            lvv.ImageIndex = 5;
                        }
                        else if (dttt.Rows.Count == 1)//要动态获得值,不能为特定的值(“1”)
                        {
                            lvv.ImageIndex = 1;
                        }

                        //未确定团体开单的客房数
                        else if (dttt.Rows.Count > 1)//所大于的值要动态获得
                        {
                            lvv.ImageIndex = 2;
                        }

                        lv.Items.Add(lvv);
                    }
                }
            }
            try
            {
                this.lblkflx.Text = this.tctkefang.TabPages[0].Text;
            }
            catch
            {

            }
           
        }

//当选项卡选定的卡片改变时的事件
        public void tctkefang_Click(object sender, EventArgs e)
        {
            //可以做你想做的内容
        }

//添加ListView的点击事件
        public void lv_Click(object sender, EventArgs e)
        {
           //可以写你想写的内容
        }

//鼠标移上事件
        public void lv_MouseMove(object sender, EventArgs e)
        {
           
            //我的地盘听我的              
        }




-----------------------------combobox 绑定 -----------
SqlDataReader dr = ZhuangHuoLuRu.TiQuDingDanDuanBoZhuangHuoDian(DuanBoDaiMa, false);
                comboBoxZhuangHuoDian.Items.Clear();
                comboBoxZhuangHuoDian.Visible = true;
                while (dr.Read())
                {                  
                    ZhuangHuoDianJI.Add(dr["ZhuangXieDianDaiMa"]);
                    comboBoxZhuangHuoDian.Items.Add(dr["ZhuangXieDianMingCheng"]);
                }
                dr.Close();

********************************************************