Spawning images/pictureboxes - c#

In C# WPF, I'm trying to create a new picturebox on the point where I clicked, whenever I click on the main window. I'm not too sure how to approach this since I could not find anything on the internet about this.

Are you sure you want to create new one each time? That could potentially be a bad idea. But still, if you want to, You can do it in codebehind like this:
public MainWindow()
{
InitializeComponent();
MouseUp += MainWindow_MouseUp; //add eventhandler vor click event
}
void MainWindow_MouseUp(object sender, MouseButtonEventArgs e)
{
var img = new Image(); //create new instance of image
img.Width = 100; //set some size properties
img.Height = 100;
img.Source = somesource;//set source
MainGrid.Children.Add(img); //add it as a child to some conteiner element, like grid.
}
But please reconsider reusing controls, if possible.

Related

C# Form Button added via code doesn't work on Click

I got a form. At Designer I wrote
public Button dugme = new Button();
and
this.Controls.Add(dugme);
dugme.Location = new Point(100, 300);
dugme.Size = new Size(400, 50);
dugme.Text = "Hesapla";
after that I went to Form1 and wrote
private void dugme_Click(object sender, EventArgs e)
{
MessageBox.Show("String");
}
But when I press the button it doesn't work. And not just in these function, I wrote different things too. How can I fix that?
I think you miss that binding Click event for your dugme Button.
Which might add to InitializeComponent method.
dugme.Click += new System.EventHandler(this.dugme_Click);
so the part of the code might look as below.
this.Controls.Add(dugme);
dugme.Location = new Point(100, 300);
dugme.Size = new Size(400, 50);
dugme.Click += new System.EventHandler(this.dugme_Click);
dugme.Text = "Hesapla";
If you are doing an android app or something like that, you have to add this line in your oncreate :
$btnVar$.Click += function;

C# Event Handler is called multiple times when selecting a menu item

I want to implement some Pictureboxes and when one of them is clicked, a MessageBox should appear and tell which Box is clicked.
However, I want to implement a choice how many Pictureboxes should appear. When I choose another MenuItem, then the click event will be called multiple times. I tried unsubscribing, but it doesn't work.
Here is my code:
public partial class Form1 : Form
{
PictureBox Pbox1;
PictureBox Pbox2;
PictureBox Pbox3;
PictureBox Pbox4;
public Form1()
{
InitializeComponent();
this.Text = "Picturebox";
Pbox1 = new PictureBox();
Pbox2 = new PictureBox();
Pbox3 = new PictureBox();
Pbox4 = new PictureBox();
}
private void Pbox_Click(object sender, EventArgs e, int nr)
{
MessageBox.Show("Picture number " + nr.ToString() + " is clicked");
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
Pbox1.Image = new Bitmap(#"Picture.png");
Pbox1.Location = new Point(20, 40);
Pbox1.Size = new Size(160, 120);
Pbox1.SizeMode = PictureBoxSizeMode.StretchImage;
Pbox1.Click += (sender2, e2) => Pbox_Click(sender2, e2, 1);
this.Controls.Add(Pbox1); }
The rest is the same as toolStripMenuItem2.
Can you help me with this problem?
When will it be called multiple times? I guess when switching to another menu item everything should work fine. But when switching back to an item that was already clicked before, then another click handler will be registered in addition to the existing one and will call the Pbox_Click method twice.
You can try to put the event registration in the constructor method or release other click-events before re-registering them.

C# mouse event for class object

First - I'm a graphics designer - not a programmer :/
I'm trying create simple aplication (C# windows Form application) with option to add some objects (PictureBox) and allow user to drag those PicturesBoxes on form (change their positon after adding to form).
I can do it for one PictureBox, but can't add function to all dinamic created objects :/
I have something like that for standard Picturebox4
public bool moveDetect = false;
private void pictureBox4_MouseDown(object sender, MouseEventArgs e)
{
moveDetect = true;
}
private void pictureBox4_MouseUp(object sender, MouseEventArgs e)
{
if (moveDetect)
{
Point pozycja = new Point();
this.Cursor = new Cursor(Cursor.Current.Handle);
pozycja = this.PointToClient(Cursor.Position);
pictureBox4.Location = pozycja;
}
}
Does anyone know any tutorial showing how to add function like above to my simple class "myPictureBox : Picturebox"
My class is:
class myPictureBox : PictureBox
{
public bool moveDetect = false;
// constructor
public myPictureBox(int w, int h, string name)
{
this.Width = w;
this.Height = h;
this.Image = Image.FromFile("../../Resources/" + name + ".png");
Debug.WriteLine("Created ...");
}
}
Constructor works good and show "Created..." in output. Cant add function to all objects :/
Thanks and Regards ;)
If I understand correctly, your code works fine with the event handlers MouseUp and MouseDown when you are working with PictureBoxes that you create at design time using the designer.
You can add these same event handlers to controls that are created dynamically when you instantiate them:
MyPictureBox dynamicPicBox = new MyPictureBox(800, 600, "JustATest");
dynamicPicBox.MouseDown += pictureBox_MouseDown;
this adds an event handler that maps to the method pictureBox_MouseDown
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
moveDetect = true;
}
Since your custom class is derived from PictureBox, it should recognize this type of event handler.

Onclick Button open a new Window only with Image inside

I am new to WPF and I've read a lot of answers, but none of them worked for me.
I have a button with a Click method. When I click on it I want my program to show a new Window, only with an image inside, which URL is previously given. Can you show me the simplest way to create such onclick method ?
This code will start you up:
private void button1_Click(object sender, EventArgs e)
{
Form form = new Form();
form.Show();
PictureBox pb = new PictureBox();
pb.ImageLocation = "https://www.google.com/images/srpr/logo11w.png";
pb.Size = form.Size;
pb.SizeMode = PictureBoxSizeMode.Zoom;
form.Controls.Add(pb);
}

Adding controls dynamically in xaml window store app with events attached?

I have a canvas ,inside it an image control which has events ManipulationDelta Event , Click Event etc etc.
I have an ADD button which add an image control to the canvas
Image image = new Image();
string url = "ms-appx:///Assets/992829_484663934955807_859212711_n.jpg";
BitmapImage bm = new BitmapImage();
bm.UriSource = new Uri(url, UriKind.Absolute);
image.Source = bm;
image.Height = 100;
image.Width = 100;
MyCanvas.Children.Add(image);
Now I want to create same events for this controls and many other also dynamically so that they have their own ManipulationDelta and Click events . How to achieve that ? Should I use Anonymous methods,Lambda Expressions etc. Just a hint which will guide me??
subscribe event like this
image.ManipulationDelta += image_ManipulationDelta;
image.Tapped += image_Tapped;
void image_Tapped(object sender, TappedRoutedEventArgs e)
{
//do something
}
void image_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
//do somthing
}
in Visual Studio .. just type image.ManipulationDelta += and then press Tab 2 times. It will create event handler method automatically
also you can create event handler by using Anonymous method
image.ManipulationDelta += (sender, e) =>
{
//do something
};

Categories