【问题标题】:Control a Form from another Hidden Form in C#从 C# 中的另一个隐藏窗体控制窗体
【发布时间】:2014-10-09 23:21:27
【问题描述】:

我正在处理一个向服务器发送登录请求的客户端,如果它被接受,主窗口将被隐藏,用户将开始使用名为 user_form 的表单 2 ...并且有 3 个按钮可用于user_form 上的用户(注销 - 退出 - 隐藏)

我想从已经隐藏的主窗口中控制它们,,,

这是我正在使用的主要形式的代码......但它没有按我想要的方式工作。

switch (client.LoginInfo.Limited)
{
    case true:
        this.Hide();
        User_Form LF = new User_Form(client);
        LF.AdminControlIsVisible = false;
        DialogResult dr = LF.ShowDialog(this);
        if (dr == System.Windows.Forms.DialogResult.Cancel)
        {
            //logout
        }
        else if (dr == System.Windows.Forms.DialogResult.OK)
        {
            if (client != null)
            {
                //hide
            }
            else
            {
                UpdateSystemMessage("No response from the server !!");
            }
            return;
        }
        break;
    case false:
        User_Form LF = new User_Form(client);
        LF.AdminControlIsVisible = false;
        DialogResult dr = LF.ShowDialog(this);
        this.Visible = false;
        if (dr == System.Windows.Forms.DialogResult.Cancel)
        {
            //logout
        }
        else if (dr == System.Windows.Forms.DialogResult.OK)
        {
            if (client != null)
            {
                //hide
            }
            else
            {
                UpdateSystemMessage("No response from the server !!");
            }
            return;
        }
        break;
}

有什么办法吗?

