In my current project, I am trying to make a button change its image when a particular key is pressed. So far, my code looks something like
if (e.KeyCode == Keys.H)
{
button1.Image = bitmap.FromFile(C: filename\filename\filename\filename);
}
I don't know if I'm calling the file correctly, or using the right method or class. I'm still fairly new to this, so a simple explanation is probably best, thanks.
If you include the images to your resources you can do it like this:
public Form1()
{
InitializeComponent();
button1.MouseEnter += new EventHandler(button1_MouseEnter);
button1.MouseLeave += new EventHandler(button1_MouseLeave);
}
void button1_MouseLeave(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img1));
}
void button1_MouseEnter(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img2));
}
I would not recommend hardcoding image paths.
For reference i use this link... maybe some other answer help you as well.
Reference: LINK
Related
I know, i know there are lots and lots of questions asking on here about this error, each with their own response, but its easier to work off a response regarding your own code rather than someone else's
I have been working on this program for some time for a college assignment, and as soon as i started putting in the class to calculate the totals of things it now crashes,
I don't know where to look so i'll post my main code
enter code here
namespace Till
public partial class MainWindow : Window
{
Calculator calc = new Calculator();
public MainWindow()
{
InitializeComponent();
}
public bool User;
public bool tillopen = false;
private void button_Click(object sender, RoutedEventArgs e)
{
//button clone thingy
Button btn = (Button)sender;
label.Content = label.Content + btn.Content.ToString();
Console.Beep(); // makes the buttons beep
}
private void clear_Click(object sender, RoutedEventArgs e)
{
// Clear
label.Content = "";
}
private void Button_Submit_Click(object sender, RoutedEventArgs e)
{
// submit
listView.Items.Add(label.Content);
label.Content = "";
calc.setSoldItems(Convert.ToInt32(label.Content)); /// it breaks on this line///
}
private void button13_Click(object sender, RoutedEventArgs e)
{
//void sale
label.Content = "";
listView.Items.Clear();
}
private void button15_Click(object sender, RoutedEventArgs e)
{
//pound
label.Content = "1.00";
}
private void button12_Click(object sender, RoutedEventArgs e)
{
//till open close
tillopen = true;
}
private void button16_Click(object sender, RoutedEventArgs e)
{
Login m = new Login();
m.Show();
this.Close();
}
private void button14_Click(object sender, RoutedEventArgs e)
{
label.Content = "2.00"; // 2 pound
}
private void button17_Click(object sender, RoutedEventArgs e)
{
label.Content = calc.finish();
}
}
I have tried to re-create the error in another WPF (converting a to an int32) and it works fine, i know this is an issue with my code itself, i have tried using other machine and using different versions of visual studio itself, so we came to the assumption its this code itself and not a broken dll file
So before I sit down with my Teacher and spend all day going though my code step by step im asking around for help in order to save both of our time, This assignment is due in in 3 weeks, and it decides to break on me now.
thankies
To replicate this error, i press a number button on my Windows form, them hit the submit button i created (which starts the conversion) If a copy of my class which handles all of this is needed im happy to post it
In the method button_click, you have assigned value as
label.Content = label.Content + btn.Content.ToString();
which is a string value to the label and the values are concatenated instead of add.
and when you are reading it, you are converting it in Int32. which will give exception as it will not contain any integer value to it.
You can add the value like this:
label.Content = (Convert.ToInt32(label.Content) + Convert.ToInt32(btn.Content)).ToString();
and check before converting if the label has blank values in it, if it has do not convert them, and only convert the value if it has some values it will not give any error. Also do not assign any values other that numerical digits.
You are calculating:
Convert.ToInt32(label.Content)
but on the line before you set:
label.Content = "";
so this means you are calculating
Convert.ToInt32("")
which gives you a FormatException.
Perhaps you should use the value of label.Content before you overwrite it?
I have 15 images on my WPF application. I want it so that whenever MouseUp on any of the images is called.. it'll call the same method.
I would like to do something similar to the psuedo code written here.. This would save so much time instead of writing 15 individual methods for each button. How can I do something like this?
private void BluePick1_Image_MouseUp_1(object sender, MouseButtonEventArgs e)
{
sender.ImageSource = something;
}
thank you for any help
if your event is always on a button :
private void ButtonMouseUp(object sender, MouseButtonEventArgs e) {
((Button)sender).ImageSource = something;
}
and
button1.MouseUp += ButtonMouseUp;
button2.MouseUp += ButtonMouseUp;
I have a richtextbox and i want to delete not cut the selected when the user presses a button.
I have used
private void button1_Click(object sender, EventArgs e)
{
SendKeys.Send("DELETE");
}
This works but i want to know another way to do it.
I have tried
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.SelectedText.Replace(richTextBox1.SelectedText, "");
}
This doesn't perform any action.
Pls what can i do?
Just do this:
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.SelectedText = "";
}
Your code doesn't work because the string is immutable, you can't change the richTextBox1.SelectedText that way. All the methods (Replace, Insert, ...) performed on a string will create a new string. This new string will be used to initialize your string variable if you need.
The following line of code works for me:
SendKeys.Send("{DELETE}");
Click Link to visit the Official documentation on SendKeys methods.
I wrote .net code and want anyone to help me and tell if this is right or not :). I made a form with two checkboxes and two picture boxes and a button and want to save values of the checkbox and the picture box when I close the form and reload these values again after rerun.
The code I wrote:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//this.BackColor = Properties.Settings.Default.UserBackColor;
Properties.Settings.Default.Reload();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
pictureBox1.Image = Image.FromFile("C:\\red.jpg");
Properties.Settings.Default.Upgrade();
}
}
private void button1_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Save();
Application.Exit();
}
The use of Upgrade() is incorrect here. It is used to update settings after an application upgrade.
Remove that line and everything should work fine and dandy.
There's a nice short article on CodeProject that explains the use of application settings: http://www.codeproject.com/Articles/17659/How-To-Use-the-Settings-Class-in-C
I'm trying to define MouseEventHandlers such that the application will exit whenever the mouse is clicked or moved or whenever a key is pressed. This is my first time using C#, but based on what I found online, I've written the code as follows:
MouseDown += new MouseEventHandler(mouseClickedResponse);
MouseMove += new MouseEventHandler(mouseMovedResponse);
KeyDown += new KeyEventHandler(keyResponse);
which connects to:
private void keyResponse(object sender, EventArgs e)
{
Application.Exit();
}
private void mouseClickedResponse(object sender, EventArgs e)
{
Application.Exit();
}
private void mouseMovedResponse(object sender, EventArgs e)
{
if (firstCall) //Keeps the application from exiting immediately
firstCall = false;
else Application.Exit();
}
The problem that I'm finding is that while the KeyEventHandler works perfectly, I can move and click the mouse as much as I want to no avail.
This is the sum total of the code that I've written to allow for user control; am I missing something?
On the surface, everything looks good with your code.
One possibility - The MouseEventHandler is defined in both the System.Windows.Input (MSDN) namespace as well as the System.Windows.Forms namespace (MSDN).
I believe the one you want is the one in the Forms namespace. Is it possible that you're using the one from the Input namespace instead?
I fixed my problem--my Form was filled with Panels, and by moving the code for mouse input over to the panels, everything worked instantly.
Change:
private void mouseClickedResponse(object sender, EventArgs e)
to:
private void mouseClickedResponse(object sender, MouseEventArgs e)
It should now work fine.