none
Navigation from one form to another in Windows applications (C Sharp)

    Question

  • I have what is surely a very, very simple question. I want to fix it so that a user can navigate FROM 'formA' TO 'formB' by clicking a button on FormA (or a line in a text box, or a cell in a datagrid... or from anything else supporting an On_Click event). In a Microsoft WEB application this is dead straightforward: Server.Transfer(<formname>?<data transfer or other command>). There must be an equivalent to this in Windows Apps but I can't find it anywhere. There's no example even in the text-books I've got ('Developing Windows-Based Applications', 'Microsoft Visual C Sharp Step by Step' and 'Beginning C# 2005 Databases'. None of these guys seem to use as an example a Windows application using multiple forms with navigation between said forms, which is odd. The Visual C Sharp Step By Step book gives you 'Server.Transfer' on p577, but I need the equivalent for Windows Apps! Perhaps it's something abstruse and difficult after all? (Hope not!) I've got experience in Web Apps but am comparatively new to Windows Apps, hence the problem. ALL I want (assuming it exists) is the one or two lines which, like Server.Transfer(....) in the Web Apps world, will get me from one form to another (also being able to transfer data from one form to another, too).

     

    Monday, July 30, 2007 5:00 PM

Answers

  • Hi,

    Why don't you add a parameter to the Form2 so that when you create an instance of Form2 you can pass the data you need

    i.e

    public Form2(string test)
    {
    InitializeComponents();
    MessageBox.Show(test);
    }

    .....to call

    Form2 f = new Form2("hello");
    f.Show();

    hope this helps
    Monday, July 30, 2007 5:59 PM

All replies

  • What exactly do you mean by "navigate"?  Normally, an application will keep the original form open and show the new form as an additional window (modal or modeless).  This is appropriate for example, with many options dialogs.  To transfer data between the forms, expose properties and methods in the forms and call them as needed.  If you want to hide the original form after showing the new one, you can call the Hide method.

    Monday, July 30, 2007 5:09 PM
  •  

    I simply want to get the user from one form to another at the click of a mouse, preferably transferring data as well. When I last did anything with Web applications, the way (or one way) to do this was via Server.Transfer(...). For instance, you might have a 'home' form with several options accessed from buttons and/or other controls. Here's a piece of sample code from a Web application:

     

    private void btnCorrections_Click(object sender, System.EventArgs e)

    {

    string StudentID=this.txtRefNo.Text.ToString() ;

    Server.Transfer("ListTrainees.aspx?ID=" + StudentID);

     

    }

     

    I simply need the equivalent to this for Windows applications via C Sharp (assuming it exists). When the above executed, the current form just vanished and the new one ('ListTrainees') loaded and displayed. The nearest I've seen to this for Windows forms is this (going from form 1 to form 2 via a button click event on form 1):

     

    private void button1_Click(object sender, System.EventArgs e)

     

    {

    form f = new Form2();

    f.Show();

     

    }

     

    Firstly, this seems a bit elaborate compared with 'Server.Transfer' (creating the new instance of form2, and so on). More important, it seems you need to keep Form 1 open in some sense (although you can hide it via the 'Hide' method - as I think you pointed out). However I'm not happy about building up a whole lot of hidden forms as use of an application progresses. The 'Server.Transfer' method seemed simply to move the user from Form1 to Form2 (transferring data too, if necessary) & that's all I really want to do. I feel there must be an equivalent for Windows Forms but maybe there isn't.

    Monday, July 30, 2007 5:44 PM
  • There's not a built-in method to do this as far as I know, but you could write it yourself.  If you actually want to close the forms as you transfer control, you can call the Close method on the forms, but you will need to take special care that the message loop doesn't end.

     

    I'm trying to picture how this program will look, though, and it seems very strange, with forms popping up and closing as you perform tasks.  You should seriously try to have a main form to the application if you can, since it will provide a logical point of reference for the user.  Perhaps you should consider keeping one form open the entire time, with navigation between various UserControls or Panels.  This could behave a bit more like web page transitions without doing crazy stuff having windows closing and opening all over the place.  If you already have a lot of work done designing the forms, you could copy that into the UserControls without too much effort.

    Monday, July 30, 2007 5:51 PM
  • Hi,

    Why don't you add a parameter to the Form2 so that when you create an instance of Form2 you can pass the data you need

    i.e

    public Form2(string test)
    {
    InitializeComponents();
    MessageBox.Show(test);
    }

    .....to call

    Form2 f = new Form2("hello");
    f.Show();

    hope this helps
    Monday, July 30, 2007 5:59 PM