How to receive event on Panel Controls? - c#

I have Form, it has 1 Panel and Panel has 0-N PictureBox's - dynamically added to the Panel, but if I take my mouse over the PictureBox and click on it, it don't fire any action. I mean when I click on Panel, it fire the click method, but how I can make sure that those PictureBox's also behave the same way?

Since picture boxes are added dinamically you need to attach event handelers for the click event in your code.
do somthing like this just before you add it to the control collection..
PictureBox pbx = new PictureBox();
pbx.Click += new EventHandler(pbx_Click);
//Now assign other properties and then add it to control collection
//panel1.Controls.Add(pbx);
private void pbx_Click(object sender, EventArgs e)
{
//handle the click event here
}

Is your PictureBoxes are added dynamically to the Panel, then the Click event of the PictureBox must also be added.
Note that you can handle all of the PictureBoxes Click event in the same handler:
for (int i = 0; i < 10; i++)
{
PictureBox pb = new PictureBox();
pb.Name = "pb" + i;
pb.Click +=new EventHandler(pb_Click);
this.Controls.Add(pb);
}
void pb_Click(object sender, EventArgs e)
{
PictureBox pb = (PictureBox) sender;
if (pb.Name == "pb1")
{
...
}
}
You get the idea...

Related

How can I make a function to relocate and resize a set of controls

I have 10 panels all visible = false and have 10 buttons
panels name = p1, p2, p3...etc
buttons name = b1, b2, b3...etc
I wanna when click on button(b1)
make panel(p1) visible = true
relocate and resize panels
The new location and new size for all panels are same,
Can I make a function do the relocate and resize instead of write a code more times like this
private void b1_Click(object sender, EventArgs e)
{
p1.Visible = true;
p1.Location = new System.Drawing.Point(9, 247);
p1.Size = new System.Drawing.Size(1120, 464);
}
You can assign all buttons' click event to the same event handler. The parameter sender is the object that fires the event which in your case it is one of your buttons. Then check the sender's Text or Name property and find which button was clicked. For panels, you could make a method with a Panel type parameter and do what you want.
private void btn_Click(object sender, EventArgs e)
{
// here put code to hide all panels
//...
//then show and relocate the panel related the clicked button as follows
switch ((sender as Button).Name) //or button's Text
{
case "b1":
showPanel(pl);
break;
case"b2":
showPanel(p2);
break;
//other cases
//...
}
}
private void showPanel(Panel pnl)
{
//show and relocate
pnl.Visible = true; //I consider pnl.Show(); as it's more readable
//...
}
Don't forget to hide all panels at the beginning of the click event. You can use following method if you do not have any other panel in your control except those 10 ones.
private void hidePanels()
{
foreach (Control c in yourControl.Controls) //replace yourControl with the control that is the container of your panels e.g. your form
{
if (c is Panel) c.Visible = false; //or c.Hide();
}
}
I haven't tested this code, but it should work.

How to Add Mouse click event to Picturebox using C#.net

i am creating simple inventory system. i need only this if i click picturebox 1 relavent message should display "pic1" if i click piturebox 2 relavent message should display "pic2"
public Form1()
{
InitializeComponent();
this.MouseClick += mouseClick;
}
private void mouseClick(object sender, MouseEventArgs e)
{
var clickedPictureBox = (PictureBox)sender;
if (clickedPictureBox == pictureBox1)
{
MessageBox.Show("Pic1");
}
}
i tried the code but it is not working
First, you're using the form's mouse click event, you'll need to replace:
this.MouseClick += mouseClick;
by:
pictureBox1.MouseClick += mouseClick;
pictureBox2.MouseClick += mouseClick;
Option 1: Use the sender object
The mouse click event gives you the argument sender, which is the object that triggered the event.
So you could use it like this:
var clickedPictureBox = (PictureBox)sender;
if (clickedPictureBox == pictureBox1)
...
Option 2: use a tag
You can set tags in your winforms elements and use them. For example, go to the Designer and set the tag of both pictures to "Pic1" and "Pic2" and then use it like this:
var clickedPictureBox = (PictureBox)sender;
if (clickedPictureBox.Tag.ToString() == "Pic1")
...

How to use "for" to set the background image of a group of buttons in a C# Windows Forms application

