一种形式的Winform Trackbar值没有传递到另一种形式
本文关键字:另一种 Winform 一种 Trackbar | 更新日期: 2023-09-27 18:12:21
我有2个winforms,我想在它们之间传递数据。
表单1只不过是一个大的图片框。
form 2在form 1之上一直保持打开状态。它作为一个带有退出按钮的半透明控件,我添加了一个跟踪条。退出按钮工作得很好,但是如果值改变,我在读取跟踪栏的值时遇到麻烦。
我希望发生的是,如果trackbar的值发生变化,它将值发送到第一个表单并触发一个事件。
我做错了什么?
中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;
}
}
谢谢安迪
你可以像绑定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);
}
}
Zoltan提供的答案和stackoverflow提供的答案缩放使用两种形式。给了我一直在寻找的答案。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;
}
}
除了一些变量的重命名、内部清理和向标签报告trackbar的值之外,代码只有一些小的变化。正如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
用于广播事件
谢谢安迪