如何从表单控件将值提供给statusStrip? [英] How do I feed values to the statusStrip from a form control?

查看:253
本文介绍了如何从表单控件将值提供给statusStrip?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的控件的上下文:

This is the context of my controls:

/*
Form
    StatusStrip
        ToolStripStatusLabel

    TableLayoutPanel
        MyGenioView
*/

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

So, MyGenioView is intercepting the MouseMove event handler. The code that is already there is for a rubber band rectangle. So I have:

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的值.我不知道该怎么做.

What I can't get my head around is that I want to set the value of statusStrip1.statusLabel from the MyGenioView MouseMove handler. I can't work out how to do it.

我要使用的代码是:

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

但是将其提供给主要表单statusStrip对象的正确方法是什么?

But what is the right way to feed it to the main forms statusStrip object?

感谢您的帮助.

更新:

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

I know how to set the text of a statusStrip label object. That is not my issue. My issue is related to context of my mouse handler event and it's relationship to the form. Please see the context of the controls as described at the start of the question. The comments so far have not taken that into account.

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

This is the current place in the form that I create the MyGenioView object (that receives the mouse handler):

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

推荐答案

您有多种选择来更新状态:

You have multiple options to update status:

  1. 在用户控件中插入Action<Point>
  2. 在用户控件中创建StatusUpdate事件
  3. 您还可以使用控件的层次结构来访问控件,例如,在用户控件this.ParentForm是您的父窗体中,并且可以使用Controls集合或在窗体中将其公开来找到目标控件.
  1. Inject an Action<Point> in the user control
  2. Create a StatusUpdate event in the user control
  3. You also can access the control using hierarchy of controls, for example in the user control this.ParentForm is your parent form and you can find the target control using Controls collection or by making it public in the form.

前两个选项要好得多,因为可以将控件与窗体分离,并且用户控件可以以这种方式用于许多窗体和其他容器.提供状态更新方法取决于容器.

The first two options are much better because decouple your control from the form and your user control can be used on many forms and other containers this way. Providing a way of updating status is up to container.

最好的选择是创建并使用事件.

The best option is creating and consuming the event.

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

1- Inject an Action<Point> in the user control

在用户控件中插入Action<Point>,并在MouseMove中使用它.为此,请将其放在用户控件"中:

Inject an Action<Point> in the user control and use it in MouseMove. To do so, put this in User Control:

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

并将此代码放在表单上:

And put this code on Form:

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

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

2- Create a StatusUpdate event in the user control

在用户控件中创建一个StatusUpdate事件,并在MouseMove中引发它,并以表单形式使用该事件.您也可以使用MouseMove事件本身.

Create a StatusUpdate event in the user control and raise it in MouseMove and consume the event in the form. Also you can use the MouseMove event itself.

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

To do so, put this code in user control:

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

然后以表格形式,添加以下代码:

And then put in form, put this code:

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

这篇关于如何从表单控件将值提供给statusStrip?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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