Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

c# - Creating Multiple Form GUI

I am developing a very basic application Windows Form Application in c# that inserts values into a SQL database. I have four separate forms

  • one for inputting customer details
  • one for inputting any transaction details
  • one for searching customers/transactions respectively.

What is the best way link all four forms together? I'm only just getting into C# so the most basic way possible would be ideal.

The way I see it working in my head is that you run the program and end up on one screen which shows four buttons for the four corresponding forms. When you press the button a separate window opens showing the insert forms. You can then close the form to return to the Main Start Screen

What would be the basic code for this in C#? For examples sake lets say the 5 different layouts are

  • Main (Containing the buttons)
  • TransactionEntry
  • AddressEntry
  • TransactionSearch
  • AddressSearch
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Okay, here is an example of how I would do it. On the main form on button click event:

frmSecondForm secondForm = new frmSecondForm(this); //"this" is passing the main form to the second form to be able to control the main form...
secondForm.Show();
this.Hide();

One your Second Forms constructor code:

Form frmHome;
frmSecondForm(Form callingForm) //requires a calling form to control from this form
{
    Initialize(); //Already here
    frmHome = callingForm as frmMain;
}

Then on the second form's closing unhide the main form:

frmSecondForm_FormClosing()
{
    frmHome.Show();
}

So all in all, if you needed to pass data between the forms just add it as a parameter on the second form.


Again, I would consider placing your data collection into a repository package (folder) and class, then create a User.cs class that will hold all of the information you keep in the database.

Hint: Right click your top level item in solution explorer and go to New -> Folder. Name it Repo. Right click that folder and go to New -> Class and name it UserRepo. In here build functions to collect the data from the databases.

On your main form's constructor area, call the class (repo).

private Repo.UserRepo userRepo; 
frmMain_FormLoad()
{
    userRepo = new Repo.UserRepo();
}

Then on log in button click:

private button1_ClickEvent()
{
    if(userRepo.isValidLogin(userNameText, passwordText))
    {
        //Do stuff
    }
}

for userRepo.isValidLogin()

public bool isValidLogin(String username, String password)
{
    bool isValid = false;
    //Write up data code
    return isValid;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...