Home > Enterprise >  Old form still display winforms c#
Old form still display winforms c#

Time:06-17

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

CodePudding user response:

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.

  • Related