简体   繁体   English

用户控件的基本用法,关闭用户控件并打开另一个用户控件参数

[英]Basic use of user controls, closing a user control and opening another, user control parameters

ladies and gentlemen, once again I unfortunately am going to bother you with newbie stuff. 女士们,先生们,我再次不幸地要用新手东西来打扰您。 I have searched for this information for hours, so if there's a thread with what I want on it, it's buried deeper than I could find. 我已经搜索了好几个小时的信息,因此,如果有一个包含我想要的内容的线程,它的埋藏比我能找到的要深。

This was my first question here, and Mark Hall was kind enough to set me straight. 是我在这里的第一个问题,马克·霍尔(Mark Hall)很友好,可以直率我。 Since then, I have created a new project and recreated my first three screens as user controls – a container/login, a choice screen, and a main screen (currently empty). 从那时起,我创建了一个新项目,并重新创建了前三个屏幕作为用户控件-容器/登录,选择屏幕和主屏幕(当前为空)。 If a user has more than one collection, the choice screen pops up and allows them to choose a collection. 如果用户有多个集合,则会弹出选择屏幕,并允许他们选择一个集合。

I did run into a snag with parameters, but I'm solving that by overloading the form declaration (solution found here) – yes, I know it's much better to send parameters through calls, but I'd hate to have to create a call for each parameter (do I?) and… OK, OK, I'm better at this than {get, set}. 我确实遇到了带有参数的障碍,但是我通过重载表单声明(在此处找到解决方案)来解决该问题–是的,我知道通过调用发送参数要好得多,但是我讨厌必须创建一个调用对于每个参数(是吗?),……好吧,好吧,我比{get,set}更好。 Man, I hate being a newbie. 老兄,我讨厌成为新手。

Anyways, I'm having trouble with the choice form – I can't seem to call it, close it, then go to the main form. 无论如何,我在选择表单时遇到了麻烦–我似乎无法调用它,将其关闭,然后转到主表单。 I have no problem (if there's only one collection) in going straight to the main form, it's that darn choice form. 我直接进入主要形式没有问题(如果只有一个集合),那就是该死的选择形式。 Yes, I know I could include a choice datagridview, but a few of our end users aren't the sharpest bulb in the tool shed, and need hand-holding. 是的,我知道我可以提供一个选择的datagridview,但是我们的一些最终用户并不是工具棚中最尖锐的灯泡,需要手持。 Anyways, here's the code. 无论如何,这是代码。

CONTAINER/LOGIN SCREEN 容器/登录屏幕

namespace DeleteThis
{
    public partial class ContainerForm : Form
    {
        Main Main = new Main();
        LoginCollectionChoice LoginChoice = new LoginCollectionChoice();
        DataTable dtPermissions = new DataTable();
        public ContainerForm()
        {
            InitializeComponent();
            Main.ExitEvent += new Main.ExitEventHandler(Main_ExitEvent);
            LoginChoice.ExitEvent += new
            LoginCollectionChoice.ExitEventHandler(LoginChoice_ExitEvent);
        }

        void LoginChoice_ExitEvent(object sender, EventArgs e)
        {
            pnlcontainer.Controls.Remove(LoginChoice);
        }

        void Main_ExitEvent(object sender, EventArgs e)
        {
            pnlcontainer.Controls.Remove(Main);
        }

    private void btnLogin_Click(object sender, EventArgs e)
    {
        LoginProcedure();
    }

    private void LoginProcedure()
    {
        DataTable dtPermissions = AdminClass.GetCollectionsForUser(int.Parse(txtbxUserName.Text));
        if (dtPermissions.Rows.Count == 1)
        {
            //Valid user, one collection.  Head right in.
            pnlcontainer.Controls.Add(Main);
            Main.BringToFront();
        }
        else
        {
            //More than one collection found.  Giving user choice
            LoginCollectionChoice LoginChoice = new LoginCollectionChoice(dtPermissions);

            pnlcontainer.Controls.Add(LoginChoice);
            LoginChoice.BringToFront();

        }
    }

    private void btnExitProgram_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}

} }

I hope I didn't kill anything in the snipping. 我希望我不会在狙击中杀死任何东西。 And now the choice screen… 现在,选择屏幕…

public partial class LoginCollectionChoice : UserControl
{
    public delegate void ExitEventHandler(object sender, EventArgs e);
    public event ExitEventHandler ExitEvent;
    private static DataTable dtPermit;
    DataTable dtPermissions = new DataTable();

    public LoginCollectionChoice()
    {
    }

    public LoginCollectionChoice(DataTable dtPermissions)
    {
        InitializeComponent();
        GrdCollection.DataSource = dtPermissions;
        dtPermit = dtPermissions;
    }

    private void btnChoose_Click(object sender, EventArgs e)
    {
    //Code for the user to choose a collection
    ExitEvent(this, new EventArgs());
    }
}

I've snipped all non-relevent code, I hope you gentlemen and ladies can help this newbie get on the right path. 我已经删除了所有非相关代码,希望各位先生和女士可以帮助这个新手走上正确的道路。 Please, be gentle, you wouldn't want to see me cry, would you? 求求你了,温柔一点,你不想看到我哭泣,是吗? :) Oh! :)哦! And if you know of any great tutorial sites, please email them to me. 如果您知道任何很棒的教程网站,请通过电子邮件将它们发送给我。 I'd prefer to spend a week on tutorials than a week on stumbling and asking here. 我宁愿花一个星期在教程上,也不愿花一个星期在绊脚石上并在这里提问。 Thank you all very much. 非常感谢大家。

Are you calling logonForm.Show()? 您在调用logonForm.Show()吗?

It seems like you'd need to show it that way. 看来您需要以这种方式显示它。

You use BringToFront(), but I think it needs to be shown first. 您使用BringToFront(),但我认为它需要首先显示。

The first thing that jumps out to me is that you are calling 跳到我身上的第一件事是你在打电话

LoginCollectionChoice LoginChoice = new LoginCollectionChoice(dtPermissions);

which is creating a local variable called LoginChoice, which is not the same as your class variable even though they share the same name. 这会创建一个名为LoginChoice的局部变量,即使它们共享相同的名称,它也与您的类变量并不相同。

What you want to do is to not declare a local variable in that method. 您要做的是不在该方法中声明局部变量。 Change that line to 将该行更改为

LoginChoice = new LoginCollectionChoice(dtPermissions);

Having said that, I believe tylerjgarland in that you need to call .Show() first. 话虽如此,我相信tylerjgarland因为您需要先调用.Show()。 And the way you are closing the form is certainly odd. 而且您关闭表格的方式当然很奇怪。 I would create a form, showDialog, get the result and then close the form that way. 我将创建一个表单showDialog,获取结果,然后以这种方式关闭表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM