创建多个窗体GUI [英] Creating Multiple Form GUI

查看:186
本文介绍了创建多个窗体GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在C#中开发一个非常基本的应用程序Windows窗体应用程序,它将值插入到SQL数据库中。
我有四种不同的表单


  • 一个用于输入客户详细信息
  • 一个用于输入任何交易详情

  • 分别用于搜索客户/交易。



什么是最佳方式将所有四种形式连接起来?我只是进入C#,所以最基本的方式可能是理想的。

我看到它在我脑海中运转的方式是运行程序,最后在一个屏幕上显示四个对应表单的四个按钮。当您按下按钮时会打开一个单独的窗口,显示插入窗体。然后,您可以关闭表单以返回到主开始屏幕。



C#中的基本代码是什么?举例来说,我们可以说5种不同的布局是:


  • Main (包含按钮)

  • TransactionEntry

  • AddressEntry

  • TransactionSearch

  • AddressSearch


解决方案

好的,这是我如何做的一个例子。在按钮单击事件的主窗体上:

  frmSecondForm secondForm = new frmSecondForm(this); //this将主窗体传递给第二个窗体以便能够控制主窗体... 
secondForm.Show();
this.Hide();

您的第二张表格构造函数代码:

 表单frmHome; 
frmSecondForm(Form callingForm)//需要一个调用窗体来控制这个表单
{
Initialize(); //已经在这里
frmHome = callingForm as frmMain;
}

然后在第二个窗体的关闭中取消隐藏主窗体:

  frmSecondForm_FormClosing()
{
frmHome.Show();





$ b因此,总而言之,如果您需要在表单之间传递数据,只需添加它作为第二种形式的参数。




同样,我会考虑将您的数据集合放入一个存储库包(文件夹)和类,然后创建一个User.cs类,它将保存您保存在数据库中的所有信息。



提示:右键单击解决方案资源管理器中的顶级项目并转到新建 - >文件夹。将它命名为Repo。
右键单击该文件夹并转到New - > Class并将其命名为UserRepo。
在这里构建函数以从数据库中收集数据。

在主窗体的构造函数区域中,调用类(repo)。

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

然后点击登录按钮:

  private button1_ClickEvent()
{
if(userRepo.isValidLogin(userNameText,passwordText))
{
//对于userRepo.isValidLogin()做的事情做


$ / code $ / pre

  public bool isValidLogin(String username,String password)
{
bool isValid = false;
//写出数据代码
return isValid;
}


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

解决方案

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;
}

这篇关于创建多个窗体GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