使用另一个类中的方法编辑表单 [英] Editing a Form using a method in another class

查看:52
本文介绍了使用另一个类中的方法编辑表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我创建了一个带有开关盒的方法.

So I created a method with a switch case in it.

public void FormStateSwitch(FormState formState)
{
    var defaulSize = new Size(278, 132);
    var backDefLoc = new Point(10, 77);
    switch (formState)
    {
        case FormState.Template: //Template
            optionsPanel.Visible = false;
            isbnPanel.Visible = false;
            titlePanel.Visible = false;
            templatePanel.Visible = true;
            Size = defaulSize;
            break;
        case FormState.Options: //Options
            titlePanel.Controls.Remove(_back);
            isbnPanel.Controls.Remove(_back);
            optionsPanel.Controls.Add(_back);
            isbnPanel.Visible = false;
            titlePanel.Visible = false;
            templatePanel.Visible = false;
            optionsPanel.Visible = true;
            Size = defaulSize;
            _back.Location = backDefLoc;
            _previousState = FormState.Template;
            break;
        case FormState.ISBN: //ISBN
            optionsPanel.Controls.Remove(_back);
            titlePanel.Controls.Remove(_back);
            isbnPanel.Controls.Add(_back);
            optionsPanel.Visible = false;
            titlePanel.Visible = false;
            templatePanel.Visible = false;
            isbnPanel.Visible = true;
            Size = defaulSize;
            _back.Location = backDefLoc;
            _previousState = FormState.Options;
            break;
        case FormState.Title: //Title
            isbnPanel.Controls.Remove(_back);
            optionsPanel.Controls.Remove(_back);
            titlePanel.Controls.Add(_back);
            optionsPanel.Visible = false;
            isbnPanel.Visible = false;
            templatePanel.Visible = false;
            titlePanel.Visible = true;
            _back.Location = new Point(10, 137);
            Size = new Size(278, 195);
            _previousState = FormState.Options;
            break;
    }
}



在包含它的表单中调用该方法时,它工作正常,但是当我从外部类调用它时,它没有任何效果.答案可能很简单,我正在忽略一些东西,因此我将感谢您的帮助.即使当我尝试不使用此方法来编辑表单并且在另一个类中工作时,它也没有任何效果.
我也希望能得到关于为什么的解释,以便我能学习一些东西,而不仅仅是被告知.



The method works fine when called within the form it is contained in, but when I call it from an outside class it has no effect. The answer is probably simple and I am overlooking something so I would appreciate the help. Even when I try to edit the form without using this method and I am working in another class it also doesn''t have an effect.
I would also appreciate an explanation as to why so that I am learning something instead of just being told something.

推荐答案

您好,

如果我没记错的话,您要实现的目标是采用插入",编辑",更新"等形式的多种模式.有一种更简单的方法可以实现这一目标.

您需要做的是,将Panel对象拖放到窗体上,将其停靠在窗体中,设计一种状态/模式.对所需的每个状态/模式重复此过程.然后,您可以显示/隐藏它们,而不是添加/删除控件.
Hi there,

If I am not mistaken, what you are trying to achieve is to have multiple modes in the form like Insert, Edit, Update. There is an easier way of achieving this.

What you need to do is, drag and drop a Panel object on to your form, dock it in the form, design one state/mode. Repeat this process, for each and every state/mode you want. Then instead of, adding/removing your controls, you can show/hide them.
enum FormState {
    Template,
    Options,
    ISBN,
    Title
}

public void FormStateSwitch(FormState formState) {
    switch (formState)
    {
        case FormState.Template:
            templatePanel.Visible = true;
            optionsPanel.Visible = false;
            isbnPanel.Visible = false;
            titlePanel.Visible = false;
            break;
        case FormState.Options:
            // Do your work here
            break;
        case FormState.ISBN:
            // Do your work here
            break;
        case FormState.Title:
            // Do your work here
            break;
        default:
            // Do your work here
            break;
    }
}


最后一点,请不要使用int来描述您的状态,而是像我以前使用的那样使用Enum.您应该出于很多原因,我只列举其中几个.
1.如果使用数字,则必须手动跟踪每种模式. IE. 1 =模板等.但是,如果使用枚举,则可以直接看到"formState"是什么,而无需记住它.因此,您可以在多个位置使用它,而不会意外地为错误的状态编码.
2.添加/删除状态非常容易.删除状态后,您在使用该特定状态的任何地方都会突出显示为错误.一旦将状态名称更改为其他名称,状态就会在您的解决方案中进行更新(如果您使用的是像VS这样的IDE).如果添加状态,则要做的就是将另一种情况添加到交换机中.
3.您可以为枚举中的每个项目分配值.例如,


On a final note, please do not use int to depict your states, use an Enum instead, like I have used. There are many reasons why you should, I am going to mention only a few.
1. If you use numbers, you have to manually keep track what each mode. I.e. 1 = Template, etc. But if you use Enums, you see directly what "formState" is without remembering it. So you can use it in multiple locations without accidentally coding for a wrong state.
2. It is very easy to add/remove states. As soon as you remove a state, everywhere you''ve used that particular state will be highlighted as an error. As soon as you change the name of a state to something different, it will be updated all over your solution (if you are using a IDE like VS). If you add a state, all you have to do is go and add another case to your switch.
3. You can assign values to each item in an enum. For instance,

enum FormState {
    Template = 1,
    Options = 10,
    ISBN = 15,
    Title = 20
}


这些只是示例值,但是您可以直接将枚举转换为整数并在计算中使用它.最好的部分是,所有内容都是强类型的.您会更新枚举,其他任何地方都会被更新.

希望对您有所帮助:)问候


These are just example values but you can directly convert your enum to integer and use it in a calculation. The best part is, everything is strongly typed. You update your enum, everywhere else it would be updated.

Hope this helps :) Regards


-如果外部类正在运行除thrad形式之外的其他线程,则它将引发跨线程操作异常.
- if the outside class is running some other thread than the form thrad then it will raise cross thread operation exception.


开关盒坏!永远不要做这样的事情!

至少创建一些枚举并将其用于大小写和其他地方:

Bad switch case! Never do such things!

At least, create some enumeration and use it in case and elsewhere:

enum MyCase { Template, Options, Isbn, Title, }



通常,避免使用所有这些立即数:0、1,","my string"等.始终使用显式常量声明,数据文件和/或资源.

—SA



In general, avoid all those immediate constants: 0, 1, "", "my string", etc. Always use explicit constant declarations, data files and/or resources.

—SA


这篇关于使用另一个类中的方法编辑表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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