Results 1 to 5 of 5

Thread: Question

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    41

    Question

    Can someone help me on how to link different forms/buttons together. Say I have 2 forms, one form will act as the main application and the other one will appear when clicked on. EX: I have an application that has <BACK> <NEXT> buttons, clicking next will transfer me to the next form, back will transfer me back.

    Can someone give me the source code so I can add a Back and Next. I am curently reading the Learn C# in 24 Hrs so I odn't kno much. All I kno is how to make a button, and when I dbl click on it, it will go to the coding page and I type in the code in the provided space.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Question

    Code:
    SecondForm f=new SecondForm();
    f.Show();// this will display your new form
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Question

    If you want to be able to go back to a previous form then you're going to need a reference to that form. Let's say that you have a number of people and a package. Each person knows the next person in the sequence so when told they can pass that package on to them. What happens if they are told to pass it back? If they don't know who gave it to them then they can't. The form situation is similar. Each time a form creates and displays the next form it must pass a reference to itself to that form so that it can later pass control back if required. This would normally be done by rewriting the constructor to take an parameter that refers to the previous form, which you can then go back to if required:
    Code:
    public class Form1 {
        
        private Form previousForm;
        
        public Form1(Form previousForm) {
            //  This call is required by the Windows Form Designer.
            InitializeComponent();
            //  Add any initialization after the InitializeComponent() call.
            this.previousForm = previousForm;
        }
        
        private void GoBack() {
            this.Hide();
            this.previousForm.Show();
        }
    }
    What you're creating is basically a wizard and just having a series of forms that each have a reference to the next and previous form is definitely not the best way to go about creating one, but it's a place to start for a very basic case.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    41

    Re: Question

    Ok thx. Where would I put this code though? Would I dbl click on the "next" or "back" button and put the code in the space? I am still confused on that part.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Question

    GoBack needs to be executed when you click the Back button, so you need to create a Click event handler for the Back button and either call the GoBack method from there or else move the code from the GoBack method to the event handler.

    An event handler is a method that handles an event, i.e. is executed whenever a specific event is raised and receives data about that event. When you double-click a control in the designer you are telling the IDE to create a handler for the control's default event. The default event for the Button class is Click.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width