CodeRazor wrote:
Maybe i am not being clear.
This is a very simple question. My experience thus far is in web application
development. Now in web app development if you want to access a variable you
set in the previous page you can use Session state, or set the variable in
the querystring to the new page.
Windows application development does not have Session state or use query
strings as far as i am aware because it has no constantly refreshing browser
to contend with.
So my question is:
In Windows app development, how can you access a variable you set in a
previous form?
(I am not using .Net 2.0)
Say you want to access a user id in your "previous" form. Your form
could do a number of things. It could set a semi-global variable, could
be a *return* value of a ShowDialog() method, or it could expose a
property that you'd have to read.
The property method is the one I like, since it's also what the
OpenFileDialog form uses. To use it add something like this to your code:
class MyForm : Form {
string userid;
public string UserID { get { return useride; } }
// rest of the form
}
Then in the code you use to call this form you'd use something like:
class EntryPoint {
static void Main(string[] args) {
string id = null;
using(MyForm form = new MyForm()) {
if(form.ShowDialog() == DialogResult.OK)
id = form.UserID;
}
Trace.WriteLine("Your id is: " + id);
}
}
Of course, this is just a mock-up. The way WinForms are used seems a
lot different to me than the way WebForms are used. You never need to
worry about switching windows. If what you're trying to do is a sort of
"Wizard", I recommend passing the object you are trying to fill to each
form constructor, then the form can fill the parts it is supposed to and
continue. I like using a separate class (EntryPoint) for this:
class EntryPoint {
static SomeObject wizobj;
static void Main(string[] args) {
wizobj = new SomeObject();
using(FormStep1 f = new FormStep1(wizobj)) { f.ShowDialog()}
// ... continue until the wizobj is filled.
}
}
Hope this helps,
Scott