复制子窗口问题

发布于 2024-10-12 09:57:25 字数 577 浏览 2 评论 0原文

在我的应用程序中,我有一个“提交”按钮可以执行此操作:

private void Submit_button_Click(object sender, RoutedEventArgs e)
{
    string variable = variable_textBox.Text;
    if (variable.Length >= 1 && variable.Length <= 6)
    {
        //get some data from db
    }
    else
    {
        ChildWindow msg = new Msg("Some string");
        msg.Show();
    }
}

这是我的问题:

当我编写一个字符串以使程序转到 else 子句时,将出现一个 childWindow (这是可以的);但如果我再这样做,就会出现 2 个 childWindows。每次单击“提交”按钮,我都会获得 childWindows 的点击次数。

有人能告诉我为什么吗?我在其他地方使用相同的ChildWindow,没有任何问题......

In my application I have a Submit button that does this:

private void Submit_button_Click(object sender, RoutedEventArgs e)
{
    string variable = variable_textBox.Text;
    if (variable.Length >= 1 && variable.Length <= 6)
    {
        //get some data from db
    }
    else
    {
        ChildWindow msg = new Msg("Some string");
        msg.Show();
    }
}

Here is my problem:

When I write a string so that the program goes to the else clause, a childWindow will appear (that is OK); but if I do this again, 2 childWindows will appear. For each click on Submit button, I get num of clicks childWindows.

Can someone tell me why? I use the same ChildWindow in other places, and I have no problems...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流星番茄 2024-10-19 09:57:25

我在其他窗口中使用相同的ChildWindow
地方,我没有问题......

你的意思是:当你在不同的地方使用子窗口时,你没有这个问题?
如果是这样,我只能假设当单击“确定”时,您不仅单击顶部窗口,还单击底部窗口。

您可以添加一个行为,从第一个子窗口中删除 Submit_button_Click 事件。

但为什么要在子窗口中显示消息呢?您可以使用 MessageBox.Show("Some string") 来实现此目的。

I use the same ChildWindow in other
places, and I have no problems...

Do you mean with then: When you use childwindows on different places you dont have this problem?
If so, I can only assume when clicking OK you dont just click the top window but also the bottom window.

You can add a behaviour that removes the Submit_button_Click event from the first childwindow.

But why show a message in a childwindow? You can use MessageBox.Show("Some string") for that.

梦晓ヶ微光ヅ倾城 2024-10-19 09:57:25

似乎 Submit_button_Click 方法被多次附加到点击事件(每次点击)。

通过在其中放置一个断点并查看是否被多次命中来确保情况并非如此。

Seems like Submit_button_Click method is being attached multiple times to the click event (for each click).

Make sure this is not the case by putting a breakpoint in there and see if it is hit multiple times.

浅紫色的梦幻 2024-10-19 09:57:25
    public static Form IsFormAlreadyOpen(Type FormType)
    {
        foreach (Form OpenForm in Application.OpenForms)
        {
            if (OpenForm.GetType() == FormType)
                return OpenForm;
        }
        return null;
    }


private void Submit_button_Click(object sender, RoutedEventArgs e)
{
    string variable = variable_textBox.Text;
    if (variable.Length >= 1 && variable.Length <= 6)
    {
        //get some data from db
    }
    else
    {
        ChildWindow frm = null;
            if ((frm = (ChildWindow)IsFormAlreadyOpen(typeof(ChildWindow))) == null)
            {
                frm = new ChildWindow();
                frm.Show();
            }
            else
            { }
    }
}
    public static Form IsFormAlreadyOpen(Type FormType)
    {
        foreach (Form OpenForm in Application.OpenForms)
        {
            if (OpenForm.GetType() == FormType)
                return OpenForm;
        }
        return null;
    }


private void Submit_button_Click(object sender, RoutedEventArgs e)
{
    string variable = variable_textBox.Text;
    if (variable.Length >= 1 && variable.Length <= 6)
    {
        //get some data from db
    }
    else
    {
        ChildWindow frm = null;
            if ((frm = (ChildWindow)IsFormAlreadyOpen(typeof(ChildWindow))) == null)
            {
                frm = new ChildWindow();
                frm.Show();
            }
            else
            { }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文