I have an PictureBox that I want to move up on the y axis after a button click. The problem is that the PictureBox simply appears there after the button click. I want it to move to the new position, not teleport. What do I do?
public partial class Form1 : Form
{
Point stageplus1 = new Point(164, 325);
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox5.Location = stageplus1;
}
private void pictureBox5_Click(object sender, EventArgs e)
{
}
}
Expanding on BJ Myers comment this is how you can implement that:
private void button2_Click(object sender, EventArgs e)
{
this.timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
// calculate new position
this.pictureBox1.Top++;
this.pictureBox1.Left++;
// when to stop
if (this.pictureBox1.Top > (this.Height - (2 * this.pictureBox1.Height)))
{
this.timer1.Enabled = false;
}
}
I used the default Timer control that raises a Tick at an interval. The Tick event gets executed on the UI thread so you don't have to be bothered by cross thread errors.
If added to your form this is what you'll get:
If you need to animate more stuff you might want to look into using a Background worker and helper classes like I show in this answer of mine
Related
I use this code to refresh my from it work good for one time after that the timer stopped with me
private void Button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval = 900000;//5 minutes
timer1.Tick += new System.EventHandler(Timer1_Tick);
timer1.Start();
}
private void Timer1_Tick(object sender, EventArgs e)
{
//do whatever you want
RefreshMyForm();
}
private void RefreshMyForm()
{
this.Hide();
Graph1 graph = new Graph1();
graph.Show();
}
i don't know what i miss in this code
it hide the from and didn't open again
start refresh that what i looking form
You need to move the timer deceleration out of the button click and make it "global" to the class. Also, set it up on the Form_Load (make sure you wire up the Form_Load method to your Form_Load event.
Also, your hide logic is a bit faulty. You hide the form, then create a graph (but don't attach it to the Form) then show it. Added some comments below to help you navigate these issues.
private System.Windows.Forms.Timer timer1;
private void Form_Load(object sender, EventArgs e)
{
timer1 = new System.Windows.Forms.Timer();
timer1.Interval = 900000;//5 minutes
timer1.Tick += new System.EventHandler(Timer1_Tick);
}
private void Button1_Click(object sender, EventArgs e)
{
if (!timer1.Enabled)
timer1.Start();
}
private void Timer1_Tick(object sender, EventArgs e)
{
//do whatever you want
RefreshMyForm();
}
private void RefreshMyForm()
{
// Do your data update logic here
this.Refresh();
}
So I have a matrix of panels (maybe will change for Picture Boxes in the future), and what i want is that every time i press one of the panels after pressing the button on the toolbox it will change it's background to a certain picture.
Right now what i have is:
private void EtapaInicial_Click(object sender, EventArgs e)
{
EtapaInicialWasClicked = true;
}
private void panel_Click(object sender, EventArgs e)
{
if (EtapaInicialWasClicked)
{
panel1.BackgroundImage = Symbols.EtapaInicialbm;
EtapaInicialWasClicked = false;
}
}
What I would like to change was the panel1 to make it work for every panel (otherwise it will only change panel1 independently of the panel i've clicked), is that possible?
Try the following
private void EtapaInicial_Click(object sender, EventArgs e)
=> EtapaInicialWasClicked = true;
private void panel_Click(object sender, EventArgs e)
{
if (EtapaInicialWasClicked)
{
(sender as Panel).BackgroundImage = Symbols.EtapaInicialbm;
EtapaInicialWasClicked = false;
}
}
Yes it is. You have to loop through each panel
and assign the same event handler but you have to make some changes in the event handler itself
foreach(var p in allPanels)
{
p.Click += panel_Click;
}
Then change your event handler like this
private void panel_Click(object sender, EventArgs e)
{
var p = (Panel)sender;
if (EtapaInicialWasClicked)
{
p .BackgroundImage = Symbols.EtapaInicialbm;
EtapaInicialWasClicked = false;
}
}
Remember the sender argument contains reference to the actual control that initiated the event but you have to cast it first in order to use it.
However if you want to store more data for the event you've just handled you can use the panel.Tag property. This can be used to store EtapaInicialWasClicked for example
I want to change the radiobutton location and make it move up while i am clicking button
tried this
private void up_MouseDown(object sender, MouseEventArgs e)
{
while(P.Location.Y>0)
P.Location = new System.Drawing.Point(P.Location.X, P.Location.Y - 1);
}
P is a radiobutton
I want it to keep moving up while I'm pressing, but it's just jumping up to the up of the form.
it's working good in debugging but it's really moving fast
I want to slow the movement of the radiobutton and make it visible
Actually you are starting a while loop that will not exit until your RadioButton is at the top of your Form wether you are still pressing the Button or not. You can slow it down by putting a Thread.Sleep in your loop that way it is slowed down visible.
private void up_MouseDown(object sender, MouseEventArgs e)
{
while (P.Location.Y > 0)
{
P.Location = new System.Drawing.Point(P.Location.X, P.Location.Y - 1);
System.Threading.Thread.Sleep(10);
}
}
If you want to have better control I would use a Timer. In this example the Interval is set to 10.
private void up_MouseDown(object sender, MouseEventArgs e)
{
timer1.Start();
}
private void up_MouseUp(object sender, MouseEventArgs e)
{
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (P.Location.Y > 0)
{
P.Location = new System.Drawing.Point(P.Location.X, P.Location.Y - 1);
}
}
You can use timer. Add a timer from the toolbox, say its name was timer1, then add following method:
private void P_MouseUp(object sender, MouseEventArgs e) {
timer1.Enabled=false;
}
private void P_MouseDown(object sender, MouseEventArgs e) {
timer1.Enabled=true;
}
private void timer1_Tick(object sender, EventArgs e) {
if(P.Location.Y>0)
P.Location=new System.Drawing.Point(P.Location.X, P.Location.Y-1);
}
You can change the interval of timer1 in properties window. I guess you write this for fun; so, have fun!
I want to display a ballon tip when an error occures instead of showing MessageBox.
[NOTE] i did not want it to be shown on mouse Hover.
I tried both but they actually show the tip on mouse hover
toolTip1.SetToolTip();
toolTip1.Show();
You can use the ToolTip Popup event to check if there is a Tooltip present and cancel it if there isn't. You can then set the tooltip during your validation then show it. In this example I set a timer to reset the tooltip text after a 2 second timeout.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
toolTip1.IsBalloon = true;
toolTip1.Popup += new PopupEventHandler(toolTip1_Popup);
toolTip1.SetToolTip(textBox1, "");
}
void toolTip1_Popup(object sender, PopupEventArgs e)
{
if (toolTip1.GetToolTip(e.AssociatedControl) == "")
e.Cancel = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
toolTip1.RemoveAll();
}
private void textBox1_Validating(object sender, CancelEventArgs e)
{
int temp;
if (!int.TryParse(textBox1.Text, out temp))
showTip("Validation Error", (Control)sender);
}
private void showTip(string message, Control destination)
{
toolTip1.Show(message, destination);
timer1.Start();
}
}
Much to my surprise it appears that toolTip1.IsOpen = true will show a tooltip and allow it to stay open. Note that you will need to provide code to close it because it wouldn't go away on its own on my machine no matter what I did.
Is there any way to make the form semi-transparent while it is being moved and then become opaque when it's not being moved anymore? I have tried the Form_Move event with no luck.
I'm stuck, any help?
The reason the form loads as semi-transparent is because the form has to be moved into the starting position, which triggers the Move event. You can overcome that by basing whether the opacity is set, on whether the form has fully loaded.
The ResizeEnd event fires after a form has finished moving, so something like this should work:
bool canMove = false;
private void Form1_Load(object sender, EventArgs e)
{
canMove = true;
}
private void Form1_Move(object sender, EventArgs e)
{
if (canMove)
{
this.Opacity = 0.5;
}
}
private void Form1_ResizeEnd(object sender, EventArgs e)
{
this.Opacity = 1;
}
To do it properly I expect you'd need to override the message processing to respond to the title bar being held, etc. But you could cheat, and just use a timer so that you make it opaque for a little while when moved, so continuous movement works:
[STAThread]
static void Main()
{
using (Form form = new Form())
using (Timer tmr = new Timer())
{
tmr.Interval = 500;
bool first = true;
tmr.Tick += delegate
{
tmr.Stop();
form.Opacity = 1;
};
form.Move += delegate
{
if (first) { first = false; return; }
tmr.Stop();
tmr.Start();
form.Opacity = 0.3;
};
Application.Run(form);
}
}
Obviously you could tweak this to fade in/out, etc - this is just to show the overall concept.