Here's my code:
public void Repaint() {
canvas1.Children.Clear();
ObservableCollection<ClassBox> classes = viewModel.Classes;
foreach (ClassBox j in classes) {
// connect our ui element to the mouse handler if not done already
if (!j.MouseActive) {
j.Box.PreviewMouseLeftButtonUp += Handle_MouseUp;
j.Box.PreviewMouseLeftButtonDown += Handle_MouseDown;
j.Box.PreviewMouseMove += Handle_MouseMove;
j.MouseActive = true;
// Heres should the code be written (something like :)
// j.Box.SetColor(Change_Color(value));
}
canvas1.Children.Add(j.Box);
} }
So the above code presents how the boxes are being repainted when im calling the method.
Works fine but now for the next part of the code:
private void Change_ColourBox(object sender, System.Windows.RoutedEventArgs e)
{
var element = sender as FrameworkElement;
ObservableCollection<ClassBox> classes = viewModel.Classes;
foreach (ClassBox j in classes)
{
// connect our ui element to the mouse handler if not done already
if (sender == BoxStandard)
{
j.Box.Background = new SolidColorBrush(Colors.White);
j.Box.Foreground = new SolidColorBrush(Colors.Black);
Repaint();
} }
This code tell that we should change color when the button "Change_Color" is pressed.
I have implemented the buttons in XAML and I have given them names, in this case "BoxStandard".
The next code snipp is where i spawn the box via an event handler.
private void Spawn_Box(object sender, System.Windows.RoutedEventArgs e) {
viewModel.GetURController.AddAndExecute(new AddClassCommand(viewModel));
}
Im gonna line it up: I give predefined colors to my elements which they start out with. When I change the colors via the click event "Change_Color" they change to the desired color. BUT when I then spawn a new element to the canvas its in the predefined color. I want to spawn it with the changed color.
Ask if you don't understand question I can explain deeper!
The ClassBox is an object which consists of properties for the boxes. The repaint is not only for foreground/background color its also calculating the difference in positioning of the boxes
Related
I need to change the button background color by itself, that means when I will click on a button then it should change the background color, but when I will click again on the same button then I need to back default button color
I have tried on this event method
private void slot_1(object sender, RoutedEventArgs e)
{
//SlotLogic();
slot1.Background = Brushes.Green;
}
private void slot_2(object sender, RoutedEventArgs e)
{
//SlotLogic();
slot2.Background = Brushes.Green;
}
1 - You specified Windows Application. That's kind of broad. I believe you mean Windows Forms?
2 - You're also not checking whatsoever the current state of the button, so I'm not sure how do you think that clicking on the same button twice would have a different result, since you are executing the exact same come twice slot1.Background = Brushes.Green;.
Windows Forms
private bool isSlot_1Clicked = false;
private void slot_Click(object sender, EventArgs e)
{
if (isSlot_1Clicked)
slot.BackColor = Color.Green;
else
slot.BackColor = Color.Red;
isSlot_1Clicked = !isSlot_1Clicked;
}
If you want to have a toggle logic, you need to check the current state to inverse it. There is a lot of ways to do it, I choose to store it on a separate variable isSlot_1Clicked for sake of simplicity.
You could also check the slot's button background color, or check a complex object's property, or an array, and so on.
The first lines of code just checks the current state of the variable, and changes the button's background accordingly. The last line just set the variable isSlot_1Clicked to the inverse of it, executing the essence of the toggle logic.
WPF Version
private Brush slot_1DefaultBackground = null;
private void Slot1_Click(object sender, RoutedEventArgs e)
{
// store the default background value
if (slot_1DefaultBackground == null)
slot_1DefaultBackground = slot1.Background;
// check the current background, and toggle accordingly
if (slot1.Background != slot_1DefaultBackground)
slot1.Background = slot_1DefaultBackground;
else
slot1.Background = Brushes.Green;
}
This version stores the default button's background, and checks the current background against it to execute the toggle logic.
I think that must be only a little problem, but I can't get a clear thought on that. Someone an idea?
I have some borders on a canvas (filled with images) and i want to click the border (i do this with the OnMouseLeftButtonDown) where the border gets red (so the user knows for sure which object he had clicked) and then, after 1 or 2 seconds, when the mousebutton is still pushed down, a drag'n'drop should start.
At first I had the borders inside buttons, but the clickevent seems to conflict with the dragevent. So I got rid of the buttons and did everything inside the borders directly, which works well also. But how can I start the drag after the mousebuttondown and stop it when the mousebuttonup happens before the time runs out.
Someone an idea for a clean solution?
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_source = sender as Border;
Mouse.Capture(_source);
}
private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
_source = null;
Mouse.Capture(null);
}
and in event OnMouseMove you can modify border margins depends on mouse position but after checking if _source is type of Border.
var position = e.GetPosition(Canvas);
EDIT:
For that time you can add property Stopwatch _watch, and inside OnMouseLeftButtonDown event handler you can do it:
_watch = Stopwatch.StartNew();
in OnMouseLeftButtonUp:
_watch.Stop();
and in OnMouseMove before your code:
while (_watch.ElapsedMilliseconds < 2000 && _watch.IsRunning)
{
Thread.Sleep(100);
}
my Question is how to "draw" on multiple labes. I have a form containing a matrix of labels. Now I want to click on one Label drag over some others and all these Labels should change the background color. I have a method which changes the color with the Click-Event, but I can't find an Event for this Problem. I also tried the Mous_Enter Event and checked if the left button was down, but it looks like, that the Event Trigger was stuck in the first label.
So at first I have this, where each number is in a different label:
And then I want to "draw" on the labels, so that the Background Color changes and so I have something like the following:
Connect the MouseClick and MouseMove event of all your labels to the following event handler:
private void MouseClickedOrMoved(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
ChangeLabelBackColor(this.PointToClient(MousePosition));
}
}
and add this function to your code:
private void ChangeLabelBackColor(Point Location)
{
foreach (Label l in this.Controls.OfType<Label>()) {
if (l.Bounds.Contains(Location))
{
l.BackColor = Color.Black;
}
}
}
So my setup is as such; I have, in my C# program for Windows Phone 8, multiple Ellipse elements, all of which call the same method, Checkpoint, when the mouse enters one. The problem is that since I will be drawing a line between the more recently entered ellipse and the previously entered ellipse, I need to know which ellipse any given call came from. If it helps, the code is below:
Point old;
private void CheckPoint(object sender, System.Windows.Input.MouseEventArgs e)
{
if (old.Equals(null))
{
old.Equals(this.);
}
else
{
System.Windows.Shapes.Line connectline = new System.Windows.Shapes.Line();
connectline.X1 = old.Margin.Left;
connectline.Y1 = old.Margin.Top;
connectline.X2 = this. ;
connectline.Y2 = this.
}
}
As you can see, this code is incomplete; old is supposed to be set to whichever ellipse is pressed after it runs through the code block. The "this." are incomplete, and are to be substituted with the margin properties from the ellipse that called the method. Thanks all!
You can identify which is the Selected Ellipse by
private void CheckPoint(object sender, System.Windows.Input.MouseEventArgs e)
{
var selectedEllipse = sender as Ellipse;
if(selectedEllipse!=null)
{
//Your code here
}
}
I am attempting to change a listview item's background colour when a mouse hovers over it
I have a mouse hover event, but how can I add a "highlight" effect upon a mouse hovering over the item?
private void pinnedAppsListBox_MouseHover(object sender, EventArgs e)
{
}
Use this:
private void pinnedAppsListBox_MouseHover(object sender, EventArgs e){
Point point = pinnedAppsListBox.PointToClient(Cursor.Position);
int index = pinnedAppsListBox.IndexFromPoint(point);
if (index < 0) return;
//Do any action with the item
pinnedAppsListBox.GetItemRectangle(index).Inflate(1,2);
}
Go to the ListView's ItemMouseHover event and add then set the property "BackColor" of the Item.
private void listView1_ItemMouseHover(object sender, ListViewItemMouseHoverEventArgs e)
{
e.Item.BackColor = Color.Black;
}
Declare this Global variable
Use this Listview Item variable to keep track of what item was hovered on
ListViewItem lvHoveredItem;
Set the following function to turn on DoubleBuffering for your control to prevent flickering:
public static void SetDoubleBuffering(System.Windows.Forms.Control control, bool value)
{
System.Reflection.PropertyInfo controlProperty = typeof(System.Windows.Forms.Control)
.GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
controlProperty.SetValue(control, value, null);
}
Where your control is loaded call this function
SetDoubleBuffering(lvTaskList, true);
Then use this code in the mousemove event of your listview
private void lvTaskList_MouseMove(object sender, MouseEventArgs e)
{
//Set the Color you want the list Item to be when mouse is over
Color oItemColor = Color.Lavender;
Color oOriginalColor = Color.blue; //Your original color
//get the Item the Mouse is currently hover
ListViewItem lvCurrentItem = lvTaskList.GetItemAt(e.X, e.Y);
if ((lvCurrentItem != null) && (lvCurrentItem != lvHoveredItem))
{
lvCurrentItem.BackColor = oItemColor;
if(lvHoveredItem != null)
{
lvHoveredItem.BackColor = oOriginalColor ;
}
lvHoveredItem = lvCurrentItem;
return;
}
if (lvCurrentItem == null)
{
if (lvHoveredItem != null)
{
lvHoveredItem.BackColor = oOriginalColor;
}
}
}
You can also add the MouseLeave Event
private void lvTaskList_MouseLeave(object sender, EventArgs e)
{
Color oOriginalColor = Color.Blue; //Your original color
//When the mouse leave the control. If a ListViewItem was highlighted then set it's original color back
if (lvHoveredItem != null)
{
lvHoveredItem.BackColor = oOriginalColor ;
}
lvHoveredItem = null;
}
If you're using a ListBox, it is quite more difficult to handle, you will need to set a MouseHover event for the ListBox and determine which item is being hovered on and then draw it manually.
See this answer.
However if you're using a ListView, you can easily add an ItemMouseHover event like this:
private void pinnedAppsListView_MouseHover(object sender, EventArgs e)
{
e.Item.BackColor = Color.Lime;
}
I have seen this question many times without a good answer.
There is no good answer that I know, but, I did it, using some hints elsewhere.
I did this using Lazarus, but you should be able to adapt it to your language.
Get the item. You may want to set variables to catch these individually.
You may also want to get the state of your mouse buttons at mousedown first.
If (ListView.GetItemAt(X,Y) <> nil) then // do this with an if else
// Next, you can get the bounding rect:
ListView.GetItemAt(X,Y).DisplayRect(drSelectBounds);
//Option: If Button up or down then
// you may have to catch this elsewhere, such as for a drag operation.
// Create and set a boolean variable:
HighLightOn := True;
ListView.Repaint; // clears previous hightlights
ListView.Canvas.Brush.Color := clBtnFace; // or your color of choice
ListView.Canvas.FillRect(Rect);
// If you are moving around in an area where GetItem is nil,
// then do this to stop flicker and remove the highlight:
If (ListView.GetItemAt(X,Y) = nil) // do this with an if else
If HighLightOn then
begin
SelectedList.Repaint;
HighLightOn := False;
end;
// If a highlight gets left behind,
// you may need to repeat this elsewhere, such as in a component exit.
// This is the basic gist of the issue.
// There can be a lot of options or things to look for,
// so you code could get more complicated.
// I am not suggesting this is the best way to implement it,
// but it is easy. Part of this code only works inside your app!