1

我有 2 个 winform,我想在它们之间传递数据。

表格 1 只不过是一个大图片框。

Form2 在表单 1 之上始终保持打开状态。它充当带有退出按钮的半透明控件,我添加了一个轨迹栏。退出按钮工作得很好,但是如果值发生变化,我将无法读取轨迹栏的值。

我想要发生的是,如果跟踪栏的值发生变化,它将值发送到第一个表单并触发一个事件。

我哪里错了?

表格 1 是

    public sbyte value
    {
        get { return Exitform.myValue; }
    }

    public Fullscreenpreview(string filename)
    {
        InitializeComponent();
        this.pictureBox1.MouseMove += this.pictureBox_MouseMove;

        pictureBox1.Image = new Bitmap(filename);
        pictureBox1.Refresh();

        //to show exit button which is a seperate form
        var frm3 = new Exitform();
        frm3.FormClosed += (o, e) => this.Close();

        frm3.Show();
        frm3.TopMost = true;
        //to show exit button which is a seperate form

        if (myValue != 0)
        {
            MessageBox.Show("zoinks the value is = " + value);
        }
    }

表格 2 是

public partial class Exitform : Form
{
    private const int CpNocloseButton = 0x200;

    private bool mouseIsDown = false;
    private Point firstPoint;

    public static sbyte myValue = 0;

    public Exitform()
    {
        InitializeComponent();
        this.TopMost = false;
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams myCp = base.CreateParams;
            myCp.ClassStyle = myCp.ClassStyle | CpNocloseButton;
            return myCp;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void label1_MouseDown(object sender, MouseEventArgs e)
    {
        firstPoint = e.Location;
        mouseIsDown = true;
        //http://stackoverflow.com/questions/3441762/how-can-i-move-windows-when-mouse-down
    }

    private void label1_MouseUp(object sender, MouseEventArgs e)
    {
        mouseIsDown = false;
    }

    private void label1_MouseMove(object sender, MouseEventArgs e)
    {
        if (mouseIsDown)
        {
            // Get the difference between the two points
            int xDiff = firstPoint.X - e.Location.X;
            int yDiff = firstPoint.Y - e.Location.Y;

            // Set the new point
            int x = this.Location.X - xDiff;
            int y = this.Location.Y - yDiff;
            this.Location = new Point(x, y);
        }
    }

    private void contrast_trackbar_Scroll(object sender, EventArgs e)
    {
        myValue = 1;
    }
}

谢谢安迪

4

2 回答 2

1

您可以像绑定到 Close 事件一样绑定到 Trackbar Scroll 事件。您只需要准备 Form1 和 Form2 之间的“连接”。

  1. 在 Form2 中创建一个公共属性(例如 int TrackBarValue),它返回 TrackBar 的实际值。

  2. 在 Form2 中创建一个公共自定义事件(例如 EventHandler TrackBarValueChanged),该事件将在 TrackBar 值更改时触发

  3. 从 Form1 绑定到 TrackBarValueChanged 事件

如果仍然不清楚,请在评论中告诉我。

表格1.h

public Fullscreenpreview(string filename)
{
    InitializeComponent();
    this.pictureBox1.MouseMove += this.pictureBox_MouseMove;

    pictureBox1.Image = new Bitmap(filename);
    pictureBox1.Refresh();

    //to show exit button which is a seperate form
    frm3 = new Exitform();
    frm3.FormClosed += (o, e) => this.Close();
    frm3.OnTrackBarValueChanged += new EventHandler(TrackBarValueChanged_Event);

    frm3.Show();
    frm3.TopMost = true;
    //to show exit button which is a seperate form
}


private void TrackBarValueChanged_Event(Object sender, EventArgs e)
{
     ExitForm exit = (ExitForm)sender;
     MessageBox.Show("zoinks the value is = " + exit.TrackBarValue.ToString()); 
}

表格2.h

public partial class Exitform : Form
{
    private const int CpNocloseButton = 0x200;

    private bool mouseIsDown = false;
    private Point firstPoint;

    public event EventHandler OnTrackBarValueChanged;

    public Exitform()
    {
        InitializeComponent();
        this.TopMost = false;
    }

