✅ my event handler cant find my textbox

So im new to prgramming and wanted to write a little programm that requres me to use a the input of a textbox to set a name, but for some reason my button click handler cant find my textbox.
private string createMacroFile()
{
Form nameInput = new Form();
nameInput.Text = "Name";
nameInput.Icon = SystemIcons.Question;
nameInput.MinimizeBox = false;
nameInput.MaximizeBox = false;
nameInput.FormBorderStyle = FormBorderStyle.FixedSingle;
nameInput.Size = new Size(200, 130);


Label labelA = new Label();
labelA.AutoSize = true;
labelA.Text = "Wie möchtest du den Macro nennen?";
nameInput.Controls.Add(labelA);

TextBox textBoxA = new TextBox();
textBoxA.Location = new Point(6, 23);
textBoxA.Width = 150;
nameInput.Controls.Add(textBoxA);

Button btnOkay = new Button();
btnOkay.Text = "Okay";
btnOkay.AutoSize = true;
btnOkay.Location = new Point(100, 50);
nameInput.AcceptButton = btnOkay;
btnOkay.Click += btnOkay_Click;
nameInput.Controls.Add(btnOkay);

Button btnCancel = new Button();
btnCancel.Text = "Cancel";
btnCancel.AutoSize = true;
btnCancel.Location = new Point(6, 50);
btnCancel.Click += btnCancel_Click;
nameInput.Controls.Add(btnCancel);

nameInput.ShowDialog();

return "test";
}

private void btnOkay_Click(object sender, EventArgs e)
{
File.WriteAllText(Path.Combine(macroPath, textBoxA.text), "");
}
private string createMacroFile()
{
Form nameInput = new Form();
nameInput.Text = "Name";
nameInput.Icon = SystemIcons.Question;
nameInput.MinimizeBox = false;
nameInput.MaximizeBox = false;
nameInput.FormBorderStyle = FormBorderStyle.FixedSingle;
nameInput.Size = new Size(200, 130);


Label labelA = new Label();
labelA.AutoSize = true;
labelA.Text = "Wie möchtest du den Macro nennen?";
nameInput.Controls.Add(labelA);

TextBox textBoxA = new TextBox();
textBoxA.Location = new Point(6, 23);
textBoxA.Width = 150;
nameInput.Controls.Add(textBoxA);

Button btnOkay = new Button();
btnOkay.Text = "Okay";
btnOkay.AutoSize = true;
btnOkay.Location = new Point(100, 50);
nameInput.AcceptButton = btnOkay;
btnOkay.Click += btnOkay_Click;
nameInput.Controls.Add(btnOkay);

Button btnCancel = new Button();
btnCancel.Text = "Cancel";
btnCancel.AutoSize = true;
btnCancel.Location = new Point(6, 50);
btnCancel.Click += btnCancel_Click;
nameInput.Controls.Add(btnCancel);

nameInput.ShowDialog();

return "test";
}

private void btnOkay_Click(object sender, EventArgs e)
{
File.WriteAllText(Path.Combine(macroPath, textBoxA.text), "");
}
6 Replies
SinFluxx
SinFluxx16mo ago
What do you mean it can't find it?
RohesKätzchen
RohesKätzchenOP16mo ago
my problem was that the my btnOkay_click wouldnt find the textBoxA, but i found a fix by declairing my textbox outside of my createMacroFile() Function so i fixed it but still thanks
SinFluxx
SinFluxx16mo ago
$close
MODiX
MODiX16mo ago
Use the /close command to mark a forum thread as answered
trunksvn
trunksvn16mo ago
Careful with the scopes...
RohesKätzchen
RohesKätzchenOP16mo ago
Idk what a scope is :)

Did you find this page helpful?