小编Jon*_*han的帖子

ExpectedException属性用法

我正在尝试使用a中的ExpectedException属性C# UnitTest,但我遇到问题让它与我的特定工作Exception.这是我得到的:

注意:我在线路周围包裹了星号,这给我带来了麻烦.

    [ExpectedException(typeof(Exception))]
    public void TestSetCellContentsTwo()
    {
        // Create a new Spreadsheet instance for this test:
        SpreadSheet = new Spreadsheet();

        // If name is null then an InvalidNameException should be thrown. Assert that the correct 
        // exception was thrown.
        ReturnVal = SpreadSheet.SetCellContents(null, "String Text");
        **Assert.IsTrue(ReturnVal is InvalidNameException);**

        // If text is null then an ArgumentNullException should be thrown. Assert that the correct
        // exception was thrown.
        ReturnVal = SpreadSheet.SetCellContents("A1", (String) null);
        Assert.IsTrue(ReturnVal …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing exception expected-exception

24
推荐指数
1
解决办法
3万
查看次数

用于关闭Windows窗体应用程序的事件处理程序

我试图创建一个event handler这样的用户,当用户选择close(x)按钮时,它会提示用户保存任何未保存的更改.这是我的C#代码:

private void CloseFileOperation()
{
    // If the Spreadsheet has been changed since the user opened it and
    // the user has requested to Close the window, then prompt him to Save
    // the unsaved changes.
    if (SpreadSheet.Changed)
    {
        DialogResult UserChoice = MessageBox.Show("Would you like to save your changes?", "Spreadsheet Utility",
                MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);

        switch (UserChoice)
        {
            case DialogResult.Yes:
                SaveFileOperation();
                this.Close();
                break;
            case DialogResult.No:
                this.Close();
                break;
            case DialogResult.Cancel:
                return;
        }
    }

    // If the …
Run Code Online (Sandbox Code Playgroud)

c# forms validation savechanges winforms

1
推荐指数
1
解决办法
2788
查看次数