我想只打开一个同样的窗口,条件是搜索到了名字相同的窗口时,将焦点给窗口,否则打开!            foreach (Form Form in )
            {
                if (Form.Name == "与好友" + sbFriends.SeletedItem.Tag.ToString() + "聊天中")
                {
                    Form.Activate();
                    return;
                }
            }那个in后面应该填什么呢???

解决方案 »

  1.   

    就个人所知,没有这种用法,必须使用
    [DllImport("User32.dll",EntryPoint="FindWindow")]   
    private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
    通过句柄来获取窗口对象,然后才能使用这个对象。
      

  2.   

    如果你有兴趣可以去http://www.cnblogs.com/jackcovey/archive/2007/08/11/852051.html这里面具体介绍了WINDOWS中各API的说明,另外GOOGLE也有很多问题已经被解决了的,下班了,HOHO,回家咯。
      

  3.   

    /// <summary>
            /// 不重复打开MDI子窗体
            /// 调用:this.OpenMDIWindow(typeof(窗体名).ToString());
            /// </summary>
            /// <param name="ChildTypeString">窗体名称(typeof(FormName).ToString())</param>
            private void OpenMDIWindow(string ChildTypeString)
            {
                Form myChild = null;
                if (!ContainMDIChild(ChildTypeString))
                {
                    // Get current process assembly
                    Assembly assembly = Assembly.GetExecutingAssembly();
                    // Create data type using type string
                    Type typForm = assembly.GetType(ChildTypeString);
                    // Create object using type's "InvokeMember" method
                    Object obj = typForm.InvokeMember(
                        null,
                        BindingFlags.DeclaredOnly |
                        BindingFlags.Public | BindingFlags.NonPublic |
                        BindingFlags.Instance | BindingFlags.CreateInstance,
                        null,
                        null,
                        null);
                    // Show child form 
                    if (obj != null)
                    {
                        this.Refresh();
                        //Form frmTip = LoadTip.ShowTipForm(this, "正在加载窗体");                    myChild = obj as Form;
                        myChild.MdiParent = this;
                        myChild.WindowState = FormWindowState.Maximized;
                        myChild.Show();
                        myChild.Focus();                    //frmTip.Close();                }
                }
            }        /// <summary>
            /// Search mdi child form by specific type string
            /// </summary>
            /// <param name="ChildTypeString"></param>
            /// <returns></returns>
            private bool ContainMDIChild(string ChildTypeString)
            {
                Form myMDIChild = null;
                foreach (Form f in this.MdiChildren)
                {
                    if (f.GetType().ToString() == ChildTypeString)
                    {
                        // found it 
                        myMDIChild = f;
                        break;
                    }
                }
                // Show the exist form
                if (myMDIChild != null)
                {
                    myMDIChild.TopMost = true;
                    myMDIChild.Show();
                    myMDIChild.Focus();
                    return true;
                }
                else
                {
                    return false;
                }
            }
    调用:
    this.OpenMDIWindow(typeof(窗体名).ToString());
      

  4.   

    啊,感谢各位的回答,解决问题的是4楼的回答,我把我的代码给大家参考下。            for (int i = 0; i < Application.OpenForms.Count; i++)
                {
                    if (Application.OpenForms[i].Tag != null)
                    {
                        if (Application.OpenForms[i].Tag.ToString() == this.sbFriends.SeletedItem.Tag.ToString())
                        {
                            Application.OpenForms[i].Focus();
                            return;
                        }
                    }
                }
                ChatForm.FriendId = (int)sbFriends.SeletedItem.Tag;
                ChatForm cf = new ChatForm();
                cf.getMainForm(this);
                cf.Show();
      

  5.   

    in 后面填 Control 呢?