我有2个winforms,我想在它们之间传递数据。
表格1只不过是一个大型的图片框。
Form2在表单1的顶部始终保持打开状态。它作为半透明控件使用“退出”按钮,我添加了一个轨迹栏。退出按钮工作正常,但是如果值发生变化,我无法读取跟踪栏的值。
我想要发生的是,如果轨迹栏的值发生变化,它会将值发送到第一个表单并触发事件。
我在哪里错了?
Form1
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;
}
}
由于 安迪
答案 0 :(得分:1)
您可以使用与绑定到Close事件相同的方式绑定到Trackbar Scroll事件。您只需要在Form1和Form2之间准备一个“连接”。
在Form2中创建一个公共属性(例如int TrackBarValue),它返回TrackBar的实际值。
在Form2中创建一个公共自定义事件(例如EventHandler TrackBarValueChanged),当TrackBar值更改时将触发该事件
从Form1绑定到TrackBarValueChanged事件
如果仍不清楚,请在评论中告诉我。
Form1.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());
}
Form2.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);
}
}
答案 1 :(得分:0)
Zoltan提供的答案和stackoverflow中提供的答案 Zoom using two forms。为我提供了我正在寻找的答案。使用我从标题中找不到的两种表格缩放。
我将此作为答案发布的唯一原因是将新代码显示为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正确指出的那样,我需要绑定trackbar滚动事件,就像我绑定close事件一样。这是通过。
完成的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
用于广播事件
由于 安迪