C# 我如何管理一个;“模式”;在Visual Studio中使用类和单选按钮?
我的Form1类中有以下代码:C# 我如何管理一个;“模式”;在Visual Studio中使用类和单选按钮?,c#,C#,我的Form1类中有以下代码: private void pictureBox1_Click(object sender, EventArgs e) { if (radioButtonActor.Checked) { MouseEventArgs me = (MouseEventArgs)e; Actor actor = new Actor(textBox1.Text,
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具有一个picturebox和一些单选按钮。根据选中的单选按钮,运行不同的方法,绘制不同的图片
在Form1类中不使用这个if语句(这不是很容易维护),我如何在另一个类中检查选中了哪个单选按钮?我想这样做:
点击绘画表格>
从名为editor>
方法检查以查看选中了哪个单选按钮>此处的If语句决定下一步运行哪个方法
感谢您的建议!谢谢。我相信您希望在代码的其他地方重复使用此逻辑。 所以你可以创建一个这样的类
但是,在ASP.NET中看到您关于使用它的评论后,这将不符合您的目的,因为控件(文本框、图片框等)完全不同,您将没有图形对象,也没有鼠标指针。
即使这样,也可以通过创建具有最终值的类来管理,而不是传递控制对象。有趣的部分是你的
DrawActor
方法的实现你在实现你所描述的内容时遇到了什么问题吗?目前它工作得很好,但是我的老师说我们很快就要做ASP.NET,我必须将我的代码从UI中分离出来。这么说这就不行了,伙计!我们不会做你的家庭作业。你需要尝试你想要的,如果你遇到任何问题,然后问一个问题。哦,对不起,我没有试图掩盖这是家庭作业这一事实!然而,我确实认为这是一个特定的问题,您可能想阅读更多关于MVC或相关模式的内容,可以使用这些模式将逻辑从视图中分离出来。
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());
}
}
}