创作和表演形式 [英] creat and show form

查看:106
本文介绍了创作和表演形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我遇到了麻烦。 //它可以是愚蠢的:(。我试过但不是真的。

i有图标,表格主要是一个按钮。我想点击按钮show form_1,我选择值1(值1 ==图标1 )然后表单2将显示(称为form2_1)。在表单2我隐藏。相同的值2,3,...

我想要show form2_1我双击图标1.

i call form2:

Hi!
i have a trouble. // it can stupid :(. i tried it but not true.
i have icons, a button in form main. i want click button show form_1, i select value 1 (value 1 == icon 1) then form 2 will display (call it form2_1). at form 2 i hide. Same for value 2, 3...
And i want show form2_1 i double click icon 1.
i call form2:

form.form_MusikUnicast M_unicast = new form_MusikUnicast();
 private void button_phatthanh_Click(object sender, EventArgs e)
        {
            using (form.form_musik  phatnhac = new form_musik())
            {
                if (phatnhac.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    if (phatnhac.listBox1.Items.Count == 1)
                    {
                        demsoip = 1;  // unicast
                          M_unicast = new form_MusikUnicast();
       M_unicast.Name = "play to :" + phatnhac.listBox1.Items[0].ToString();
                        if (M_unicast.label1.Text != "1")  // when form not hide. call form M_unicast the first.
                        {
                            for (int i = 0; i < phatnhac.listBox2.Items.Count; i++)
                            {
                                M_unicast.playlistM.Items.Add(phatnhac.listBox2.Items[i].ToString());
                            }
                            M_unicast.label_ip.Text = phatnhac.listBox1.Items[0].ToString();
                            M_unicast.Tag += phatnhac.listBox1.Items[0].ToString();
                            M_unicast.Text = "Phát nhạc tới :" + phatnhac.listBox1.Items[0].ToString();
                            M_unicast.Show();
                        }
                        else
                        { M_unicast.Show(); }
                    } // che do unicast/
            }
        }





我显示:



I show it:

private void image_open_MouseDoubleClick(object sender, MouseEventArgs e)
       {

           PictureBox abc = (PictureBox)sender;
           objects.speaker ImageTag = abc.Tag as objects.speaker;
           if (demsoip == 1) // unicast
               {
                  if (M_unicast.Tag.ToString() == ImageTag.ip)
                       {
                           M_unicast.Show();
                       }
                 }
        }





:(。我无法显示form2_1。我最后只能显示form2 :(。

你能帮助我吗?非常感谢你:)。



:(. i cant show form2_1. i can only show form2 last :(.
Can you help me? Thank you very much :).

My english is Bad. expression is not good. i hope u can understand what i mean.

推荐答案

事实上,很难理解你的问题是什么;我不能不是一个正确的解释问题。您没有在代码示例中显示您正在使用的重要类型和成员的定义。您应该考虑显示重现问题所需的所有步骤。



所以,为了帮助你,我只能给你一些笔记。



首先,一次又一次地创建某种形式的实例是非常糟糕的用 Form.ShowDialog 显示为一个模态形式。当你再次显示它时,你会丢失它之前状态的所有细节,因为这是一个非常不同的对象。它是为这些表单使用延迟初始化要好得多,实际上是在调用时创建一个实例,但如果它已经存在则使用先前创建的实例。从本质上讲,它可能类似于

Indeed, it's hard to understand what is your problem; "I can't" is not a proper explanation of a problem. You don't show the definition of important types and members you are using in your code sample. You should think at showing all steps needed to reproduce the problem.

So, to help you, I can only give you some notes.

First, it's really bad to create again and again an instance of some form used to show as a modal form with Form.ShowDialog. When you show it again, you loose all the detail of its previous state, because this is a really different object. It's much better to use lazy initialization for such forms, essentially creating an instance on call but using previously created instance if it already exists. Essentially, it could be something like
class Something {
    FormMusic phatnhac;

    //...

    void ShowFormMusic() {
        if (phatnhac == null) {
            phatnhac = new FormMusic();
            phatnhac.Owner = //... main form, which could be "this"
        }
        var dialogResult = phatnhac.ShowDialog();
        // use dialogResult
        // ...
    }
}



它也是像许多初学者一样,你可能会对从另一种形式访问一个人感到困惑。首先,你需要在未定类型和实例中非常舒服。但我的文章可能会有所帮助:一次回答的许多问题 - Windows之间的协作表格或WPF Windows [ ^ ]。



在我过去的答案中,我还解释了表单所有权的作用和 ShowInTaskBar

当任务栏图标隐藏C#时如何聚焦模态表单[ ^ ],

隐藏任何打开的应用程序背后隐藏的主表单点击年龄框按钮 [ ^ ],

父子表单关系 [ ^ ]。



顺便说一下,永远不要使用 form2_1 之类的名称或窗口2 。它使代码不可读,甚至无法维护。帮自己一个大忙:利用Microsoft命名约定,因此永远不会使用任何自动生成的名称,这些名称总是违反这些约定(由于明显的原因)。始终重命名所有内容,以正确拼写拼写和清晰理解的语义名称。毕竟,你有Visual Studio重构引擎。



-SA


It's also possible that you, like many beginners, are puzzled with accessing one from from another form. First of all, you need to be very comfortable in undestanding types and instanced. But my article could be helpful: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows[^].

In my past answers, I also explained the role of form ownership and ShowInTaskBar:
How to focus a modal form when its taskbar icon hidden C#[^],
Main Form Hidden Behind any open Application on Messagebox button click[^],
Parent Child Form Relationship[^].

By the way, never use names like form2_1 or form2. It makes code not readable, even not maintainable. Do yourself a big favor: utilize Microsoft naming conventions and, hence, never use any auto-generated names, which always violate those conventions (by apparent reasons). Always rename everything to give things correctly spelled and clearly understood semantic names. After all, you have the Visual Studio refactoring engine.

—SA


这篇关于创作和表演形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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