I want to use a wheel to change (for example) 30 buttons' background image each time a form loads.
I cannot use this:
for(int i=1;i<=30;i++)
{
button i .backgroundimage=image.fromfile("URL");
}
What should I do?
There are many possible interpretations of your problem. Why can't you use your code? There are also different solutions for your problem.
As example:
public Form1() // Constructor
{
InitializeComponent(); // Ensure all controls are created.
List<Button> buttons = new List<Button>(30);
buttons.Add(mybutton1)
buttons.Add(mybutton2)
// Go futher with all your buttons.
}
private void Form1_Load(object sender, System.EventArgs e) // Create a load event
{
foreach(Button button in buttons)
{
button.BackgroundImage = Image.FromFile(path);
// Note: The file remains locked until the Image is disposed!
}
}
Well you could use something like this assuming this code executes in a Form_Load and the buttons Parent control is your form. Have in mind that you should supply the real path to your image that you want to set as a background image
string path = "rootNameOfTheImage";
int counter = 0;
foreach(Control ctrl in this.Controls)
{
if(ctrl is Button)
{
Button btn = (Button)ctrl;
if(/* test if this button should be used */)
{
btn.BackgroundImage=Image.FromFile(path + counter++.ToString() + ".jpg");
}
}
}

MouseEnter & MouseLeave objectname

I want to add MouseOver and MouseLeave events to dynamical created panels in a flowLayoutPanel.
I added all panels in a list named "panels" and they are accessible with "panels[index]".
Now I want to dynamical add a MouseOver and MouseLeave event to each panel.
I thought it could be possible to get the panelname the Mouse is over and use just one method for each event and identify the panel the mouse is over with its panelname (panel.Name) but I found nothing in "sender".
Is there a way to do this?
My code:
//Method
private void PanelsMouseEnter(object sender, EventArgs e)
{
var panel = sender as Control;
foreach (Control control in this.fLpKoerper.Controls)
{
if (control.Name == panel.Name)
{
foreach (Panel panels in panelsKoerper)
{
if (panels.Name == panel.Name)
panels.BackColor = Color.DarkGray;
}
}
}
}
//Event
panelsKoerper[y].MouseEnter += PanelsMouseEnter;
var panel = sender as Control;
var thePanelName = panel.Name;
I believe you can generate one mouseover event for a control, copy that event method name and then paste it into another controls mouseover event box and that should work
So you would have this event
private void label1_MouseHover(object sender, EventArgs e)
{
//Code...
}
and then you could put 'label1_MouseHover' in any controls mouseover event

How to allow user to drag a dynamically created control at the location of his choice

I am creating an application where I need to generate dynamically created controls say textbox or label etc.
Now what I that user can relocate that textbox to his desired location. Like we do in Visual Studio.
One way is to get new location by getting values from him using textbox. But I want the user interface easy.
Can we have such functionality in winforms
I have created a simple form that demonstrate how to move the control by dragging the control.
The example assumes there is a button named button1 on the form attached to the relevant event handler.
private Control activeControl;
private Point previousLocation;
private void button1_Click(object sender, EventArgs e)
{
var textbox = new TextBox();
textbox.Location = new Point(50, 50);
textbox.MouseDown += new MouseEventHandler(textbox_MouseDown);
textbox.MouseMove += new MouseEventHandler(textbox_MouseMove);
textbox.MouseUp += new MouseEventHandler(textbox_MouseUp);
this.Controls.Add(textbox);
}
void textbox_MouseDown(object sender, MouseEventArgs e)
{
activeControl = sender as Control;
previousLocation = e.Location;
Cursor = Cursors.Hand;
}
void textbox_MouseMove(object sender, MouseEventArgs e)
{
if (activeControl == null || activeControl != sender)
return;
var location = activeControl.Location;
location.Offset(e.Location.X - previousLocation.X, e.Location.Y - previousLocation.Y);
activeControl.Location = location;
}
void textbox_MouseUp(object sender, MouseEventArgs e)
{
activeControl = null;
Cursor = Cursors.Default;
}
It is the easiest way: First go to your solution name and right click. Select "Manage NuGet Packages". After a while a window opens with a search bar on top of it. Choose the "Browse option " and search DraggableControl package. The name Control.Draggable must be seen. Click on it then click install. Now you can use it's special commands for example.
private void button1_Click(object sender, EventArgs e)
{ Point p = new Point(20,70 * i);
RichTextBox tb = new RichTextBox();
tb.Location = p;
tb.Height= 60;
tb.Width = 100;
tb.Font = Normal;
ControlExtension.Draggable(tb,true);
this.Controls.Add(tb);
i++;
The ControlExtension.Draggable command can set it to be draggable or not. Just write the name of the object in the brackets (tb for me) and write a comma. Then write it is draggable (true) or not (false).
NOTE: Do not Forget to put a semicolon.
Hope it helps.
Link : https://www.nuget.org/packages/Control.Draggable/
You can call DoDragDrop with a data object containing or representing the control to begin a drag&drop operation, then handle the container's DragDrop event and move the control.
If you want to see the control as it's dragged, you can either make a transparent (handle WM_NCHITTEST) form under the mouse showing the control (call DrawToBitmap), or not use drag&drop at all and instead handle mouse events and track state manually.
If you want Visual Studio-style snaplines, you can compare the control's bounds to other controls, make a set of lines to draw, and draw them in a paint event.

Categories

Resources