Chaning the image according to what button is clicked - c#

I have this interface and I want the middle image to change according to which button is clicked. What would I need in order to do this?
http://i62.tinypic.com/kbx9z.png

I assume that you have a list of images. Then you can assign a button's Tag property to index of an image. Then assign each button's click event to a sinle event such as
private void buttonClicked(object sender, EventArgs e)
{
this.pictureBox1.Image = this.imageList[(int)((Button)sender).Tag];
}

Related

Dragging over the buttons to show a word in windows form C#

I have a windows form that includes a text box and 3 buttons
The text property of the first button is C.
The text property of the second button is A
Text property of the third button is R.
Is there a way to show a car word in the text box by dragging the mouse over the previous buttons?
I tried to write the code in the MouseMove event & DragEnter event but I did not get anything.
I need to click on the first button "C" then keep pressing and dragging over the other buttons to create the word "CAR" in the textBox
You can do this using Event MouseHover
private void BtnC_MouseHover(object sender, EventArgs e)
{
TxtCar.Text += BtnC.Text;
}
private void BtnA_MouseHover(object sender, EventArgs e)
{
TxtCar.Text += BtnA.Text;
}
private void BtnR_MouseHover(object sender, EventArgs e)
{
TxtCar.Text += BtnR.Text;
}

Button mouse over ForeColour change

To create a mouse over button I use this code
private void btnCreateAccount_MouseHover(object sender, EventArgs e)
{
btnCreateAccount.ForeColor = Color.Gold;
}
private void btnCreateAccount_MouseLeave(object sender, EventArgs e)
{
btnCreateAccount.ForeColor = Color.Black;
}
The mouse over button works however when I hover over the button there is a good at least 1 second delay. I would think that it should change colour as soon as the mouse is placed over the button and not with a (in my opinion) too long delay.
Is there any way of fixing that code by like refreshing the button or something along those lines? or perhaps someone has a code that works perfectly?
You are handling the Mouse Hover event. This will require the cursor to be still for a short while in order to fire.
The pause required for this event to be raised is specified in milliseconds by the MouseHoverTime property.
This is read only.
Normally if you want the colour to change immediately you should handle the Mouse Enter event:
private void btnCreateAccount_MouseEnter(object sender, EventArgs e)
{
btnCreateAccount.ForeColor = Color.Gold;
}

Hover over: Button Change using Flatyle properties C#

Im working in WinForms I have 4 buttons on my form. I want to be able to hover my mouse over it and change the FlatStyle from Flat to System.
My code transforms all the buttons to System Style when you hover your mouse over it, that's not exactly what i had in mind.
All the buttons should remain flat until you hover over them. If you hover off the button it should turn back into flat button
private void All_Button_Hover_MouseHover(object sender, EventArgs e)
{
btn_Back.FlatStyle = FlatStyle.System;
Btn_Forward.FlatStyle = FlatStyle.System;
btn_Print.FlatStyle = FlatStyle.System;
btn_Open.FlatStyle = FlatStyle.System;
}
Here's a suggestion of how you could handle this.
You're already setting all the buttons in a single event method, which is fine. Since the button that triggered the event is stored in sender, you could just use that:
private void All_Button_Hover_MouseHover(object sender, EventArgs e)
{
((Button)sender).FlatStyle = FlatStyle.System;
}
To change the buttons back to the original FlatStyle.Flat style, you'll probably want to subscribe all of their MouseLeave events to a method as well:
private void All_Button_Hover_MouseLeave(object sender, EventArgs e)
{
((Button)sender).FlatStyle = FlatStyle.Flat;
}

Hide image when click outside of image in wpf

When my WPF application loads, an image is shown in center.
how can i handle mouse click outside of image. when user clicks outside of image it hide.
My code is in c#.
You can simply add a handler to the top level control, eg. Grid, Window, etc. In that handler, you can check whether the control that was clicked on was the Image and if it wasn't, then you could hide it:
The XAML:
<Grid PreviewMouseLeftButtonDown="Grid_PreviewMouseLeftButtonDown">
...
<Image Name="TheImage" Source="/WpfApplication2;component/Images/Add_16.png" />
...
</Grid>
The code behind:
private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.OriginalSource != TheImage)
{
TheImage.Visibility = Visibility.Hidden;
}
}
While this fulfils your requirements, it should be noted that once hidden, the Image will no longer be 'clickable'.
just put the code MyImage.Visibility = System.Windows.Visibility.Hidden; in whichever event you want to capture.
for eg:
private void MyButton_Click_1(object sender, RoutedEventArgs e)
{
MyImage.Visibility = System.Windows.Visibility.Hidden;
}
private void MyTextbox_PreviewMouseDown_1(object sender, MouseButtonEventArgs e)
{
MyImage.Visibility = System.Windows.Visibility.Hidden;
}
private void MyWindow_MouseDown(object sender, MouseButtonEventArgs e)
{
MyImage.Visibility = System.Windows.Visibility.Hidden;
}
where MyImage is the name of image, MyTextbox is the name of textbox, MyButton is the name of Button and MyWindow is the name of Main Window
you can simply write
YourImageName.Visibility = Visibility.Hidden;
Write this code to any of the control i.e. TextBox or Button.
first click on the form to select it. then go to the event's section (near property section) and double-click the click event to make a function call when a form is clicked. in the code compare the sender object with the IsEqual(obj) method and see the sender is same image or it's not. if not hide it. :)
You can handle the click event of Application current window , and inside that event you can check if the mouse position is within that image or outside of that image , on basis of this you can set your image visibility condition you prefer .
Code sample example :
//registering event
Application.Curent.mainWindow.MouseRightButtonDown += MainWindow_MouseRightButtonDown;
//event implementation
void MouseRightButtonDown(object sender , MouseButtonEventArgs e)
{
//here you can check the ui element for image control using sender
//below will let you know the position of Click
e.GetPosition(// pass the ui element here)
}
Note : The above code shown is at app level click handling . If you
dont want at app level you can take the parent xaml within which the
image is present and do the same

Share an event handler across multiple controls

In my Windows forms application written in C# I have a bunch of buttons. When the user's mouse hovers over a button, I want the button's border to change.
Currently I have multiple instances of the following (a copy for each button):
private void btnStopServer_MouseEnter(object sender, EventArgs e)
{
oldColor = btnStopServer.FlatAppearance.BorderColor;
btnStopServer.FlatAppearance.BorderColor = mouseOverColor;
}
private void btnStopServer_MouseLeave(object sender, EventArgs e)
{
btnStopServer.FlatAppearance.BorderColor = oldColor;
}
Since I have a lot of buttons, the code to change the color of the button's border takes up a lot of space.
Is there any simpler way that I could do this?
You should wire-up a single MouseEnter and MouseLeave to each control that needs this functionality (rather than writing a new version of each method for each control). Assuming you're using Visual Studio, this can be done by changing the target method name for the event, in each Button's property pane. If you write the following code first, then this method will appear in the property's MouseEnter and MouseLeave events' drop-down lists.
The code would then need to check which button from which the event was fired, as follows:
private void btnWithHoverBorder_MouseEnter(object sender, EventArgs e)
{
Button eventButton = (Button) sender;
oldColor = eventButton.FlatAppearance.BorderColor;
eventButton.FlatAppearance.BorderColor = mouseOverColor;
}
private void btnWithHoverBorder_MouseLeave(object sender, EventArgs e)
{
Button eventButton = (Button) sender;
eventButton.FlatAppearance.BorderColor = oldColor;
}
I presume oldColor is a global? This might get out of sync if something "odd" happens where your MouseEnter event is fired for another button, before the corresponding MouseLeave is caught. To make this more robust, I'd consider storing the old color on the Button's .tag property, so that it's self-contained.
Eg:
private void btnWithHoverBorder_MouseEnter(object sender, EventArgs e)
{
Button eventButton = (Button) sender;
eventButton.tag = eventButton.FlatAppearance.BorderColor;
eventButton.FlatAppearance.BorderColor = mouseOverColor;
}
private void btnWithHoverBorder_MouseLeave(object sender, EventArgs e)
{
Button eventButton = (Button) sender;
eventButton.FlatAppearance.BorderColor = (Color)eventButton.tag;
}
(The tag is basically a hook on which to tag "anything" relevant to a specific instance of a control, that there is not already a property for. It's of type Object which means you can tag anything there, but when you read from it, you need to cast it back to whatever type you put there in the first place. But because it's an Object you can put anything there, including eg a custom class that contains multiple properties, or an array, etc if you need to tag a control with more than one thing).

Categories

Resources