Drag from treeview to picture box and connect elements - c#

I'm stuck with this problem. I have in one frame pictureBox and treeview. I want to drag and drop from treeview to picture box and in that picture box I paint another picture (first picture box is map in treeview i have 'Sings')
private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
Type testTip = new TreeNode().GetType();
TreeNode dragedNode;
_mouseDownSelectionWindow= Rectangle.Empty;
if (e.Data.GetDataPresent(testTip))
{
dragedNode = (TreeNode)e.Data.GetData(testTip);
PictureBox picOneFaceUpA = new PictureBox();
picOneFaceUpA.Parent = MapView;
picOneFaceUpA.Tag = dragedNode;
TreeNode tr = (TreeNode)picOneFaceUpA.Tag;
Sing vr = (Sing)tr.Tag;
picOneFaceUpA.Name = vr.idSing;
vr.onMap = true;
int xCord = e.X;
int yCord = e.Y;
picOneFaceUpA.Location = MapView.PointToClient(new Point (xCord - 20, yCord - 20));
picOneFaceUpA.BackgroundImage = vr.image;
vr.location = picOneFaceUpA.Location;
dictionary.Add(picOneFaceUpA.Location, vr);
picOneFaceUpA.Size = new Size(40, 40);
picOneFaceUpA.BackgroundImageLayout = ImageLayout.Stretch;
picOneFaceUpA.BringToFront();
picOneFaceUpA.Focus();
}
}
And that works, but how I can connect so when I click on Sing on map it gets some kind of focus(frame or something) and it selects treenode in treview that I used to make that picture box.

You already have created a connection when you have stored the dragged node in the PictureBox's Tag.
To make it work you need to code a few events for the created PictureBoxes.
Here is the minimum:
When clicked I set a Border, select the Treenode you have already stored in the Tag and explicitly set the focus on the PictureBox. (PBs don't usually get focus when clicked, so we need to do it in code, so we can catch the LostFocus event..)
picOneFaceUpA.Click += (ss,ee) => {
picOneFaceUpA.BorderStyle = BorderStyle.FixedSingle;
treeView1.SelectedNode = dragedNode;
picOneFaceUpA.Focus();
};
And in the LostFocus I simply remove the Border:
picOneFaceUpA.LostFocus += (ss, ee) =>
{
picOneFaceUpA.BorderStyle = BorderStyle.None;
};
Don't forget to set the treeview HideSelection = false; so you can see the selection while the focus still is on the PB!
Now, depending on the images you show, the border may be a bit weak. For a stronger effect you would have to code the Paint event to draw something more flashy on the top of the control..:
picOneFaceUpA.Paint += (ss, ee) =>
{
if (picOneFaceUpA.BorderStyle == BorderStyle.FixedSingle)
ee.Graphics.DrawRectangle(Pens.Orange, 0, 0,
picOneFaceUpA.ClientSize.Width - 1,
picOneFaceUpA.ClientSize.Height - 1);
};
In your code you use a picOneFaceUpA.Focus();call. This seems to clash with setting the LostFocus event, so will have to remove it!

Related

Dynamically sized children of WrapPanel

I have a list of texts placed in WrapPanel, representing nested text blocks... The texts are clickable so I wanted to emphasize it, so when user enters mouse over them, I make them bigger (basically I'm just increasing the font by one). The problem is that this changes size of the framework element which affects layout in WrapPanel, meaning the panel can move last item into next row. Normally there is enough space, or even if not, it's not a problem when hovering over some text in the middle. But if user want's to click last element when it's near the edge of the panel, he basically can't. Because as soon as the element grows, it moves to next row and the cursor is not longer over it, so it shrinks again and panel moves it back to first row, where it is under cursor again, and so on...
Here you can see this scenario:
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
var wp = new WrapPanel() {
Background = Brushes.Blue,
Orientation = Orientation.Horizontal,
VerticalAlignment = VerticalAlignment.Top,
Children = {
createChild(),
createChild(),
createChild(),
createChild(),
}
};
Content = new Grid() {
Background = Brushes.Gray,
Children = { wp }
};
}
private UIElement createChild() {
var g = new Grid() {
Width = 100,
Height = 20,
Margin = new Thickness(5),
Background = Brushes.Red,
};
g.MouseEnter += (s, e) => { g.Width *= 2; g.Background = Brushes.White; };
g.MouseLeave += (s, e) => { g.Width *= .5; g.Background = Brushes.Red; };
return g;
}
}
How can I avoid it?
I was thinking of some "margin" property I could specify in WrapPanel so it will move the child to next row earlier when it's close to the edge whiting that margin (even if it could actually fit), but I didn't found such thing.

