can anyone tell me if one can create a form level variable in c# and how one does that. with form level variable i maen a variable which can be accessed by all the command buttons on the form. the buttons are on different panels. i dont know if that would make a difference. i doubt it. but just how do i create a variable which can be accesses by all the buttons on the form.

by buttons I assume you mean event handlers? (ignore that--I don't know why I'm so mean)

Anyway, try declaring the variables outside of any methods, but instead, declare them right after the class declaration (before the constructor), Like so:

public partial class FooForm : Form
{
string foo_string; //Can be accessed anywhere in the form
int foo_int = 0; //Can also be accessed anywhere

public FooForm() //constructor that is automatically generated
{
InitializeComponent();
}

void FooButtonClick(object sender, EventArgs e) //your button event handler
{
foo_string = "foobar";
}

void BarButtonClick(object sender, EventArgs e) //another button event handler
{
if (foo_int==0)
{
foo_string = "Not a bar anymore";
}
}
}

Is that what you meant?

yes scru. that is exactly what i need. thanks a million.

event handlers. i get it. i had to retrain from java but in java you know an event handler because you create so much of the code yourself. this feels more like visual basic.

This question has already been answered. Start a new discussion instead.