Exemple #1
0
        private void btnStartWatch_Click(object sender, EventArgs e)
        {
            bool isVaild = true;

            if (string.IsNullOrEmpty(txtStationNO.Text))
            {
                errorProvider1.Clear();
                errorProvider1.SetError(txtStationNO, "Field required!");
                txtStationNO.Focus();
                isVaild = false;
            }
            else if (string.IsNullOrEmpty(txtPath.Text))
            {
                errorProvider1.Clear();
                errorProvider1.SetError(panel4, "Field required!");
                txtPath.Focus();
                isVaild = false;
            }
            else if (string.IsNullOrEmpty(cboModel.Text))
            {
                errorProvider1.Clear();
                errorProvider1.SetError(cboModel, "Field required!");
                cboModel.Focus();
                isVaild = false;
            }
            else if (isVaild == true)
            {
                Ultils.WriteRegistryKey(txtStationNO.Text, txtPath.Text);

                if (m_bIsWatching)
                {
                    m_bIsWatching = false;
                    m_Watcher.EnableRaisingEvents = false;
                    m_Watcher.Dispose();
                    txtStationNO.Enabled    = true;
                    panel4.Enabled          = true;
                    cboModel.Enabled        = true;
                    btnStartWatch.BackColor = Color.Green;
                    btnStartWatch.Text      = "Start Watching";
                    lblConnected.Visible    = false;

                    CloseConnection(fullPath);
                }
                else
                {
                    if (CheckConnection(fullPath) == true)
                    {
                        m_bIsWatching        = true;
                        txtStationNO.Enabled = false;
                        panel4.Enabled       = false;
                        cboModel.Enabled     = false;

                        btnStartWatch.BackColor = Color.Red;
                        btnStartWatch.Text      = "Stop Watching";

                        m_Watcher = new FileSystemWatcher();
                        m_Watcher.IncludeSubdirectories = true;

                        string path = Ultils.GetValueRegistryKey("PATH").ToString() + "\\";

                        m_Watcher.Filter = cboModel.Text + ".mdb";
                        m_Watcher.Path   = path;


                        m_Watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                                                 | NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastAccess;
                        m_Watcher.Changed            += new FileSystemEventHandler(OnChanged);
                        m_Watcher.Created            += new FileSystemEventHandler(OnChanged);
                        m_Watcher.EnableRaisingEvents = true;
                    }
                    else
                    {
                        string message = "Kết nối đến database thất bại. Vui lòng kiểm tra lại đường dẫn và thử lại.\nNếu vẫn không được, liên hệ với phòng IT để được hỗ trợ!";
                        errorProvider1.Clear();
                        errorProvider1.SetError(panel4, message);
                        txtPath.Focus();
                        MessageBox.Show(message);
                        return;
                    }
                }
            }
        }