    public int TrackBarValue
    {
        get
        {
            return contrast_trackbar.Value;
        }
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams myCp = base.CreateParams;
            myCp.ClassStyle = myCp.ClassStyle | CpNocloseButton;
            return myCp;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void label1_MouseDown(object sender, MouseEventArgs e)
    {
        firstPoint = e.Location;
        mouseIsDown = true;
        //http://stackoverflow.com/questions/3441762/how-can-i-move-windows-when-mouse-down
    }

    private void label1_MouseUp(object sender, MouseEventArgs e)
    {
        mouseIsDown = false;
    }

    private void label1_MouseMove(object sender, MouseEventArgs e)
    {
        if (mouseIsDown)
        {
            // Get the difference between the two points
            int xDiff = firstPoint.X - e.Location.X;
            int yDiff = firstPoint.Y - e.Location.Y;

            // Set the new point
            int x = this.Location.X - xDiff;
            int y = this.Location.Y - yDiff;
            this.Location = new Point(x, y);
        }
    }

    private void contrast_trackbar_Scroll(object sender, EventArgs e)
    {
        OnTrackBarValueChanged(this, EventArgs.Empty);
    }
}
于 2013-05-04T23:33:58.317 回答
0

Zoltan 提供的答案和 stackoverflow Zoom 中提供的答案使用两种形式。为我提供了我正在寻找的答案。Zoom 使用了两种形式,我没有立即从标题中找到。

我将其发布为答案的唯一原因是将新代码显示为 Zoltan 答案下方的单独条目,而不必向下滚动到我的代码下方以查看他的回复。

由于我只是在试验,变量名称已从第一次发布中更改。

表格 1

public sbyte contrast
{
    get { return Exitform.contrastvalue; }
}

public Fullscreenpreview(string filename)
{
    InitializeComponent();
    this.pictureBox1.MouseMove += this.pictureBox_MouseMove;

    pictureBox1.Image = new Bitmap(filename);
    pictureBox1.Refresh();

    //to show exit button which is a separate form
    var frm3 = new Exitform();
    frm3.FormClosed += (o, e) => this.Close();
    frm3.TrackBarMoved += new Action<int>(ChangeContrast);//<< Added

    frm3.Show();
    frm3.TopMost = true;
    //to show exit button which is a separate form

    frm3.FormClosed -= (o, e) => this.Close();//<< for memory leaks
    frm3.TrackBarMoved -= new Action<int>(ChangeContrast);//<< for memory leaks
}

private void ChangeContrast(int contrastVal)
{
    MessageBox.Show("zoinks the value is = " + contrastVal);
}

表格 2

public partial class Exitform : Form
{
    private const int CpNocloseButton = 0x200;

    private bool mouseIsDown = false;
    private Point firstPoint;

    public static sbyte contrastvalue = 0;

    public Exitform()
    {
        InitializeComponent();
        this.TopMost = false;
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams myCp = base.CreateParams;
            myCp.ClassStyle = myCp.ClassStyle | CpNocloseButton;
            return myCp;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void label1_MouseDown(object sender, MouseEventArgs e)
    {
        firstPoint = e.Location;
        mouseIsDown = true;
        //https://stackoverflow.com/questions/3441762/how-can-i-move-windows-when-mouse-down
    }

    private void label1_MouseUp(object sender, MouseEventArgs e)
    {
        mouseIsDown = false;
    }

    private void label1_MouseMove(object sender, MouseEventArgs e)
    {
        if (mouseIsDown)
        {
            // Get the difference between the two points
            int xDiff = firstPoint.X - e.Location.X;
            int yDiff = firstPoint.Y - e.Location.Y;

            // Set the new point
            int x = this.Location.X - xDiff;
            int y = this.Location.Y - yDiff;
            this.Location = new Point(x, y);
        }
    }

    public event Action<int> TrackBarMoved;// << public event to sense if the trackbar is moved
    private void contrast_trackbar_Scroll(object sender, EventArgs e)
    {
        sbyte contrastVal = (sbyte)(this.contrast_trackbar.Value);//local variable just for use in trackbar

        TrackBarMoved(contrast_trackbar.Value);//pull value is trackbar moved

        string trackerbarvalue = Convert.ToString(contrastVal, CultureInfo.CurrentCulture);// convert to string to push to label
        contrastlabel.Text = trackerbarvalue;//actual push to label

        contrastvalue = (sbyte)contrast_trackbar.Value;
    }
}

除了一些变量重命名、内部清理以及向标签报告跟踪栏的值之外,代码只有一些小的更改。正如 Zoltan 正确指出的那样,我需要以与绑定 close 事件相同的方式绑定 trackbar 滚动事件。这是完成的。

frm3.TrackBarMoved += new Action<int>(ChangeContrast);//<< Added

我还添加了取消订阅以帮助防止资源泄漏

frm3.FormClosed -= (o, e) => this.Close();//<< for memory leaks
frm3.TrackBarMoved -= new Action<int>(ChangeContrast);//<< for memory leaks

另一种形式

public event Action<int> TrackBarMoved;// << public event to sense if the trackbar is moved

用于广播事件

谢谢安迪

于 2013-05-05T11:41:02.530 回答