Onclick Button open a new Window only with Image inside - c#

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);
}

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;

How to make a form in a panel stop moving in VisualStudio?

I created two forms and adding one panel on each. The button from the first form opens the second one, and the button from the second one opens the first form. The problem is that whenever i press the button to see the other form, the whole panel moves!
This is the code i have for the both buttons.
private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
Form1 form = new Form1();
form.TopLevel = false;
panel1.Controls.Add(form);
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
form.Dock = DockStyle.Fill;
form.Show();
}
I also posted a gif to see the movement they do:
movement_problem
I would change the color of the whole Form like this
this.BackColor = Color.FromArgb(0, 0, 0); // this is black

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.

Datagridview shall disappear when clicking on the Form in the background

As described in the title, I have a Form with a Datagridview on the front. The datagridview is smaller than my form in the back and I want the Datagridview to disappear whenever I click anywhere else but the Datagridview.
My code looks like this:
this.dataGridView1.Leave += new System.EventHandler(this.focus);
and the Eventhandler is defined like this:
private void focus(object sender, EventArgs e)
{
if(dataGridView1.Focused == false)
{
dataGridView1.Visible = false;
}
}
My problem is that my Datagridview only disappears when a new event in my form is activated but not when I click for example in a textbox on my form.
Can anyone help me?
The Leave event will not raise if you click on Form, or a ToolStripButton, PictureBox or any other non-selectable control.
If you expect a behavior like a dropdown, you can host DataGridView in a ToolStripControlHost and show it using a ToolStripDropDown. This way when you click anywhere outside the `DataGridView, it will disappear. It will act like a dropdown menu. Also the grid can be larger than your form:
private void button1_Click(object sender, EventArgs e)
{
this.dataGridView1.Margin = new Padding(0);
var host = new ToolStripControlHost(this.dataGridView1);
this.dataGridView1.MinimumSize = new Size(200, 100);
host.Padding = new Padding(0);
var dropdown = new ToolStripDropDown();
dropdown.Padding = new Padding(0);
dropdown.Items.Add(host);
dropdown.Show(button1, 0,button1.Height);
}
Important Note: It's an example. It's better to pay attention to disposing of objects in a real world application. For example, use just a single ToolStripdropDown and dispose it when closing the form.
Change the event handler assigning to:
this.dataGridView1.Leave += new System.EventHandler(fokussiert);
Worked for me when focusing on a textbox
you want your dgv also to disapear when you click on your textbox? is that what you mean?
private void dataGridView1_Leave(object sender, EventArgs e)
{
dataGridView1.Visible = false;
}
private void Form1_Click(object sender, EventArgs e)
{
dataGridView1.Visible = false;
}

How can i show an image while my application is loading

i have and application windows form .net and my form1 takes a lot of time to appear because in it's event form1_Load does a lot of operation.
My goal is to show an image while the operation are being done.
private void form1_Load(object sender, EventArgs e)
{
methode1();
}
While my methode1() is working, my form doesnt show, i want to show an image on the screen while my methode1() is working because while methode1() is working, there is nothing on the screen.
All the visual things in .net is done on form. You can do it by creating an small form which contains an image load it before module1() and after completing module1() close it. Just below..
private void form1_Load(object sender, EventArgs e)
{
Form f = new Form();
f.Size = new Size(400, 10);
f.FormBorderStyle = FormBorderStyle.None;
f.MinimizeBox = false;
f.MaximizeBox = false;
Image im = Image.FromFile(path);
PictureBox pb = new PictureBox();
pb.Dock = DockStyle.Fill;
pb.Image = im;
pb.Location = new Point(5, 5);
f.Controls.Add(pb);
f.Show();
methode1();
f.Close();
}
Create another form, just for loading, with a static image, and display it before your application starts to load, and destroy it afterwards. Always on top, and with no border is the usual setup for such things.
Try this code
using System.Reactive.Linq;
private void RealForm_Load(object sender, EventArgs e)
{
var g = new Splash();
// place in this delegate the call to your time consuming operation
var timeConsumingOperation = Observable.Start(() => Thread.Sleep(5000));
timeConsumingOperation.ObserveOn(this).Subscribe(x =>
{
g.Close();
this.Visible = true;
});
this.Visible = false;
g.ShowDialog();
}
This code uses Microsoft Rx to execute operations in background threads among other cool features
http://msdn.microsoft.com/en-us/data/gg577609.aspx
In order for this code to work you need to reference two nuget packages: Rx and Rx windows forms
https://nuget.org/packages/Rx-Main/1.0.11226
https://nuget.org/packages/Rx-WinForms/1.0.11226
(splash screen c# -- google it)
Here's what I just found:
http://msdn.microsoft.com/en-us/library/aa446493.aspx
How about using the built in SplashScreen class?
http://msdn.microsoft.com/en-us/library/system.windows.splashscreen.aspx

Categories