C# 在c窗体中检测到无法访问的代码

C# 在c窗体中检测到无法访问的代码,c#,C#,我是C语言的新手,我正在创建Q+A表单应用程序。我在txt.AcceptsReturn=true;行中收到一个无法访问的代码检测警告;。我尝试在行中使用pragma warning disable,但它没有执行这段代码。 任何帮助都将不胜感激,谢谢 namespace WindowsFormsApplication11 { public partial class c1l1 : Form { int cLeft = 1; public c1l1()

我是C语言的新手,我正在创建Q+A表单应用程序。我在txt.AcceptsReturn=true;行中收到一个无法访问的代码检测警告;。我尝试在行中使用pragma warning disable,但它没有执行这段代码。 任何帮助都将不胜感激,谢谢

namespace WindowsFormsApplication11
{
    public partial class c1l1 : Form
    {
        int cLeft = 1;
        public c1l1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TextBox t= new TextBox();  

            t.Text = "Enter the question for Category 1, Level 1";
            String t2;
            t2 = t.Text;
            MessageBox.Show(t2);

            AddNewTextBox();

        }

        public System.Windows.Forms.TextBox AddNewTextBox()
        {
            System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();
            this.Controls.Add(txt);
            txt.Top = cLeft * 25;
            txt.Left = 100; 
            txt.Text = "TextBox " + this.cLeft.ToString();
            cLeft = cLeft + 1;
            return txt;

            txt.AcceptsReturn = true;

            KeyEventArgs e;
            if (txt.Text != null && e.KeyCode == Keys.Enter)
            {
                txt.Visible = false;
            }
        }
    }
}

在C中,不能在语句之后编写任何代码

    public System.Windows.Forms.TextBox AddNewTextBox()
    {
        System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();
        this.Controls.Add(txt);
        txt.Top = cLeft * 25;
        txt.Left = 100; 
        txt.Text = "TextBox " + this.cLeft.ToString();
        cLeft = cLeft + 1;


        txt.AcceptsReturn = true;

        KeyEventArgs e;
        if (txt.Text != null && e.KeyCode == Keys.Enter)
        {
            txt.Visible = false;
        }

        return txt;

}

基本上,检测到的不可访问代码只是代码永远不会执行,因此,如果看到代码,您将尝试在执行return语句下面的行之前返回函数中的值。因此,检查您的需求,并在此基础上在函数中放置return语句,这将解决您的问题

如果你需要进一步的帮助,请告诉我

返回txt下面的任何内容;永远不会被处决。您需要找出实际要返回txt值的位置


您在返回块之后写入了代码,静态代码分析器可以看到的代码永远不会到达


很可能是您将return语句放错了位置-任何像样的IDE都会突出显示这一点,因为很容易检查

您是从该行上方的方法返回的。由于您已返回,因此无法访问。return关键字表示函数的结束函数的意图不清楚。
public System.Windows.Forms.TextBox AddNewTextBox()
    {
        System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();
        this.Controls.Add(txt);
        txt.Top = cLeft * 25;
        txt.Left = 100; 
        txt.Text = "TextBox " + this.cLeft.ToString();
        cLeft = cLeft + 1;
        return txt;    //Everything below this line will never get executed

        txt.AcceptsReturn = true;

        KeyEventArgs e;
        if (txt.Text != null && e.KeyCode == Keys.Enter)
        {
            txt.Visible = false;
        }
    }