How do I make a border around the windows form

How do I create a border made of picture boxes, I'm trying to make a snake game and I want to first make a border for the playing area, using picture boxes, so far I have used a while loop to make the top row but I feel there would be a better way to make the whole thing, I heard a helper function may help but I'm not sure how to go about that either
int i = 1;
public Snake()
{
InitializeComponent();
}
private void Snake_Load(object sender, EventArgs e)
{
while (i < 404)
{
var picture = new PictureBox
{
Name = "pictureBox",
Size = new Size(20, 20),
Location = new Point(i, 0),
BackColor = Color.FromArgb(0,0,0),
};
this.Controls.Add(picture);
i += 20;
}
}

Make Panel scrollable

I am working on a simple Windows Forms application which consists of a Panel where I draw graphics with Graphic. Let's say, my panel is now of size 300x300 but the content inside is 500x500. Obviously, I need to add scrollbars to the Panel.
My code so far:
public CircuitControl()
{
// Initialize empty list of circuit objects
CircuitObjects = new List<CircuitObject>();
drawingAreaPanel.AutoScroll = true;
drawingAreaPanel.VerticalScroll.Enabled = true;
drawingAreaPanel.VerticalScroll.Visible = true;
drawingAreaPanel.HorizontalScroll.Enabled = true;
drawingAreaPanel.MaximumSize = new Size(300, 300);
drawingAreaPanel.Size = new Size(600, 600);
}
But none of these codes create actually a scroll bar. My question is: Where and how do I set the size of the Panel where I actually drew? I think this is the part which is missing. Thanks.
The scrollbars won't show up until there's actually something in the Panel that you can't see all of.
Try placing a larger control, such as a PictureBox, inside the Panel, and setting the PictureBox's initial size as larger than the Panel.
Just add:
drawingAreaPanel.AutoScroll = true;
And it will be done automatically.
€dit: Don't forget to set the anchors in order to get the scrollbars.
A clean and simple approach is to set AutoScrollMinSize. This shows the scrollbars (or just one if you leave the other value at 0).
Now drawing through the graphics object will not be scrolled automatically.
This can be easily achieved with a transformation matrix, which is set before painting and translates the drawing by the scroll offset.
A pretty example: (this flickers of course without further optimizations)
private void button1_Click(object sender, EventArgs e)
{
using(Form frm = new Form())
{
Panel pnl = new Panel();
pnl.Paint += delegate (Object snd, PaintEventArgs e2) {
Matrix mtx = new Matrix();
mtx.Translate(pnl.AutoScrollPosition.X, pnl.AutoScrollPosition.Y);
e2.Graphics.Transform = mtx;
e2.Graphics.Clear(Color.Black);
for(int i=0; i <= 125; i++)
for(int j=0; j <= 125; j++)
using(Brush b = new SolidBrush(Color.FromArgb(255, 255-i*2, j*2, (i*j) % 255)))
e2.Graphics.FillRectangle(b, new Rectangle(5+j*20, 5+i*20, 20, 20));
};
pnl.AutoScrollMinSize = new Size(126*20+10, 126*20+10);
pnl.Dock = DockStyle.Fill;
frm.Controls.Add(pnl);
frm.Padding = new Padding(25);
frm.ShowDialog(this);
}
}

Forcing the inner part of a form to stay in the same position while changing border style

