Open Input Dialog Box Did Not Run

Lin100

Well-known member
Joined
Dec 12, 2022
Messages
69
Programming Experience
10+
Error CS0106 The modifier 'public' is not valid for this item Line 30

Error CS8370 Feature 'static local functions' is not available in C# 7.3.
Please use language version 8.0 or greater. Line 30

Error CS0136 A local or parameter named 'value' cannot be
declared in this scope because that name is
used in an enclosing local scope to define
a local or parameter. Line 30

///////////////////////////////////////////////////////////////////////////////

Input Dialog Box:
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 Input_Dialog_Box
{
    public partial class Form1 : Form
    {
      public Form1()
      {
        InitializeComponent();
      }

    private void button1_Click(object sender, EventArgs e)
    {
       string value = "";
       if (InputBox("Dialog Box", "What is your name?", ref value) == DialogResult.OK)
        {
          labelResponseInput.Visible = true;
          labelResponseInput.Text = "Your name: " + value;
        }
    
    // ERROR IS FLAGGED ON LINE 30 BELOW
    public static DialogResult InputBox(string title, string promptText, ref string value)
    {
       Form form = new Form();
       Label label = new Label();
       TextBox textBox = new TextBox();
       Button buttonOk = new Button();
       Button buttonCancel = new Button();

       form.Text = title;
       label.Text = promptText;

       buttonOk.Text = "OK";
       buttonCancel.Text = "Cancel";
       buttonOk.DialogResult = DialogResult.OK;
       buttonCancel.DialogResult = DialogResult.Cancel;

       label.SetBounds(36, 36, 372, 13);
       textBox.SetBounds(36, 86, 700, 20);
       buttonOk.SetBounds(228, 160, 160, 60);
       buttonCancel.SetBounds(400, 160, 160, 60);

       label.AutoSize = true;
       form.ClientSize = new Size(796, 307);
       form.FormBorderStyle = FormBorderStyle.FixedDialog;
       form.StartPosition = FormStartPosition.CenterScreen;
       form.MinimizeBox = false;
       form.MaximizeBox = false;

       form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
       form.AcceptButton = buttonOk;
       form.CancelButton = buttonCancel;

       DialogResult dialogResult = form.ShowDialog();
       value = textBox.Text;
       return dialogResult;
     }
   }
 }
}
 
I used the code below and it work.
Can you explain why the author uses the error code on the
web site ?

// GOOD CODE.
DialogResult InputBox (string title, string promptText, ref string
Input_Value)

////////////////////////////////////////////////////////////////////

// ERROR IS FLAGGED ON LINE 30 BELOW
public static DialogResult InputBox (string title, string promptText,
ref string value)
 
This is all because you pasted in code at the wrong place. Notice that your button1_Click() method is missing a closing curly brace on line 28. I suspect that closing curly brace is on line 66.

But that's okay as long as the language version you are using supports static local functions. It looks like you are using a version that doesn't. So by removing the public static on the declaration of InputBox() you converted it into a local function. The public (and other visibility modifiers) are only valid for member functions/methods.
 
Back
Top Bottom