【问题标题】:How I manage a "mode" using a class and radio buttons in Visual Studio?如何在 Visual Studio 中使用类和单选按钮管理“模式”?
【发布时间】:2017-07-11 04:48:16
【问题描述】:

我的 Form1 类中有以下代码:

private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (radioButtonActor.Checked)
            {
                MouseEventArgs me = (MouseEventArgs)e;
                Actor actor = new Actor(textBox1.Text, me.X, me.Y);

                actor.DrawActor(pictureBox1.CreateGraphics());
            }
            else if (radioButtonUseCase.Checked)
            {
                MouseEventArgs me = (MouseEventArgs)e;
                UseCase usecase = new UseCase(textBox1.Text, me.X, me.Y);

                usecase.DrawUseCase(pictureBox1.CreateGraphics());
            }
        }

DrawActor 和 DrawUseCase 方法只是在称为 Actor 和 UseCase 的类中绘制的图形线条。 UI 有一个图片框和一些单选按钮。根据选中的单选按钮,运行不同的方法,绘制不同的图片。

不是在 Form1 类中使用这个 if 语句,这不是很容易维护,我将如何检查另一个类来检查哪个单选按钮被选中?我想做这样的事情:

点击绘制表格 > 从名为编辑器的新类中运行方法 > 方法检查以查看哪个单选按钮被选中 > 此处的 If 语句决定接下来运行哪个方法。

感谢所有建议!谢谢。

【问题讨论】:

  • 您在实施您所描述的内容时是否遇到任何问题?
  • 目前它工作得很好,但是我的老师说我们很快就会做 ASP.NET,我必须将我的代码与 UI 分开。所以这不会按原样工作。
  • 伙计!我们不会做你的功课。你需要尝试你想要的,如果你遇到任何问题,然后问一个问题。参考:meta.stackoverflow.com/questions/334822/…
  • 哦,对不起,我并没有试图隐瞒这是家庭作业的事实!不过,我确实认为这已经足够具体了
  • 您可能想了解更多关于 MVC 或可用于从视图中分离逻辑的相关模式。

标签: c#


【解决方案1】:

我相信您想在代码的其他地方重用此逻辑。 所以你可以创建一个这样的类..


static class MyDrawer
{ 
public static void ChecknDraw(RadioButton radioButtonActor,RadioButton  radioButtonUseCase, TextBox textBox1, PictureBox pictureBox1, EventArgs e)
{
    if (radioButtonActor.Checked)
    {
        MouseEventArgs me = (MouseEventArgs)e;
        Actor actor = new Actor(textBox1.Text, me.X, me.Y);

        actor.DrawActor(pictureBox1.CreateGraphics());
    }
    else if (radioButtonUseCase.Checked)
    {
        MouseEventArgs me = (MouseEventArgs)e;
        UseCase usecase = new UseCase(textBox1.Text, me.X, me.Y);

        usecase.DrawUseCase(pictureBox1.CreateGraphics());
    }
}

}

但是,在看到您关于在 ASP.NET 中使用它的评论后,这将无法满足您的目的,因为控件(TextBox、PictureBox 等)完全不同,您将没有图形对象,也没有鼠标指针。 甚至这可以通过创建具有最终值的类而不是传递控制对象来管理。有趣的部分是您对DrawActor 方法的实现

【讨论】:

    猜你喜欢
    • 2010-11-14
    • 2015-10-13
    • 2023-04-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多