I have a winform that starts out with a sizable border around it. On the form is a button which, when pressed, changes the form to border style none.
The problem is that then the inner part of the form moves up and to the left slightly. I want to make it that, no matter what border is used, the "inner" part of the form will always stay in the same spot (note: but I do still want the form to be moved around when it has a movable border selected)
Thanks.
The Borderless form moves up and slightly left because thats the location that the form currently has,you need to count for the border.To achieve the result you are after you need to reassign the location property and to do that you need to account for the client size and the whole size(with border),the code i think is simple and it will be self-explanatory i believe:
private void button1_Click(object sender, EventArgs e)
{
var X = (this.Size.Width - this.ClientRectangle.Width) / 2;
var Y = (this.Size.Height - this.ClientRectangle.Height) - X;
Point p = new Point(Location.X + X, Location.Y + Y);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Location = p;
}
Another option is to set the padding on the parent that contains the panel with the border. E.g.
public class Form10 : Form {
Button btn = new Button { Text = "Button", Location = new Point(100, 100) };
Panel border = new Panel { Dock = DockStyle.Fill, BorderStyle = BorderStyle.Fixed3D };
public Form10() {
Controls.Add(border);
border.Controls.Add(btn);
btn.Click += delegate {
if (border.BorderStyle == BorderStyle.Fixed3D) {
border.BorderStyle = BorderStyle.None;
border.Parent.Padding = new Padding(2);
}
else {
border.BorderStyle = BorderStyle.Fixed3D;
border.Parent.Padding = new Padding(0);
}
};
}
}

Image Button in C# Code behind

I'm trying to set an image background in code-behind. I tried adding a background image to the button, but as soon as I hover over the button, the image disappears. To solve this, I have to write functions to override the button behavior, which is too much to do in code-behind.
I then use an alternative method, that is to add a button and an image separately to a grid cell. The issue is -- when I click on the image, the button won't trigger.
How do I make the button to have the hover and pressed effect, even when, the mouse is either hovering or presses the image on the button, but not the remaining area of the button?
Or hope someone can suggest me a better solution.Below is my code.
InitializeComponent();
Button playBtn = new Button();
playBtn.Width = 60;
playBtn.Height = 30;
Image playIcon = new Image();
playIcon.Source = new BitmapImage(new Uri(#"PATH"));
playIcon.Stretch = Stretch.Uniform;
playIcon.Height = 25;
grid1.Children.Add(playBtn);
grid1.Children.Add(playIcon);
Grid.SetColumn(playBtn, 0);
Grid.SetRow(playBtn, 0);
Grid.SetColumn(playIcon, 0);
Grid.SetColumn(playIcon, 0);
thanks for everyone input, after digging more into it, it sort of work out. What I did is add a Grid to Button.Content then add the image to the Grid. And using Opacity to add the grey out effect for IsEnable false state. Below I post my code, hope someone find it useful or improve on:
Button playBtn = new Button();
Image playIcon = new Image();
public MainWindow()
{
InitializeComponent();
Grid grid2 = new Grid();
RowDefinition grid2_row1 = new RowDefinition();
ColumnDefinition grid2_col1 = new ColumnDefinition();
grid2.RowDefinitions.Add(grid2_row1);
grid2.ColumnDefinitions.Add(grid2_col1);
playBtn.Width = 60;
playBtn.Height = 30;
playBtn.Click += playBtn_Click;
playIcon.Source = new BitmapImage(new Uri(#"pack://PATH..."));
playIcon.Stretch = Stretch.Uniform;
playIcon.Height = 25;
playBtn.Content = grid2;
grid2.Children.Add(playIcon);
grid1.Children.Add(playBtn);
Grid.SetRow(playIcon, 0);
Grid.SetColumn(playIcon, 0);
}
public void playBtn_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello");
}
private void button1_Click(object sender, RoutedEventArgs e)
{
playBtn.IsEnabled = false;
playIcon.Opacity = 0.3;
}
Buttons in WPF have different states = "normal", "mouse over" and "pressed" are three.
When you create the button you are setting up it's "normal" state. You also need to set the "mouse over" state to have the same image as well.
Check the below links
http://blogs.msdn.com/b/knom/archive/2007/10/31/wpf-control-development-3-ways-to-build-an-imagebutton.aspx
http://www.hardcodet.net/2009/01/create-wpf-image-button-through-attached-properties
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/91df562f-414c-4326-ac65-42ef301b5f8f/

Categories

Resources