【问题标题】:How do I feed values to the statusStrip from a form control?如何从表单控件向 statusStrip 提供值?
【发布时间】:2016-05-27 12:12:14
【问题描述】:

这是我的控件的上下文:

/*
Form
    StatusStrip
        ToolStripStatusLabel

    TableLayoutPanel
        MyGenioView
*/

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

public void MyMouseMove(Object sender, MouseEventArgs e)
{
    Point ptCurrent = new Point(e.X, e.Y);
    // 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 标签对象的文本。那不是我的问题。我的问题与我的鼠标处理程序事件的上下文以及它与表单的关系有关。请参阅问题开头所述的控件上下文。到目前为止,cmets 还没有考虑到这一点。

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

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);
}

【问题讨论】:

  • 在设计时为其添加标签并设置标签的Text
  • 当您使用设计器将状态标签添加到 StatusStrip 时,您将获得它的标识符名称。它只是 toolStripStatusLabel1.Text = strCoordinate;相当不清楚这是哪里出了问题。
  • @RezaAghaei,我已经有了“statusLabel”。我不知道如何从给定的上下文中访问它。
  • 如果是同一个表格,直接用this.toolStripStatusLabel1.Text = "Some text";
  • @HansPassant 我知道这一点。重点是无法从 MyGenioView 访问变量 toolStripStatusLabel。

标签: c# winforms onmousemove statusstrip


【解决方案1】:

您有多种更新状态的选项:

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

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

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

1- 在用户控件中注入Action<Point>

在用户控件中注入一个Action<Point> 并在MouseMove 中使用它。为此,请将其置于用户控制中:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Action<Point> 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<MouseEventArgs> 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();
}

【讨论】:

  • 太棒了!我不得不对使用Action&lt;Point&gt; 做一个小改动。我改用OdGePoint3d。我必须在另一个处理程序中创建用户控件的表单中的位(出于某种原因,它在表单加载方法中为空)。我可能会及时改变这一点。谢谢!
  • 不客气 :) 很高兴听到你的主要想法。当您了解主要思想并了解模式后,您可以简单地应用它并对其进行改进。
猜你喜欢
  • 2021-07-05
  • 1970-01-01
  • 1970-01-01
  • 2010-10-05
  • 1970-01-01
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
  • 2011-11-04
相关资源
最近更新 更多