I am making a simple program that changes the image of a button when the user interact with it. When the computer mouse gets inside the button the image highlights, when the mouse clicks it gets darker and when the mouse leaves the button, the image gets norma. To accomplish this behavior I am changing the button image between 3 images, BUT when I click the button and it displays a modal control, e.g. a MessageBox, when it goes back from the MessageBox, the image in the button is still the "mouse inside" image, but the mouse is out of the button already...so I think the button1_MouseLeave event is not rising, but I dont understand why and how to repair it...can you guys help me out on this one?
The code I am using is simple:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace teste1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Normal));
}
private void button1_MouseEnter(object sender, EventArgs e)
{
button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Claro));
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Test");
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Clique));
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Claro));
}
}
}
The mouse sure doesn't leave, because the focus on the button is stolen before that, so it doesn't receive those messages. You should also listen to the LostFocus event and switch to normal image when the button loses focus.
private void button1_LostFocus(object sender, EventArgs e)
{
button1.Image = (System.Drawing.Image)Properties.Resources.Botao_Del_Normal;
}
Make sure to actually listen to the LostFocus event! Just adding the code won't do a thing.
Note that since the signatures of expected LostFocus and MouseLeave delegates are the same, you could actually listen to both events with the same method (and call it something like button1_MouseLeaveOrLostFocus or anything that suits you)
Please try button1_MouseHover event instead of button1_MouseEnter Event.
I will work.
This is to be expected, as according to your code the image gets reset when you stop clicking, which will be triggered by the window losing focus due to the messageBox.
Delete the MouseUp handler to achieve the behaviour you desire
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button1.Text = "Hello World!";
}
private void button1_MouseHover(object sender, EventArgs e)
{
button1.Text = "Mouse Hover";
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
button1.Text = "Mouse Down";
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
button1.Text = "Mouse Up";
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.Text = "Mouse Leave";
}
}
}
SOLVED:
Ok guys, I figured how to make it work! I had to remove the MouseClick event, and added all the processing to the MouseUp event, so now MouseLeave is called after MouseUp as it should (even with ALT+TAB).
The code now is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace teste1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_MouseEnter(object sender, EventArgs e)
{
button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Claro));
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Clique));
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Normal));
}
private void button1_Click(object sender, EventArgs e)
{
button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Normal));
// Processing is made here!
}
}
}
Related
I have made a browser. But when I press the maximize button, the components of the form is at its default size. How can I fix it?
here is the code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Devops
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
webBrowser1.GoBack();
}
private void button5_Click(object sender, EventArgs e)
{
webBrowser1.GoForward();
}
private void button6_Click(object sender, EventArgs e)
{
webBrowser1.Refresh();
}
private void button4_Click(object sender, EventArgs e)
{
webBrowser1.GoSearch();
}
private void button2_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void button7_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Maximized;
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
also, here are some pics
1.App in its normal state:Image
2.App in its max state:Image
You need to adjust the Anchor properties of most of those controls to adjust and resize their own properties
On the Properties window, change the WebBrowser Anchor to Top, Bottom, Left, Right:
For the buttons on your top left corner of the form, change its Anchor to Top, Right:
And to adjust the textbox length when the form is resize, change the Anchor of the text to Top, Left, Right:
I have a dotnet 2.0 app in C# that I have developed in Visual Studio 2008, it's pretty simple:
I would like to change the label name currently called "label1" to "Running" or "Stopped" when my process loop.exe is running or stopped.
When I press start, it will run loop.exe and the stop button will obviously stop it.
I've read a lot of topic on C# Winforms but I cannot get this working and I have no idea what do to now. I think that I need to add a backgroundworker but I don't know how to check the process and update the label programmatically.
Here's my clean/current code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace APP_NAME
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Start_Click(object sender, EventArgs e)
{
Process.Start("loop.exe");
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Process.Start("taskkill", "/F /IM loop.exe");
Process.Start("taskkill", "/F /IM azcopy.exe");
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button3_Click(object sender, EventArgs e)
{
Process.Start("notepad.exe", #"C:\ProgramData\APP_NAME\settings\settings.xml");
}
private void button4_Click(object sender, EventArgs e)
{
Process.Start("explorer.exe", #"C:\ProgramData\APP_NAME\logs");
}
}
}
I hope I made myself clear, thank you.
You can update the text shown by a label by accessing it's Text property. So, change your method like:
private void Start_Click(object sender, EventArgs e)
{
Process.Start("loop.exe");
label1.Text = "Running";
}
//hi
//Do this to see the label value changes in real time in the window
private void Start_Click(object sender, EventArgs e)
{
Process.Start("loop.exe");
label1.Text = "Running";
lable1.Refresh();
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Apostolh_Texnikou
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void GN_TextChanged(object sender, EventArgs e)
{
}
private void CP_TextChanged(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
I want the user to click in the ready button and a window to pop out.
In the window the user must be able to copy the text.
I found how to make a pop window but its just a window which doesn't allow
copy paste. thank you in advance
You might want to look into a more complex type of dialog box. Perhaps one with a textbox? This will require launching another form. (see how to display textBox control in MessageBox?)
basically I mean I want a picture to move when I press a button and the picture to sop moving at a certain point on the form application, not cancel it the picture to move then just stop with the application still running. on Microsoft visual studios c# windows form application
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Left += 2;
}
}
}
You can call timer1.Stop() to stop the timer.
You need to set the Interval property of the timer to raise the TickEvent once after that Interval is Elapsed.
In TickEvent you can Stop the Timer or do whatever you want to do.
Try This :
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval=5000;//to raise tick event for 5 sec's
timer1.Start();
timer1.Tick += new System.EventHandler(this.timer1_Tick);
}
protected void timer1_Tick(Object sender,EventArgs e)
{
//Stop the timer here
timer1.Stop();
}
This is for a project I'm doing for class, I'm trying to create a win form that will have 2 buttons one that will increment in the text box when the button is pushed and one that will decrement when a different button is pushed. I'm having trouble finding the right line that will do what I want. Is there someone that could help me out?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Project10TC
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Teancum Project 10");
}
private void button1_Click(object sender, EventArgs e)
{
int i = 1;
textBox1.Text = Convert.ToString(i++);
}
private void button2_Click(object sender, EventArgs e)
{
int i = 1;
textBox1.Text = Convert.ToString(i--);
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Clear();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Since its a class project, I can only give you a hint.
You need to define the variable i outside of your button click events. Use the same variable in both events.
Also look at the difference between i++ and ++i
Declare the i variable as a field. Besides I would use ++i instead of i++. Otherwise you have different values in the textbox and in the variable. Also, no need to use Convert.ToString().
public partial class Form1 : Form
{
int i;
public Form1()
{
InitializeComponent();
i = 0;
}
//...
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = (++i).ToString();
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = (--i).ToString;
}
}