簡體   English   中英

即使將 IsEnabled 屬性設置為 false,Excel 按鈕也不會被禁用

[英]Excel button is not disabled even when IsEnabled property is set to false

單擊按鈕時,我希望禁用單擊的按鈕,並在執行進程后重新啟用它。

雖然看起來很簡單,但出於某種原因,整個過程成功執行但按鈕並未禁用。 它在特定情況下被禁用,而不是在按鈕開始時被禁用。

下面是按鈕的 XAML 代碼

<Button 
    x:Name="PreviewReportButton"
    Click="PreviewExcelReportButton_Click"
    Background="{StaticResource ExportExcelButtonColor}"
    BorderBrush="{StaticResource ExportExcelButtonColor}"
    Focusable="False"
    IsEnabled="False"
    Width="80"
    Height="Auto"
    Margin="450,0,0,0"
    FontSize="9"
    FontWeight="DemiBold"
    HorizontalAlignment="Left"
    VerticalAlignment="Center"
    Grid.Column="3"
    Grid.Row="5"
    Cursor="Hand"
    VerticalContentAlignment="Center"
    HorizontalContentAlignment="Center">
    <TextBlock TextAlignment="Center">Button 1</TextBlock>
</Button>

c# VS 2019 中的 .cs 文件代碼

private void PreviewExcelReportButton_Click(object sender, RoutedEventArgs e)
{
    var btn_excel = (Button)sender;
    btn_excel.IsEnabled = false;
    //PreviewReportButton.IsEnabled = false; This is the same button as btn_excel (the button I want to click)

    Debug.WriteLine("Button must be disabled");

    try
    {
        //Check if file is open
        int IsMacroFileOpen = CheckFileIsOpen($@"{path}file_1.xlsm");
        int IsReportFileOpen = CheckFileIsOpen($@"{path}file_2.xlsm");
        int IsXLSXFileOpen = CheckFileIsOpen($@"{path}file_3.xlsx");

        Debug.WriteLine(IsMacroFileOpen);
        Debug.WriteLine(IsReportFileOpen);
        Debug.WriteLine(IsXLSXFileOpen);

        if (new[] { 1, 2 }.Contains(IsMacroFileOpen) || new[] { 1, 2 }.Contains(IsReportFileOpen) || new[] { 1, 2 }.Contains(IsXLSXFileOpen))
        {
            btn_excel.IsEnabled = true;
            return;
        }

        Mouse.OverrideCursor = Cursors.Wait;

        //Step 1: Create copy of standard report file
        CreateCopyReportServerNameDB($@"{path}file_1.xlsm");

        //Step 2: Run macro
        ExecuteExcelMacro($@"{path}file_2.xlsm");

        //Step 3: Open a copy of the xlsx updated file
        //Approach 1
        Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();

        ExcelApp.DisplayAlerts = false;
        ExcelApp.Visible = true;

        Microsoft.Office.Interop.Excel.Workbook ExcelWorkBook = ExcelApp.Workbooks.Add($"{path}file_3.xlsx");
    }
    catch (Exception)
    {
        this.Effect = new BlurEffect();
        bool? Result = new CustomMessageBox($"Unable to execute Excel button.\nPlease contact application support", "Error produced", MessageType.Error, MessageButtons.Ok).ShowDialog();
        if (Result.Value)
        {
            this.Effect = null;
            return;
        }
        else
        {
            this.Effect = null;
            return;
        }
    }
    finally
    {
        Mouse.OverrideCursor = null;
        btn.IsEnabled = true;
    }
}

不要太關注每個方法(CreateCopyReportServerNameDB、ExecuteExcelMacro)的作用,因為它與問題無關。 整個功能都有效。 不起作用的是我的代碼頂部的按鈕禁用。 當我單擊按鈕時,由於我使用了Mouse.OverrideCursor = Cursors.Wait;因此 Cursor 更改為Wait Mouse.OverrideCursor = Cursors.Wait; . 奇怪的是,該按鈕僅在CheckFileIsOpen方法CheckFileIsOpen的文件打開時才被禁用。 如果沒有打開文件,則永遠不會禁用該按鈕。 而且我確信我只在班級頂部禁用按鈕,並且只在最后啟用它。

您還會注意到,如果IsEnabled = false; ,我已經放置了一個Debug.Writeline來寫入IsEnabled = false; 成功執行,實際上該行被寫入為輸出。 盡管按鈕永遠不會被禁用,除非文件打開並被方法CheckFileIsOpen捕獲。

抱歉,我已經到了為這樣一個“虛擬”思考發布問題的地步,因為對我來說這很明顯。 但我不明白為什么按鈕的 IsEnabled 會發生這種情況。 如果這很簡單,請道歉,但我無法弄清楚發生了什么。

問題好像是我執行第二種方法的時候ExecuteExcelMacro這個方法的代碼

public void ExecuteExcelMacro(string sourceFile)
{
    var destinationFile = @"file_1";
    Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();

    ExcelWorkBook = ExcelApp.Workbooks.Open(sourceFile);

    string macro = "ThisWorkbook.Run_Code";
    try
    {
        ExcelApp.Run(macro);
        Debug.WriteLine("Macro: " + macro + " executed successfully");
    }
    catch (Exception)
    {
        this.Effect = new BlurEffect();
        bool? Result = new CustomMessageBox($"Unable to Run Macro: {macro}", "Cannot execute Macro", MessageType.Error, MessageButtons.Ok).ShowDialog();
        if (Result.Value)
        {
            this.Effect = null;
            return;
        }
        else
        {
            this.Effect = null;
            return;
        }
        //Debug.WriteLine("Unable to Run Macro: " + macro + " Exception: " + ex.Message);
    }

    ExcelApp.DisplayAlerts = false;
    ExcelApp.Visible = false;

    ExcelWorkBook.SaveAs($@"{path}{destinationFile}", Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook, Type.Missing);
    ExcelWorkBook.Close(0);
    ExcelApp.Quit();

    if (ExcelWorkBook != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelWorkBook); }
    if (ExcelApp != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp); }
}

在 ExecuteExcelMacro() 方法中,創建 ExcelApp 實例后,使ExcelApp.ScreenUpdating=false; ExcelApp.Calculation=manual; ExcelApp.DisplayAlerts=false; ExcelApp.ScreenUpdating=false; ExcelApp.Calculation=manual; ExcelApp.DisplayAlerts=false; .

不要使ExcelApp.Visible=false;

在 PreviewExcelReportButton_Click() 方法的最后一個塊中,執行Mouse.OverrideCursor = Cursors.Arrow;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM