I am using C# programming language.
I have created a new borderless Windows Form in Visual Studio 2012. I have two panels. First panel (panel1) is docked at top and second panel (panel2) is docked at left and its Visible status is false. I also added a button (buttonMenu) in panel1 and docked it on left. What I am trying to achieve is that on buttonMenu click, panel2 slides from left to its original location (left dock) and when buttonMenu clicked again slides away.
I have tried to set the following code:
private void buttonMenu_Click(object sender, EventArgs e)
{
panel2.Visible = !panel2.Visible;
}
And it works but i am trying to make it slide with an animation.
The easier way to do it would be to create a timer and in its Tick event change the panel width like so
private Timer timer = new Timer();
bool closing = false;
int desiredWidth = 300
private void ConfigureTimer()
{
timer.Tick += timer_Tick;
timer.Interval = 16;
}
private void timer_Tick(object sender, EventArgs e){
{
if(closing)
{
panel2.Width-=15;
if(panel2.Width < 0)
{
panel2.Width = 0;
timer.Stop();
}
}else{
panel2.Width+=15;
if(panel2.Width >= desiredWidth)
{
panel2.Width = desiredWidth;
timer.Stop();
}
}
}
private void SwitchPanelState()
{
closing = !closing;
timer.Start();
}
But if you want to make you life easy and more object oriented, you should create an Control with the specified behavior and just add it in your form
Related
I'm trying to understand what is executing before the MouseWheel event.
What I've done:
I have a form which has AutoScroll property set to true. There is a control (ZEDGRAPH) at the top and the bottom of this form.
To overcome the issue of scrolling and zooming at the same time I captured the mousewheel += new MouseEvenHandler(mymethod) for the form.Then using a bool variable I keep track of when the control (ZEDGRAPH) has focus and when it does not.
When it has focus I make verticalscroll.value = (int)mydesiredposition;
This works in accomplishing what I wanted which is to ignore the mousewheel event in the form and focus on the control.
What I am struggling with is the fact that when I scroll the form flickers every time and scrolls down before coming to the set scrollbar value.
So what I am wondering is what is getting triggered before this mouseeventhandler that causes it to flicker and is there a relatively simple workaround this?
My code snapshot:
public Form(Form1 f)
{
InitializeComponent();
this.MouseWheel += new MouseEventHandler(mousewheel);
}//end of constructor
//
//
bool mousehoverZedGraph1 = false;
bool mousehoverZedGraph2 = false;
//
//
private void zedGraphControl1_MouseHover(object sender, EventArgs e)
{
mousehoverZedGraph1 = true;
return;
}
private void mousewheel(object sender, MouseEventArgs e)
{
if (mousehoverZedGraph1 == true)
{
VerticalScroll.Enabled = false;
VerticalScroll.Value = 0;
return;
}
else if (mousehoverZedGraph2 == true)
{
VerticalScroll.Value = 429;
VerticalScroll.Enabled = false;
}
else
{
//VerticalScroll.Value += e.Delta;
}
}
private void Form_MouseEnter(object sender, EventArgs e)
{
mousehoverZedGraph1 = mousehoverZedGraph2 = false;
VerticalScroll.Enabled = true;
}
A small video highlighting the flicker:
I'm currently working on a project in UWP and I have a CommandBar that I want to go from Hidden to Compact if the mouse moves. After five seconds (If the mouse dont move) the CommandBar should go back to Hidden again.
I dont get any errors, but when I move the mouse the CommandBar is going crazy and it's just flashing from Hidden to Compact when I move the mouse again. I think the problem is that the OnMouseMovement event is stacking upon itself.
This is my code for the mouse movement event:
public async void OnPointerMoved(object Sender, PointerRoutedEventArgs e)
{
CmdBar.ClosedDisplayMode = AppBarClosedDisplayMode.Compact;
DispatcherTimer ButtonTimer = new DispatcherTimer();
ButtonTimer.Interval = TimeSpan.FromSeconds(5);
ButtonTimer.Tick += (sender, args) =>
{
CmdBar.ClosedDisplayMode = AppBarClosedDisplayMode.Hidden;
};
ButtonTimer.Start();
}
I made a little test project to try it out and get you an answer, this is what I did :
private DispatcherTimer Timer { get; set; }
public MainPage()
{
this.InitializeComponent();
CmdBar.ClosedDisplayMode = AppBarClosedDisplayMode.Hidden;
Timer = new DispatcherTimer(){Interval = TimeSpan.FromSeconds(5) };
Timer.Tick += (sender, args) => {
CmdBar.ClosedDisplayMode = AppBarClosedDisplayMode.Hidden;
Timer.Stop();
};
}
public async void OnPointerMoved(object Sender, PointerRoutedEventArgs e)
{
Timer.Stop();
CmdBar.ClosedDisplayMode = AppBarClosedDisplayMode.Compact;
Timer.Start();
}
Basically as #Evk said, you are creating a new timer every move of your mouse. So I declared a property for the timer and stop it then restart it when your mouse move.
Is there any way to capture the time interval between mouse wheel scroll start and stop? Actually I want to capture the interval between the scrolling start and stop when I very quickly scroll the mouse wheel.
I have already looked at MouseWheel event but it don't fulfill my requirement. In senes that it always gives a value of Delta 120 or -120 but i want to call a function depending on the speed of the mouse scroll for example when i scroll the mouse normally i want to perform function 1 and when i scrolled the mouse very quickly i want to perform the function 2. In other words is there any way to distinguish between the mouse scroll high and normal speed.
Any advice will be appreciated.
can't you capture the mouse wheel events and see how long between them. Basically start a timer when you get a mouse wheel event and then in the next event see what the timer is at (and so how long has elapsed between the events) to determine the speed the wheel is being turned at? If the elapsedtime is smaller than a certain threshold, perform function2 and if it is faster than a certain threshold perform function 1.
You will probably have to set it to perform function 1 if the timer goes off in case they only do a single scroll.
In fact you might be able to do it this way:
start a timer (with an interval that indicates slow mouse wheeling) in the mouse wheel event, then if the timer goes off perform function 1. If the mouse wheel event happens again before the timer has gone off then reset the timer and increment a counter (to keep track of the number in wheel events since you did stuff) then start a second (longer) timer. if the counter is greater then a certain threshold perform function 2. When the second timer elapses, reset the counter. Something along those lines should give you the ability to fire function 1 when slow wheel turning and function 2 when the wheel is rapidly turned through a few 'clicks'.
this code should give a (very dirty) indication of the sort of thing I was thinking of. After playing a little I'm not really sure it's a good solution though....
private void mouseWheelHandler (object sender, MouseEventArgs e)
{
slowTimer.Enabled = false;
slowTimer.Stop ();
slowTimer.Interval = 200;
slowTimer.Start();
slowTimer.Enabled = true;
m_counter++;
Trace.WriteLine(string.Format("counter={0}", m_counter));
if (fastTimer.Enabled==false)
{
fastTimer.Enabled = true;
fastTimer.Interval = 150;
fastTimer.Start ();
}
if (m_counter>5)
{
Trace.WriteLine("called method 2");
m_counter = 0;
fastTimer.Stop ();
slowTimer.Enabled = false;
slowCheckTimer.Stop ();
slowCheckTimer.Interval = 250;
slowCheckTimer.Start();
slowCheckTimer.Enabled = true;
}
}
private void slowTimer_Tick(object sender, EventArgs e)
{
Trace.WriteLine("slow timer ticked");
if (slowCheckTimer.Enabled==false)
{
Trace.WriteLine ("called method 1");
}
slowTimer.Enabled = false;
}
private void fastTimer_Tick(object sender, EventArgs e)
{
fastTimer.Enabled = false;
Trace.WriteLine("fast timer ticked");
m_counter = 0;
fastTimer.Stop ();
}
private void slowCheckTimer_Tick(object sender, EventArgs e)
{
Trace.WriteLine("slow check timer ticked");
slowCheckTimer.Stop ();
slowCheckTimer.Enabled = false;
}
Take a look at the Control.MouseWheel event.
As suggested by the Sam Holder i am posting here a modified verion of his advice to help other programmers facing the same problem.
public partial class Form1 : Form
{
int m_counter = 0;
public Form1()
{
InitializeComponent();
// Attach Mouse Wheel Event
this.MouseWheel += new MouseEventHandler(Form1_MouseWheel);
}
void Form1_MouseWheel(object sender, MouseEventArgs e)
{
// Refresh Slow Timer
slowTimer.Enabled = false;
slowTimer.Stop();
slowTimer.Interval = 150;
slowTimer.Start();
slowTimer.Enabled = true;
// Incremenet counter
m_counter++;
// Start Fast Timer
if (fastTimer.Enabled == false)
{
fastTimer.Enabled = true;
fastTimer.Interval = 50;
fastTimer.Start();
}
// If this returns true call
// the fast scroll implementation
if (m_counter > 4)
{
Console.WriteLine("Quick Method Called");
m_counter = 0;
fastTimer.Stop();
slowTimer.Enabled = false;
slowCheckTimer.Stop();
slowCheckTimer.Interval = 200;
slowCheckTimer.Start();
slowCheckTimer.Enabled = true;
}
}
private void slowTimer_Tick(object sender, EventArgs e)
{
if (slowCheckTimer.Enabled == false)
{
Console.WriteLine("Slow Method Called");
}
slowTimer.Enabled = false;
}
private void fastTimer_Tick(object sender, EventArgs e)
{
fastTimer.Enabled = false;
m_counter = 0;
fastTimer.Stop();
}
private void slowCheckTimer_Tick(object sender, EventArgs e)
{
slowCheckTimer.Stop();
slowCheckTimer.Enabled = false;
}
}
I Want Use a Panel in a Windows Form in C#.net. I Set Visible Property of this Control to false And When I Click on a Button, Panel is showed. I Want Show a Panel by some Effect.
Please Help me for this
You are leaving us guessing about what kind of effect you are looking for. I'll just arbitrarily pick a collapse and expand effect. It takes a Timer, you implement the effect in a Tick event handler. Here's an example, it requires a Panel, Timer and Button:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
timer1.Interval = 16;
timer1.Tick += new EventHandler(timer1_Tick);
panel1.BackColor = Color.Aqua;
mWidth = panel1.Width;
}
int mDir = 0;
int mWidth;
void timer1_Tick(object sender, EventArgs e) {
int width = panel1.Width + mDir;
if (width >= mWidth) {
width = mWidth;
timer1.Enabled = false;
}
else if (width < Math.Abs(mDir)) {
width = 0;
timer1.Enabled = false;
panel1.Visible = false;
}
panel1.Width = width;
}
private void button1_Click(object sender, EventArgs e) {
mDir = panel1.Visible ? -5 : 5;
panel1.Visible = true;
timer1.Enabled = true;
}
}
The only effect I can think of is to expand the panel by using a timer and change the size of the panel step-by-step.
I would recommend you to use WPF instead of Winforms that is very good at doing this kind of stuff. You can animate all properties of the control like location, size, alpha. Please, check these articles on WPF animation
WPF Animation overview
Walkthroughs: Create a Custom Animated Button
I'm trying to display different tool tip text depending on what part of a control the user has their mouse over. As a simplified example, this will display "Left" if the user hovers over the left half, and "Right" if the user hovers over the right half:
public partial class TestForm : Form
{
private ToolTip toolTip = new ToolTip();
public TestForm() {
InitializeComponent();
}
private void TestForm_MouseMove(object sender, MouseEventArgs e) {
if (e.X < Width / 2) {
toolTip.SetToolTip(this, "Left");
} else {
toolTip.SetToolTip(this, "Right");
}
}
}
The problem is that this causes the tool tip to display immediately and also follow the cursor around. Is there any way to make it behave like the different regions are separate controls?
You can add timer that will trigger tooltip on delay when mouse is not moved.
Add timer with some interval, say, 1000 ms (1 sec).
public TestForm()
{
InitializeComponent();
timer1.Enabled = true;
}
While mouse moves it should run
private void TestForm_MouseMove(object sender, MouseEventArgs e) {
//if (e.X < Width / 2) {
// toolTip.SetToolTip(this, "Left");
//} else {
// toolTip.SetToolTip(this, "Right");
//}
timer1.Stop();
timer1.Start();
}
And if stopped, trigger a tooltip
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
if (PointToClient(Cursor.Position).X < this.Width / 2)
{
toolTip.SetToolTip(this, "Left");
}
else
{
toolTip.SetToolTip(this, "Right");
}
}