无法在源代码Visual Studio中获得代码

时间:2017-08-29 13:35:26

标签: c#

我知道有很多方法可以到罗马,但我想知道我用我所用的书中复制的代码做错了什么。我想我知道答案,但不知道如何纠正它。

GUI:

GUI

当我从书中复制代码时,它没有运行,我得到的结果如下:

Result

对我有用的代码是:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RadioButtonsTestForm
{
    public partial class RadioButtonsTestForm : Form
    {
        private MessageBoxIcon iconType;
        private MessageBoxButtons buttonType;

        public RadioButtonsTestForm()
        {
            InitializeComponent();
        }        

        private void displayButton_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("This is your Custom MessageBox.", "Custom MessageBox", buttonType, iconType);

            switch (result)
            {
                case DialogResult.OK:
                    displayLabel.Text = "OK was pressed.";
                    break;
                case DialogResult.Cancel:
                    displayLabel.Text = "Cancel was pressed.";
                    break;
                case DialogResult.Abort:
                    displayLabel.Text = "Abort was pressed.";
                    break;
                case DialogResult.Retry:
                    displayLabel.Text = "Retry was pressed.";
                    break;
                case DialogResult.Ignore:
                    displayLabel.Text = "Ignore was pressed.";
                    break;
                case DialogResult.Yes:
                    displayLabel.Text = "Yes was pressed.";
                    break;
                case DialogResult.No:
                    displayLabel.Text = "No was pressed.";
                    break;
            }
        }

        private void okRadio_CheckedChanged(object sender, EventArgs e)
        {
            buttonType=MessageBoxButtons.OK;
        }

        private void okCancelRadio_CheckedChanged(object sender, EventArgs e)
        {
            buttonType = MessageBoxButtons.OKCancel;
        }

        private void abortRadio_CheckedChanged(object sender, EventArgs e)
        {
            buttonType = MessageBoxButtons.AbortRetryIgnore;
        }

        private void yesNoCancelRadio_CheckedChanged(object sender, EventArgs e)
        {
            buttonType = MessageBoxButtons.YesNoCancel;
        }

        private void yesNoRadio_CheckedChanged(object sender, EventArgs e)
        {
            buttonType = MessageBoxButtons.YesNo;
        }

        private void retryRadio_CheckedChanged(object sender, EventArgs e)
        {
            buttonType = MessageBoxButtons.RetryCancel;
        }

        private void asteriskRadio_CheckedChanged(object sender, EventArgs e)
        {
            iconType = MessageBoxIcon.Asterisk;
        }

        private void errorRadio_CheckedChanged(object sender, EventArgs e)
        {
            iconType = MessageBoxIcon.Error;
        }

        private void exclamationRadio_CheckedChanged(object sender, EventArgs e)
        {
            iconType = MessageBoxIcon.Exclamation;
        }

        private void handRadio_CheckedChanged(object sender, EventArgs e)
        {
            iconType = MessageBoxIcon.Hand;
        }

        private void informationRadio_CheckedChanged(object sender, EventArgs e)
        {
            iconType = MessageBoxIcon.Information;
        }

        private void questionRadio_CheckedChanged(object sender, EventArgs e)
        {
            iconType = MessageBoxIcon.Question;
        }

        private void stopRadio_CheckedChanged(object sender, EventArgs e)
        {
            iconType = MessageBoxIcon.Stop;
        }

        private void warningRadio_CheckedChanged(object sender, EventArgs e)
        {
            iconType = MessageBoxIcon.Warning;
        }        
    }
}

本书中使用的代码是:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RadioButtonsTest_3
{
    public partial class RadioButtonTestForm : Form
    {
        private MessageBoxButtons ButtonType;
        private MessageBoxIcon IconType;

        public RadioButtonTestForm()
        {
            InitializeComponent();
        }

        private void ButtonType_CheckedChanged(object sender, EventArgs e)
        {
            if (sender == OKRadioButton)
                ButtonType = MessageBoxButtons.OK;
            else if (sender == OKCancelRadioButton)
                ButtonType = MessageBoxButtons.OKCancel;
            else if (sender == AbortRetryIgnoreRadioButton)
                ButtonType = MessageBoxButtons.AbortRetryIgnore;
            else if (sender == YesNoCancelRadioButton)
                ButtonType = MessageBoxButtons.YesNoCancel;
            else if (sender == YesNoRadioButton)
                ButtonType = MessageBoxButtons.YesNo;
            else 
                ButtonType = MessageBoxButtons.RetryCancel;
        }

        private void IconType_CheckedChanged(object sender, EventArgs e)
        {
            if (sender == AsteriskRadioButton)
                IconType = MessageBoxIcon.Asterisk;
            else if (sender == ErrorRadioButton)
                IconType = MessageBoxIcon.Error;
            else if (sender == ExclamationRadioButton)
                IconType = MessageBoxIcon.Exclamation;
            else if (sender == HandRadioButton)
                IconType = MessageBoxIcon.Hand;
            else if (sender == InformationRadioButton)
                IconType = MessageBoxIcon.Information;
            else if (sender == QuestionRadioButton)
                IconType = MessageBoxIcon.Question;
            else if (sender == StopRadioButton)
                IconType = MessageBoxIcon.Stop;
            else 
                IconType = MessageBoxIcon.Warning;
        }

        private void DisplayButton_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("This is your Custom MessageBox.", "Custom MessageBox", ButtonType, IconType);

            switch (result)
            {
                case DialogResult.OK:
                    OutputLabel.Text = "OK was pressed.";
                    break;
                case DialogResult.Cancel:
                    OutputLabel.Text = "Cancel was pressed.";
                    break;
                case DialogResult.Abort:
                    OutputLabel.Text = "Abort was pressed.";
                    break;
                case DialogResult.Retry:
                    OutputLabel.Text = "Retry was pressed.";
                    break;
                case DialogResult.Ignore:
                    OutputLabel.Text = "Ignore was pressed.";
                    break;
                case DialogResult.Yes:
                    OutputLabel.Text = "Yes was pressed.";
                    break;
                case DialogResult.No:
                    OutputLabel.Text = "No was pressed.";
                    break;
            }
        }
    }
}

我自己也不确定,但我认为错误是如何获取ButtonType / IconType_CheckedChanged(只是在代码中输入)。双击DisplayButton获取DisplayButton_Clicked,但我不知道如何使用Button / IconType。

编辑:

Designercode:

namespace RadioButtonsTest_3
{
    partial class RadioButtonTestForm
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.ButtonGroupBox = new System.Windows.Forms.GroupBox();
            this.IconGroupBox = new System.Windows.Forms.GroupBox();
            this.DisplayButton = new System.Windows.Forms.Button();
            this.OutputLabel = new System.Windows.Forms.Label();
            this.OKRadioButton = new System.Windows.Forms.RadioButton();
            this.OKCancelRadioButton = new System.Windows.Forms.RadioButton();
            this.AbortRetryIgnoreRadioButton = new System.Windows.Forms.RadioButton();
            this.YesNoCancelRadioButton = new System.Windows.Forms.RadioButton();
            this.YesNoRadioButton = new System.Windows.Forms.RadioButton();
            this.RetryCancelRadioButton = new System.Windows.Forms.RadioButton();
            this.AsteriskRadioButton = new System.Windows.Forms.RadioButton();
            this.ErrorRadioButton = new System.Windows.Forms.RadioButton();
            this.ExclamationRadioButton = new System.Windows.Forms.RadioButton();
            this.HandRadioButton = new System.Windows.Forms.RadioButton();
            this.InformationRadioButton = new System.Windows.Forms.RadioButton();
            this.QuestionRadioButton = new System.Windows.Forms.RadioButton();
            this.StopRadioButton = new System.Windows.Forms.RadioButton();
            this.WarningRadioButton = new System.Windows.Forms.RadioButton();
            this.ButtonGroupBox.SuspendLayout();
            this.IconGroupBox.SuspendLayout();
            this.SuspendLayout();
            // 
            // ButtonGroupBox
            // 
            this.ButtonGroupBox.Controls.Add(this.RetryCancelRadioButton);
            this.ButtonGroupBox.Controls.Add(this.YesNoRadioButton);
            this.ButtonGroupBox.Controls.Add(this.YesNoCancelRadioButton);
            this.ButtonGroupBox.Controls.Add(this.AbortRetryIgnoreRadioButton);
            this.ButtonGroupBox.Controls.Add(this.OKCancelRadioButton);
            this.ButtonGroupBox.Controls.Add(this.OKRadioButton);
            this.ButtonGroupBox.Location = new System.Drawing.Point(22, 13);
            this.ButtonGroupBox.Name = "ButtonGroupBox";
            this.ButtonGroupBox.Size = new System.Drawing.Size(143, 172);
            this.ButtonGroupBox.TabIndex = 0;
            this.ButtonGroupBox.TabStop = false;
            this.ButtonGroupBox.Text = "ButtonType";
            // 
            // IconGroupBox
            // 
            this.IconGroupBox.Controls.Add(this.WarningRadioButton);
            this.IconGroupBox.Controls.Add(this.StopRadioButton);
            this.IconGroupBox.Controls.Add(this.QuestionRadioButton);
            this.IconGroupBox.Controls.Add(this.InformationRadioButton);
            this.IconGroupBox.Controls.Add(this.HandRadioButton);
            this.IconGroupBox.Controls.Add(this.ExclamationRadioButton);
            this.IconGroupBox.Controls.Add(this.ErrorRadioButton);
            this.IconGroupBox.Controls.Add(this.AsteriskRadioButton);
            this.IconGroupBox.Location = new System.Drawing.Point(171, 13);
            this.IconGroupBox.Name = "IconGroupBox";
            this.IconGroupBox.Size = new System.Drawing.Size(137, 211);
            this.IconGroupBox.TabIndex = 1;
            this.IconGroupBox.TabStop = false;
            this.IconGroupBox.Text = "Icon";
            // 
            // DisplayButton
            // 
            this.DisplayButton.Location = new System.Drawing.Point(22, 191);
            this.DisplayButton.Name = "DisplayButton";
            this.DisplayButton.Size = new System.Drawing.Size(143, 33);
            this.DisplayButton.TabIndex = 2;
            this.DisplayButton.Text = "Display";
            this.DisplayButton.UseVisualStyleBackColor = true;
            this.DisplayButton.Click += new System.EventHandler(this.DisplayButton_Click);
            // 
            // OutputLabel
            // 
            this.OutputLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.OutputLabel.Location = new System.Drawing.Point(22, 227);
            this.OutputLabel.Name = "OutputLabel";
            this.OutputLabel.Size = new System.Drawing.Size(286, 23);
            this.OutputLabel.TabIndex = 3;
            // 
            // OKRadioButton
            // 
            this.OKRadioButton.AutoSize = true;
            this.OKRadioButton.Location = new System.Drawing.Point(7, 20);
            this.OKRadioButton.Name = "OKRadioButton";
            this.OKRadioButton.Size = new System.Drawing.Size(40, 17);
            this.OKRadioButton.TabIndex = 0;
            this.OKRadioButton.TabStop = true;
            this.OKRadioButton.Text = "OK";
            this.OKRadioButton.UseVisualStyleBackColor = true;
            // 
            // OKCancelRadioButton
            // 
            this.OKCancelRadioButton.AutoSize = true;
            this.OKCancelRadioButton.Location = new System.Drawing.Point(7, 44);
            this.OKCancelRadioButton.Name = "OKCancelRadioButton";
            this.OKCancelRadioButton.Size = new System.Drawing.Size(73, 17);
            this.OKCancelRadioButton.TabIndex = 1;
            this.OKCancelRadioButton.TabStop = true;
            this.OKCancelRadioButton.Text = "OKCancel";
            this.OKCancelRadioButton.UseVisualStyleBackColor = true;
            // 
            // AbortRetryIgnoreRadioButton
            // 
            this.AbortRetryIgnoreRadioButton.AutoSize = true;
            this.AbortRetryIgnoreRadioButton.Location = new System.Drawing.Point(7, 68);
            this.AbortRetryIgnoreRadioButton.Name = "AbortRetryIgnoreRadioButton";
            this.AbortRetryIgnoreRadioButton.Size = new System.Drawing.Size(105, 17);
            this.AbortRetryIgnoreRadioButton.TabIndex = 2;
            this.AbortRetryIgnoreRadioButton.TabStop = true;
            this.AbortRetryIgnoreRadioButton.Text = "AbortRetryIgnore";
            this.AbortRetryIgnoreRadioButton.UseVisualStyleBackColor = true;
            // 
            // YesNoCancelRadioButton
            // 
            this.YesNoCancelRadioButton.AutoSize = true;
            this.YesNoCancelRadioButton.Location = new System.Drawing.Point(7, 92);
            this.YesNoCancelRadioButton.Name = "YesNoCancelRadioButton";
            this.YesNoCancelRadioButton.Size = new System.Drawing.Size(90, 17);
            this.YesNoCancelRadioButton.TabIndex = 3;
            this.YesNoCancelRadioButton.TabStop = true;
            this.YesNoCancelRadioButton.Text = "YesNoCancel";
            this.YesNoCancelRadioButton.UseVisualStyleBackColor = true;
            // 
            // YesNoRadioButton
            // 
            this.YesNoRadioButton.AutoSize = true;
            this.YesNoRadioButton.Location = new System.Drawing.Point(7, 116);
            this.YesNoRadioButton.Name = "YesNoRadioButton";
            this.YesNoRadioButton.Size = new System.Drawing.Size(57, 17);
            this.YesNoRadioButton.TabIndex = 4;
            this.YesNoRadioButton.TabStop = true;
            this.YesNoRadioButton.Text = "YesNo";
            this.YesNoRadioButton.UseVisualStyleBackColor = true;
            // 
            // RetryCancelRadioButton
            // 
            this.RetryCancelRadioButton.AutoSize = true;
            this.RetryCancelRadioButton.Location = new System.Drawing.Point(7, 140);
            this.RetryCancelRadioButton.Name = "RetryCancelRadioButton";
            this.RetryCancelRadioButton.Size = new System.Drawing.Size(83, 17);
            this.RetryCancelRadioButton.TabIndex = 5;
            this.RetryCancelRadioButton.TabStop = true;
            this.RetryCancelRadioButton.Text = "RetryCancel";
            this.RetryCancelRadioButton.UseVisualStyleBackColor = true;
            // 
            // AsteriskRadioButton
            // 
            this.AsteriskRadioButton.AutoSize = true;
            this.AsteriskRadioButton.Location = new System.Drawing.Point(7, 20);
            this.AsteriskRadioButton.Name = "AsteriskRadioButton";
            this.AsteriskRadioButton.Size = new System.Drawing.Size(62, 17);
            this.AsteriskRadioButton.TabIndex = 0;
            this.AsteriskRadioButton.TabStop = true;
            this.AsteriskRadioButton.Text = "Asterisk";
            this.AsteriskRadioButton.UseVisualStyleBackColor = true;
            // 
            // ErrorRadioButton
            // 
            this.ErrorRadioButton.AutoSize = true;
            this.ErrorRadioButton.Location = new System.Drawing.Point(7, 44);
            this.ErrorRadioButton.Name = "ErrorRadioButton";
            this.ErrorRadioButton.Size = new System.Drawing.Size(47, 17);
            this.ErrorRadioButton.TabIndex = 1;
            this.ErrorRadioButton.TabStop = true;
            this.ErrorRadioButton.Text = "Error";
            this.ErrorRadioButton.UseVisualStyleBackColor = true;
            // 
            // ExclamationRadioButton
            // 
            this.ExclamationRadioButton.AutoSize = true;
            this.ExclamationRadioButton.Location = new System.Drawing.Point(7, 68);
            this.ExclamationRadioButton.Name = "ExclamationRadioButton";
            this.ExclamationRadioButton.Size = new System.Drawing.Size(82, 17);
            this.ExclamationRadioButton.TabIndex = 2;
            this.ExclamationRadioButton.TabStop = true;
            this.ExclamationRadioButton.Text = "Exclamation";
            this.ExclamationRadioButton.UseVisualStyleBackColor = true;
            // 
            // HandRadioButton
            // 
            this.HandRadioButton.AutoSize = true;
            this.HandRadioButton.Location = new System.Drawing.Point(7, 92);
            this.HandRadioButton.Name = "HandRadioButton";
            this.HandRadioButton.Size = new System.Drawing.Size(51, 17);
            this.HandRadioButton.TabIndex = 3;
            this.HandRadioButton.TabStop = true;
            this.HandRadioButton.Text = "Hand";
            this.HandRadioButton.UseVisualStyleBackColor = true;
            // 
            // InformationRadioButton
            // 
            this.InformationRadioButton.AutoSize = true;
            this.InformationRadioButton.Location = new System.Drawing.Point(7, 116);
            this.InformationRadioButton.Name = "InformationRadioButton";
            this.InformationRadioButton.Size = new System.Drawing.Size(77, 17);
            this.InformationRadioButton.TabIndex = 4;
            this.InformationRadioButton.TabStop = true;
            this.InformationRadioButton.Text = "Information";
            this.InformationRadioButton.UseVisualStyleBackColor = true;
            // 
            // QuestionRadioButton
            // 
            this.QuestionRadioButton.AutoSize = true;
            this.QuestionRadioButton.Location = new System.Drawing.Point(7, 140);
            this.QuestionRadioButton.Name = "QuestionRadioButton";
            this.QuestionRadioButton.Size = new System.Drawing.Size(67, 17);
            this.QuestionRadioButton.TabIndex = 5;
            this.QuestionRadioButton.TabStop = true;
            this.QuestionRadioButton.Text = "Question";
            this.QuestionRadioButton.UseVisualStyleBackColor = true;
            // 
            // StopRadioButton
            // 
            this.StopRadioButton.AutoSize = true;
            this.StopRadioButton.Location = new System.Drawing.Point(7, 164);
            this.StopRadioButton.Name = "StopRadioButton";
            this.StopRadioButton.Size = new System.Drawing.Size(47, 17);
            this.StopRadioButton.TabIndex = 6;
            this.StopRadioButton.TabStop = true;
            this.StopRadioButton.Text = "Stop";
            this.StopRadioButton.UseVisualStyleBackColor = true;
            // 
            // WarningRadioButton
            // 
            this.WarningRadioButton.AutoSize = true;
            this.WarningRadioButton.Location = new System.Drawing.Point(7, 188);
            this.WarningRadioButton.Name = "WarningRadioButton";
            this.WarningRadioButton.Size = new System.Drawing.Size(65, 17);
            this.WarningRadioButton.TabIndex = 7;
            this.WarningRadioButton.TabStop = true;
            this.WarningRadioButton.Text = "Warning";
            this.WarningRadioButton.UseVisualStyleBackColor = true;
            // 
            // RadioButtonTestForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(320, 394);
            this.Controls.Add(this.OutputLabel);
            this.Controls.Add(this.DisplayButton);
            this.Controls.Add(this.IconGroupBox);
            this.Controls.Add(this.ButtonGroupBox);
            this.Name = "RadioButtonTestForm";
            this.Text = "RadioButtons";
            this.ButtonGroupBox.ResumeLayout(false);
            this.ButtonGroupBox.PerformLayout();
            this.IconGroupBox.ResumeLayout(false);
            this.IconGroupBox.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.GroupBox ButtonGroupBox;
        private System.Windows.Forms.RadioButton RetryCancelRadioButton;
        private System.Windows.Forms.RadioButton YesNoRadioButton;
        private System.Windows.Forms.RadioButton YesNoCancelRadioButton;
        private System.Windows.Forms.RadioButton AbortRetryIgnoreRadioButton;
        private System.Windows.Forms.RadioButton OKCancelRadioButton;
        private System.Windows.Forms.RadioButton OKRadioButton;
        private System.Windows.Forms.GroupBox IconGroupBox;
        private System.Windows.Forms.RadioButton WarningRadioButton;
        private System.Windows.Forms.RadioButton StopRadioButton;
        private System.Windows.Forms.RadioButton QuestionRadioButton;
        private System.Windows.Forms.RadioButton InformationRadioButton;
        private System.Windows.Forms.RadioButton HandRadioButton;
        private System.Windows.Forms.RadioButton ExclamationRadioButton;
        private System.Windows.Forms.RadioButton ErrorRadioButton;
        private System.Windows.Forms.RadioButton AsteriskRadioButton;
        private System.Windows.Forms.Button DisplayButton;
        private System.Windows.Forms.Label OutputLabel;
    }
}

1 个答案:

答案 0 :(得分:1)

转到设计师并选择其中一个单选按钮,然后按F4(打开属性窗口)

enter image description here

单击顶部的照明螺栓以转到事件。下来查找已更改的支票,然后从下拉列表中选择您已编码的活动。

所有单选按钮选择ButtonType或Icon类型,根据它们所在的列进行。