2

I implement a form for handle excel file when click button "Start". Event click Start button:

private void btnImport_Click(object sender, EventArgs e)
        {
            showFormSelectLanguage();
            if (CheckSheetFile() == true) {
                using (WaitingForm frm = new WaitingForm(handleExcel))
                {
                    frm.ShowDialog(this);
                }
                var dialogMessage = new DialogMessage();
                dialogMessage.ShowDialog(this);
            } else
            {
                ShowDialogNotFoundSheet();
            }
        }

showFormSelectLanguage method display dialog for select language:

private void showFormSelectLanguage()
        {
            var formSelectLanguage = new FormSelectLanguage();
            formSelectLanguage.ShowDialog(this);
        }

ShowDialogNotFoundSheet function for check sheet excel exist:

private void ShowDialogNotFoundSheet()
        {
            var dialogNotFoundSheet = new DialogNotFoundSheet();
            dialogNotFoundSheet.setTextContent("Not found sheet");
            dialogNotFoundSheet.ShowDialog(this);
        }

Event click confirm select language button at Select language form:

private void btnConfirmLanguage_Click(object sender, EventArgs e)
        {
            //close dialog
            this.Close();
        }

Event click Close button for close DialogNotFoundSheet form:

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

CheckSheetFile method:

private bool CheckSheetFile()
        {
            var isCorrectFile = false;
            try
            {
                xlWorkBook = xlApp.Workbooks.Open(txtFilePath.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                var xlWorkBook1 = xlWorkBook.Sheets["SheetName"];
                isCorrectFile = true;
            }
            catch (Exception e)
            {
                return false;
            }
            return isCorrectFile;
        }

Issue: When I click Close button at DialogNotFoundSheet form. Then FormSelectLanguage from still display. It repeats. How can resolve it? Expected 2 forms can close Thanks!

Update: All References btnImport_Click: References btnImport_Click

UI: FormSelectLanguage DialogNotFoundSheet

Phan Kieu Hung
  • 123
  • 1
  • 7

1 Answers1

1

I don't exactly know what you did with btnImport_Click, but if your purpose is to disable the function of a button at a time and to enable it at another time, actually you don't have to register or unregister the click event, you can simply set button's Enabled propety.

//btnImport.Click += btnImport_Click;
btnImport.Enabled = true;

//btnImport.Click -= btnImport_Click;
btnImport.Enabled = false;

My guess of the reason of this loop is that you have called += btnImport_Click many times, but -= btnImport_Click is never (or less) run.

For instance if you do:

btnImport.Click += btnImport_Click;
btnImport.Click += btnImport_Click;

Each time btnImport is clicked, btnImport_Click will get invoked twice.

shingo
  • 18,436
  • 5
  • 23
  • 42
  • Thank bro. But when I set enable = false then Mouse_Move event not working. For simple, I don't using Mouse_Move event (for handle cursor display type) and remove code: `btnImport.Click += btnImport_Click; btnImport.Click += btnImport_Click;` – Phan Kieu Hung Jun 17 '22 at 04:12
  • 1
    MouseMove on a button? OK, you can keep registering the click event, just remember that when the form is loaded, `btnImport_Click` is already listened once, and you must ensure that you call `+= btnImport_Click` as many times as call `-= btnImport_Click`. – shingo Jun 17 '22 at 04:18
  • Ok bro. This is root cause form repeats. Thank you! I resolved it. :D – Phan Kieu Hung Jun 17 '22 at 04:22