如何从表单控件向statusStrip提供值?

这是我的控件的上下文:

/* Form StatusStrip ToolStripStatusLabel TableLayoutPanel MyGenioView */ 

因此, MyGenioView正在拦截MouseMove事件处理程序。 已经存在的代码是橡皮筋矩形。 所以我有:

 public void MyMouseMove(Object sender, MouseEventArgs e) { Point ptCurrent = new Point(eX, eY); // If we "have the mouse", then we draw our lines. if (m_bHaveMouse) { // If we have drawn previously, draw again in // that spot to remove the lines. if (m_ptLast.X != -1) { MyDrawReversibleRectangle(m_ptOriginal, m_ptLast); } // Update last point. m_ptLast = ptCurrent; // Draw new lines. MyDrawReversibleRectangle(m_ptOriginal, ptCurrent); } // New code here } 

我无法理解的是,我想从MyGenioView MouseMove处理程序设置statusStrip1.statusLabel的值。 我无法弄明白该怎么做。

我想要使​​用的代码是:

 OdGePoint3d pt = GetWorldCoordinates(ptCurrent); String strCoordinate = String.Format("{0},{1}", ptCurrent.X, ptCurrent.Y); 

但是将它提供给主窗体statusStrip对象的正确方法是什么?

谢谢你的帮助。

更新:

知道如何设置statusStrip标签对象的文本。 那不是我的问题。 我的问题与我的鼠标处理程序事件的上下文及其与表单的关系有关。 请参阅问题开头所述的控件上下文。 到目前为止的评论没有考虑到这一点。

这是我创建MyGenioView对象(接收鼠标处理程序)的forms中的当前位置:

 private void viewToolStripMenuItem_Click(object sender, EventArgs e) { OdDbDatabase TDDatabase = m_oGenioView.GetDatabase(); if (m_oGenioViewCtrl != null) m_oGenioViewCtrl.DeleteContext(); tableLayoutPanel.RowCount = 1; tableLayoutPanel.ColumnCount = 1; m_oGenioViewCtrl = new MyGenioView(); m_oGenioViewCtrl.TDDatabase = TDDatabase; m_oGenioViewCtrl.ResetDevice(true); m_oGenioViewCtrl.Dock = DockStyle.Fill; m_oGenioViewCtrl.Margin = new Padding(1); tableLayoutPanel.Controls.Add(m_oGenioViewCtrl); } 

您有多个选项可以更新状态:

  1. 在用户控件中注入Action
  2. 在用户控件中创建StatusUpdate事件
  3. 您还可以使用控件层次结构访问控件,例如在用户控件this.ParentForm是您的父窗体,您可以使用Controls集合找到目标Controls或者在窗体中将其公开。

前两个选项要好得多,因为将控件与表单分离,用户控件可以通过这种方式用于多种表单和其他容器。 提供更新状态的方法取决于容器。

最好的选择是创建和使用事件。

1-在用户控件中注入Action

在用户控件中注入Action并在MouseMove使用它。 为此,请将其置于用户控件中:

 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public Action StatusUpdate{ get; set; } //Don't forget to assign the method to MouseMove event in your user control private void UserControl1_MouseMove(object sender, MouseEventArgs e) { if (StatusUpdate!= null) StatusUpdate(e.Location); } 

并将此代码放在表单上:

 private void Form1_Load(object sender, EventArgs e) { this.userControl11.StatusUpdate= p => this.toolStripStatusLabel1.Text=p.ToString(); } 

2-在用户控件中创建StatusUpdate事件

在用户控件中创建StatusUpdate事件并在MouseMove引发它并使用表单中的事件。 您也可以使用MouseMove事件本身。

为此,请将此代码放在用户控件中:

 public event EventHandler StatusUpdate; public void OnStatusUpdate(MouseEventArgs e) { var handler = StatusUpdate; if (handler != null) handler(this, e); } //Don't forget to assign the method to MouseMove event in your user control private void UserControl1_MouseMove(object sender, MouseEventArgs e) { OnStatusUpdate(e); } 

然后放入表格,把这段代码:

 //Don't forget to assign the method to StatusUpdate event in form void userControl11_StatusUpdate(object sender, MouseEventArgs e) { this.toolStripStatusLabel1.Text = e.Location.ToString(); }