已有56人关注
每次点父窗口菜单,打开一个子窗口,可下方的notifyIcon1图标就会多出一个
发表在C#图书答疑 2010-07-17
是否精华
版块置顶:
        private void ShowNewForm(object sender, EventArgs e)
        {
            frm_Networking_Maintenance childForm = new frm_Networking_Maintenance();
            childForm.MdiParent = this;            
            childForm.Show();
            this.notifyIcon1.Visible = false; [font color=#FF0000]//加了这句都没有用,每次点父窗口菜单,打开一个子窗口,可下方的notifyIcon1图标就会多出一个,要关了再点,又会再多出一个图标,要怎样作才可以让程序只显示一个图标呀?请老师指点一下,谢谢。[/font]
        }






[font color=#FF0000]//以下是完整的代码[/font]

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 IT部门管理系统
{
    public partial class frm_main : Form
    {
        

        public frm_main()
        {
            InitializeComponent();
        }

        private void ShowNewForm(object sender, EventArgs e)
        {
            frm_Networking_Maintenance childForm = new frm_Networking_Maintenance();
            childForm.MdiParent = this;            
            childForm.Show();
            this.notifyIcon1.Visible = false;
        }

        private void OpenFile(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            openFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = openFileDialog.FileName;
            }
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }               

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frm_dbConf frmdb = new frm_dbConf();    //显示主窗体
            frmdb.Show(); 
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            tsslSTime.Text = "系统时间:" + DateTime.Now.ToString();
        }

        private void frm_main_Load(object sender, EventArgs e)
        {

            frm_note childForm = new frm_note();
            childForm.MdiParent = this;
            childForm.Show();

            timer1.Start();
            tsslLTime.Text = "|登录时间:" + DateTime.Now.ToShortTimeString();
            tsslSTime.Text = "|系统时间:" + DateTime.Now.ToString();

            this.notifyIcon1.Visible = false;
            
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            //if (MessageBox.Show("将要要关闭窗体,是否继续?", "询问", MessageBoxButtons.YesNo) == DialogResult.Yes)
            //{
            //    e.Cancel = false;
            //}
            //else
            //{
            //    e.Cancel = true;
            //}

            e.Cancel = true;
            this.Hide();
            this.notifyIcon1.Visible = true;  
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.Visible == true)   //根据窗口的显示和隐藏设置Trayico对象的显示   
            {
                this.Hide();
                this.notifyIcon1.Visible = true;
            }
            else
            {
                this.Show();
                this.notifyIcon1.Visible = false;
            }   

        }

        private void 退出系统ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            Application.ExitThread(); 
            this.notifyIcon1.Visible = false;
        }

        private void 打开主界面ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            this.notifyIcon1.Visible = false;
        }


    }
}


分享到:
精彩评论 5
小科_mrkj
学分:43 LV2
2010-07-17
沙发
读者朋友:
    您好,首先判断notifyIcon控件是否显示,如果显示,则隐藏掉,否则,显示该控件。
wufayou
学分:0 LV1
TA的每日心情
第五天
2023-03-18 21:24:17
2010-07-18
板凳
[font color=#FF0000] 我找到了,调用的子窗体加载时,如果dataGridView1绑定了数据,下方的notifyIcon1图标就会多出一个,代码如下,这代码有问题吗?我要怎样改,才不会多一个图标呢?请老师指点一下,谢谢。
[/font]
       private void frm_Networking_Maintenance_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“iTSysDataSet.tb_user”中。您可以根据需要移动或移除它。            
            cls_dbOpSql dbOp = new cls_dbOpSql();
            DataSet ds = new DataSet();
            ds = dbOp.getDataSet("select * from tb_user", "tb_user");
            dataGridView1.DataSource = ds.Tables[0];   
            
        }


//数据调用类代码如下:

 public DataSet getDataSet(string SQLstr, string tableName)
        {
            getcon();   //打开与数据库的连接
            SqlDataAdapter SQLda = new SqlDataAdapter(SQLstr, My_con);  //创建一个SqlDataAdapter对象,并获取指定数据表的信息
            DataSet My_DataSet = new DataSet(); //创建DataSet对象
            SQLda.Fill(My_DataSet, tableName);  //通过SqlDataAdapter对象的Fill()方法,将数据表信息添加到DataSet对象中
            con_close();    //关闭数据库的连接
            return My_DataSet;  //返回DataSet对象的信息

            //WritePrivateProfileString(string section, string key, string val, string filePath);
        }
小科_mrkj
学分:43 LV2
2010-07-19
地板
读者朋友:
    您好,这段代码没有问题,代码里面没有涉及到提示控件。你还是先判断notifyIcon控件是否显示,然后根据这个控件的显示状态确定是否重新显示或者隐藏它。
wufayou
学分:0 LV1
TA的每日心情
第五天
2023-03-18 21:24:17
2010-07-19
4L
我又新建了一个用户管理的窗体,调用就不会多图标,也是数据绑定的。也有dataGridView1。实在看不出,我再重新新建一个窗口,看下。
小科_mrkj
学分:43 LV2
2010-07-19
5L
读者朋友:
    您好,图标控件是否显示跟数据绑定没有关系,肯定是你那块多写了显示的代码,好好检查一下你的代码。
首页上一页 1 下一页尾页 5 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照