【问题讨论】:

    标签: c# forms controls hidden


    【解决方案1】:

    我通过添加一个新线程而不是主线程来运行 .net 套接字来修复它。

    【讨论】:

      【解决方案2】:

      如果我了解您要做什么,一种简单的方法是您所控制的表单具有公共功能,当被调用时会影响表单。然后,您需要将这些表单的实例传递给控制的表单(通常在构造函数中)。然后,您可以从控制器表单中调用这些函数,从而影响受控表单。

      例子:

      public class ControlledForm : Form 
      {
          //...        
      
          //This founction will affect the form
          public void DoSomething(int Params)
          {
          //Affect Form
          }
      
          private void SomethingEventHandler() //function that calls the new form and hides this one
          {
              //Give the construction this (the instance of the form) in parameter
              ControllerForm newControllerForm = new ControllerForm(this);
              newControllerForm.Show();
              this.Hide();
          }
      }
      
      public class ControllerForm : Form
      {
          //Constructor that receives the instance of the controlled form in parameter
          public ControllerForm(ControlledForm CF)
          {
              _ControlledForm = CF;
              InitializeComponents();
          }
      
          private ControlledForm _ControlledForm;
      
          private void SomethingElseEventHandler() //Function that modifies the controlled form
          {
              //This will effectively affect the form
              _ControlledForm.DoSomething(12);
          }    
      }
      

      如果这能回答你的问题,我不知道,但自从我偶然发现这个问题以来,我就是这样解决的。

      【讨论】:

      • 你不明白我的意思,我的意思是我有 2 个表单(form1,form2)我想隐藏表单 1 并显示表单 2,但我需要处理隐藏表单中的 3 个按钮(form1 )我的意思是(表单 1 控制表单 2 中的 3 个按钮),其中 2 个按钮正在显示表单 1 并关闭表单 2 .. 仅此而已。
      • 哈,那么您需要在隐藏表单中创建公共函数并手动订阅按钮的点击事件(请参阅:msdn.microsoft.com/en-us/library/ms366768.aspx)。您仍然需要获取隐藏表单的实例,以便
      • 那么有什么简单的例子吗?
      • 谢谢我修复了它,我现在会发布答案
      【解决方案3】:

      如果你只是想很好地控制显示在一个表单上的东西,从另一个表单,也许这可以帮助你......(你没有提供相关代码来给出一个使用你的项目的例子,所以我只是做了2 个表单可以相互更改背景颜色,而无需过多地“了解”对方,通过使用一个保存模型数据并由两个表单共享的对象...)

      Form1.cs:

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      
      namespace WindowsFormsApplication1
      {
          public partial class Form1 : Form
          {
              MyModelClass model = new MyModelClass();
              public Form1()
              {
                  InitializeComponent();
                  model.AnotherColor = model.AColor = System.Drawing.Color.FromKnownColor(KnownColor.Control);
                  bindingSource1.DataSource = model;
                  this.DataBindings.Add("BackColor", bindingSource1, "AColor");
                  this.DataBindings.Add("Visible", bindingSource1, "ABool");
                  new Form2(model).Show();
              }
      
              private BindingSource bindingSource1;
              private Button button1;
              private Button button2;
              private Button button3;
              private Button button4;
              private Button button5;
      
              /// <summary>
              /// Required designer variable.
              /// </summary>
              private System.ComponentModel.IContainer components = null;
      
              /// <summary>
              /// Clean up any resources being used.
              /// </summary>
              /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
              protected override void Dispose(bool disposing)
              {
                  if (disposing && (components != null))
                  {
                      components.Dispose();
                  }
                  base.Dispose(disposing);
              }
      
              #region Windows Form Designer generated code
      
              /// <summary>
              /// Required method for Designer support - do not modify
              /// the contents of this method with the code editor.
              /// </summary>
              private void InitializeComponent()
              {
                  this.components = new System.ComponentModel.Container();
                  this.bindingSource1 = new System.Windows.Forms.BindingSource(this.components);
                  this.button1 = new System.Windows.Forms.Button();
                  this.button2 = new System.Windows.Forms.Button();
                  this.button3 = new System.Windows.Forms.Button();
                  this.button4 = new System.Windows.Forms.Button();
                  this.button5 = new System.Windows.Forms.Button();
                  ((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).BeginInit();
                  this.SuspendLayout();
                  // 
                  // button1
                  // 
                  this.button1.Location = new System.Drawing.Point(12, 12);
                  this.button1.Name = "button1";
                  this.button1.Size = new System.Drawing.Size(127, 23);
                  this.button1.TabIndex = 0;
                  this.button1.Text = "Form2 -> green";
                  this.button1.UseVisualStyleBackColor = true;
                  this.button1.Click += new System.EventHandler(this.button1_Click);
                  // 
                  // button2
                  // 
                  this.button2.Location = new System.Drawing.Point(12, 41);
                  this.button2.Name = "button2";
                  this.button2.Size = new System.Drawing.Size(127, 23);
                  this.button2.TabIndex = 1;
                  this.button2.Text = "Form2 -> red";
                  this.button2.UseVisualStyleBackColor = true;
                  this.button2.Click += new System.EventHandler(this.button2_Click);
                  // 
                  // button3
                  // 
                  this.button3.Location = new System.Drawing.Point(12, 70);
                  this.button3.Name = "button3";
                  this.button3.Size = new System.Drawing.Size(127, 23);
                  this.button3.TabIndex = 2;
                  this.button3.Text = "Form2 -> Visible";
                  this.button3.UseVisualStyleBackColor = true;
                  this.button3.Click += new System.EventHandler(this.button3_Click);
                  // 
                  // button4
                  // 
                  this.button4.Location = new System.Drawing.Point(12, 99);
                  this.button4.Name = "button4";
                  this.button4.Size = new System.Drawing.Size(127, 23);
                  this.button4.TabIndex = 3;
                  this.button4.Text = "Form2 -> Invisible";
                  this.button4.UseVisualStyleBackColor = true;
                  this.button4.Click += new System.EventHandler(this.button4_Click);
                  // 
                  // button5
                  // 
                  this.button5.Location = new System.Drawing.Point(46, 159);
                  this.button5.Name = "button5";
                  this.button5.Size = new System.Drawing.Size(106, 42);
                  this.button5.TabIndex = 4;
                  this.button5.Text = "some important form1 button";
                  this.button5.UseVisualStyleBackColor = true;
                  this.button5.Click += new System.EventHandler(this.button5_Click);
                  // 
                  // Form1
                  // 
                  this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                  this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                  this.ClientSize = new System.Drawing.Size(284, 262);
                  this.Controls.Add(this.button5);
                  this.Controls.Add(this.button4);
                  this.Controls.Add(this.button3);
                  this.Controls.Add(this.button2);
                  this.Controls.Add(this.button1);
                  this.Name = "Form1";
                  this.Text = "Form1";
                  ((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).EndInit();
                  this.ResumeLayout(false);
      
              }
      
              #endregion
      
              private void button1_Click(object sender, EventArgs e)
              {
                  model.AnotherColor = Color.Green;
                  model.doSomething();
              }
      
              private void button2_Click(object sender, EventArgs e)
              {
                  model.AnotherColor = Color.Red;
                  model.doSomething();
              }
      
              private void button3_Click(object sender, EventArgs e)
              {
                  model.AnotherBool = true;
              }
      
              private void button4_Click(object sender, EventArgs e)
              {
                  model.AnotherBool = false;
              }
      
              private void button5_Click(object sender, EventArgs e)
              {
                  model.someImportantMethod();
              }
          }
      }
      

      Form2.cs:

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      
      namespace WindowsFormsApplication1
      {
          public partial class Form2 : Form
          {
              MyModelClass model;
              private Button button4;
              private Button button3;
              private Button button5;
              int callCounter = 0;
              public Form2(MyModelClass model)
              {
                  InitializeComponent();
                  this.model = model;
                  bindingSource1.DataSource = model;
                  DataBindings.Add("BackColor", bindingSource1, "AnotherColor");
                  this.DataBindings.Add("Visible", bindingSource1, "AnotherBool");
      
                  model.PropertyChanged += new PropertyChangedEventHandler(model_PropertyChanged);
              }
      
              void model_PropertyChanged(object sender, PropertyChangedEventArgs e)
              {
                  label1.Text = String.Format("PropertyChanged was called {0} times.", ++callCounter);
              }
      
              private BindingSource bindingSource1;
              private Button button2;
              private Button button1;
              private Label label1;
              /// <summary>
              /// Required designer variable.
              /// </summary>
              private System.ComponentModel.IContainer components = null;
      
              /// <summary>
              /// Clean up any resources being used.
              /// </summary>
              /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
              protected override void Dispose(bool disposing)
              {
                  if (disposing && (components != null))
                  {
                      components.Dispose();
                  }
                  base.Dispose(disposing);
              }
      
              #region Windows Form Designer generated code
      
              /// <summary>
              /// Required method for Designer support - do not modify
              /// the contents of this method with the code editor.
              /// </summary>
              private void InitializeComponent()
              {
                  this.components = new System.ComponentModel.Container();
                  this.bindingSource1 = new System.Windows.Forms.BindingSource(this.components);
                  this.button2 = new System.Windows.Forms.Button();
                  this.button1 = new System.Windows.Forms.Button();
                  this.label1 = new System.Windows.Forms.Label();
                  this.button4 = new System.Windows.Forms.Button();
                  this.button3 = new System.Windows.Forms.Button();
                  this.button5 = new System.Windows.Forms.Button();
                  ((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).BeginInit();
                  this.SuspendLayout();
                  // 
                  // button2
                  // 
                  this.button2.Location = new System.Drawing.Point(18, 41);
                  this.button2.Name = "button2";
                  this.button2.Size = new System.Drawing.Size(127, 23);
                  this.button2.TabIndex = 3;
                  this.button2.Text = "Form1 -> red";
                  this.button2.UseVisualStyleBackColor = true;
                  this.button2.Click += new System.EventHandler(this.button2_Click);
                  // 
                  // button1
                  // 
                  this.button1.Location = new System.Drawing.Point(18, 12);
                  this.button1.Name = "button1";
                  this.button1.Size = new System.Drawing.Size(127, 23);
                  this.button1.TabIndex = 2;
                  this.button1.Text = "Form1 -> green";
                  this.button1.UseVisualStyleBackColor = true;
                  this.button1.Click += new System.EventHandler(this.button1_Click);
                  // 
                  // label1
                  // 
                  this.label1.AutoSize = true;
                  this.label1.Location = new System.Drawing.Point(15, 231);
                  this.label1.Name = "label1";
                  this.label1.Size = new System.Drawing.Size(181, 13);
                  this.label1.TabIndex = 4;
                  this.label1.Text = "PropertyChanged was called 0 times.";
                  // 
                  // button4
                  // 
                  this.button4.Location = new System.Drawing.Point(18, 99);
                  this.button4.Name = "button4";
                  this.button4.Size = new System.Drawing.Size(127, 23);
                  this.button4.TabIndex = 6;
                  this.button4.Text = "Form1 -> Invisible";
                  this.button4.UseVisualStyleBackColor = true;
                  this.button4.Click += new System.EventHandler(this.button4_Click);
                  // 
                  // button3
                  // 
                  this.button3.Location = new System.Drawing.Point(18, 70);
                  this.button3.Name = "button3";
                  this.button3.Size = new System.Drawing.Size(127, 23);
                  this.button3.TabIndex = 5;
                  this.button3.Text = "Form1 -> Visible";
                  this.button3.UseVisualStyleBackColor = true;
                  this.button3.Click += new System.EventHandler(this.button3_Click);
                  // 
                  // button5
                  // 
                  this.button5.Location = new System.Drawing.Point(154, 164);
                  this.button5.Name = "button5";
                  this.button5.Size = new System.Drawing.Size(94, 40);
                  this.button5.TabIndex = 7;
                  this.button5.Text = "some less important button";
                  this.button5.UseVisualStyleBackColor = true;
                  this.button5.Click += new System.EventHandler(this.button5_Click);
                  // 
                  // Form2
                  // 
                  this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                  this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                  this.ClientSize = new System.Drawing.Size(284, 262);
                  this.Controls.Add(this.button5);
                  this.Controls.Add(this.button4);
                  this.Controls.Add(this.button3);
                  this.Controls.Add(this.label1);
                  this.Controls.Add(this.button2);
                  this.Controls.Add(this.button1);
                  this.Name = "Form2";
                  this.Text = "Form2";
                  ((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).EndInit();
                  this.ResumeLayout(false);
                  this.PerformLayout();
      
              }
      
              #endregion
      
              private void button1_Click(object sender, EventArgs e)
              {
                  model.AColor = Color.Green;
                  model.doSomething();
              }
      
              private void button2_Click(object sender, EventArgs e)
              {
                  model.AColor = Color.Red;
                  model.doSomething();
              }
      
              private void button3_Click(object sender, EventArgs e)
              {
                  model.ABool = true;
              }
      
              private void button4_Click(object sender, EventArgs e)
              {
                  model.ABool = false;
              }
      
              private void button5_Click(object sender, EventArgs e)
              {
                  MessageBox.Show("some less important button was clicked ... \nnow calling the same method that's called from form1's important button");
                  model.someImportantMethod();
              }
          }
      }
      

      MyModelClass.cs:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      
      namespace WindowsFormsApplication1
      {
          public class MyModelClass : System.ComponentModel.INotifyPropertyChanged
          {
              private System.Drawing.Color _aColor;
      
              public System.Drawing.Color AColor
              {
                  get { return _aColor; }
                  set { 
                      _aColor = value;
                      fireChanged("AColor");
                  }
              }
      
              private System.Drawing.Color _anotherColor;
      
              public System.Drawing.Color AnotherColor
              {
                  get { return _anotherColor; }
                  set { 
                      _anotherColor = value;
                      fireChanged("AnotherColor");
                  }
              }
      
              private bool _aBool = true;
      
              public bool ABool
              {
                  get { return _aBool; }
                  set
                  {
                      _aBool = value;
                      fireChanged("ABool");
                  }
              }
      
              private bool _anotherBool = true;
      
              public bool AnotherBool
              {
                  get { return _anotherBool; }
                  set
                  {
                      _anotherBool = value;
                      fireChanged("AnotherBool");
                  }
              }
      
      
              public void doSomething() 
              {
                  //some function here
                  System.Windows.Forms.MessageBox.Show("doSomething() was called"); 
              }
      
              private void fireChanged(string name)
              {
                  var evth = PropertyChanged;
                  if (evth != null)
                      evth(this, new System.ComponentModel.PropertyChangedEventArgs(name));
              }
      
              public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
      
              internal void someImportantMethod()
              {
                  System.Windows.Forms.MessageBox.Show("some important mehod, that is usually called from a button in from1");
              }
          }
      }
      

      编辑: 如果您想在表单上触发其他操作,您还可以在模型类上定义自己的事件并触发它们,就像PropertyChanged

      使用您的事件的类应该简单地注册一个事件处理程序,就像这里的 Form2 用于更新调用计数器一样


      edit2:更改示例代码以显示隐藏表单并将代码从 button_click 处理程序重新定位到单独的方法

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-04
        • 2016-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多