i have generated picturebox dynamically... now i have to displaying Different images on that picture boxes after that when i click the particular picture box it should be displayed in the next form Picture box.... how do i know the particular picturebox got clicked.... and How can I do it... reply me.. Thanks In Advance..
and my coding is
for(int i=0;i<Num_Picbox;i++)
{
shapes[i].Location = new Point(Left,Top);
Left += 200;
Top += i + 0;
shapes[i].Size = new Size(150, 150);
shapes[i].BackColor = Color.Black;
shapes[i].Visible = true;
shapes[i].BorderStyle = BorderStyle.FixedSingle;
this.Controls.Add(shapes[i]);
shapes[i].Click += new EventHandler(PictureBox_Click);
}
private void PictureBox_Click(object sender, EventArgs e)
{
int imageid = 1;
ClsProperty.ImageId = imageid;
fd2 = new frmImageDisplay(imageid, ClsProperty.ipaddress);
fd2.Show();
}
The "sender" in the event handler will be the picture box that got clicked:
private void PictureBox_Click(object sender, EventArgs e) {
PictureBox senderAsPictureBox = sender as PictureBox;
//this is the picture box that got clicked
int imageid = 1;
ClsProperty.ImageId = imageid;
fd2 = new frmImageDisplay(imageid, ClsProperty.ipaddress);
fd2.Show();
}
It would be helpful if you show your code, but anyways,
if you are dynamically creating a picture box, you can add a code like .Click += your method name.
This is more help about dynamically adding an event to the control when adding them
Hope it helps
Ok, I think it's easy, the first argument of event is always object sender,
cast it to the picture box object and read the ID property, and you can move forward with your problem!
Related
btnName1 = new Button();
counter++;
//Start setting of Button
btnName1.Location = new Point(47, 35 + a);
btnName1.Size = new Size(132, 59);
btnName1.FlatStyle = FlatStyle.Popup;
btnName1.Text = textBox1.Text;
btnName1.Name = "btn" + counter.ToString();
btnName1.BackColor = Color.White;
btnName1.ForeColor = Color.Black;
panel1.Controls.Add(btnName1);
a += btnName1.Size.Height + 2;
btnName1.Click += BtnName1_Click;
I wrote this code for making a new button. When I click on add button this code runs and by each click on add button we can make new button.
But my problem is this
How can I set click handle for each button? I mean when I click on each button, they show their text to me
and I wrote this code to make the texts different:
btnName1.Text = textBox1.Text;
You didn't post your event code, but as Steve mentioned in his comment (that appears to have since been removed), you can use the sender argument to get the particular button that was clicked. Something like the following should be what you're after:
private void BtnName1_Click(object sender, EventArgs e)
{
//Access the text with: (sender as Button).Text
//Example: Write this button's text to the debug output window
Debug.WriteLine((sender as Button).Text);
}
Just be careful that in my specific example, you're only subscribing a Button to this event.
Prefered way is what you've put so far
btnName1.Click += BtnName1_Click;
...
private void BtnName1_Click(object sender, EventArgs e) {
// Button which has been clicked
Button button = sender as Button;
//TODO: put relevant code here
...
}
However, you can assign to each button its own event handler:
btnName1.Click += (s, e) => {
Button button = s as Button;
//TODO: put relevant code here
...
};
For a school project, I have to make a table reservation system,
I made the following (just a small part).
PictureBox[] pb = new PictureBox[70] { pictureBox1, pictureBox2, pictureBox3,etc.. };
foreach (PictureBox p in pb)
{
p.BorderStyle = BorderStyle.Fixed3D;
p.BackColor = Color.White;
p.MouseEnter += new EventHandler(mouseOn);
p.MouseClick += new MouseEventHandler(mouseClick);
}
private void mouseOn(object sender, EventArgs e)
{
((PictureBox)sender).BackColor = Color.Green;
}
private void mouseClick(object sender, EventArgs e)
{
reservationForm rf = new reservationForm();
rf.ShowDialog();
}
I chose for pictureboxes to represent the tables, the default BackColor is white, and when the mouse enters the BackColor turns green.
When you click on 1 of those PictureBoxes my reservationForm will open, this is where you can further fill in the details to book the table.
but the part where I get stuck now is that I don't know on my reservation form which PictureBox I clicked, so I need to get something like the index of the PictureBox array.
How do I fix this?
You know how to do it! :)
You're already doing it correctly on the mouseOn event handler. Just do the same cast of the sender parameter.
private void mouseClick(object sender, EventArgs e)
{
PictureBox clickedBox = (PictureBox)sender;
reservationForm rf = new reservationForm();
rf.ShowDialog();
}
If you need the index, you can use Array.IndexOf, assuming your pb array is a class level variable and not a method local.
int index = Array.IndexOf(pb, clickedBox);
Check sender
private void mouseClick(object sender, EventArgs e)
{
PicterBox pb = (PicterBox)sender;
...
}
In your "MouseOn" you already use (PictureBox)sender to get a reference to the picturebox.
Then you can walk through the array to compare the picturebox at a specific index to the clicked one. When you have a match, you found the index.
var clicked = (PictureBox)sender;
int index = 0;
while (index < pb.Length && pb[index] != clicked) index = index+1;
// now index is either 70 (if not found) or the position you want (0..69)
Maybe you can set in for cycle a name at each picturebox equals at the corrispondent index. So in the listener u can use
PicterBox pb = (PicterBox)sender;
and get the name that is the index.
You have many options. I will suggest some of them:
Create a public class variable or property and store the current selected Picturebox on the click event, so you can access it from the form;
Pass in the Picturebox for the constructor of the form (creating a constructor that accepts this info and stores it in an internal variable).
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...
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.
I'm making a windows forms application using C#. I add buttons and other controls programmatically at run time. I'd like to know how to handle those buttons' click events?
Try the following
Button b1 = CreateMyButton();
b1.Click += new EventHandler(this.MyButtonHandler);
...
void MyButtonHandler(object sender, EventArgs e) {
...
}
Use this code to handle several buttons' click events:
private int counter=0;
private void CreateButton_Click(object sender, EventArgs e)
{
//Create new button.
Button button = new Button();
//Set name for a button to recognize it later.
button.Name = "Butt"+counter;
// you can added other attribute here.
button.Text = "New";
button.Location = new Point(70,70);
button.Size = new Size(100, 100);
// Increase counter for adding new button later.
counter++;
// add click event to the button.
button.Click += new EventHandler(NewButton_Click);
}
// In event method.
private void NewButton_Click(object sender, EventArgs e)
{
Button btn = (Button) sender;
for (int i = 0; i < counter; i++)
{
if (btn.Name == ("Butt" + i))
{
// When find specific button do what do you want.
//Then exit from loop by break.
break;
}
}
}
If you want to see what button was clicked then you can do the following once you create and assign the buttons. Considering that you create the button IDs manually:
protected void btn_click(object sender, EventArgs e) {
Button btn = (Button)sender // if you're sure that the sender is button,
// otherwise check if it is null
if(btn.ID == "blablabla")
// then do whatever you want
}
You can also check them from giving a command argument to each button.
Check out this example How to create 5 buttons and assign individual click events dynamically in C#
seems like this works, while adding a tag with each element of the array
Button button = sender as Button;
do you know of a better way?
In regards to your comment saying you'd like to know which button was clicked, you could set the .Tag attribute of a button to whatever kind of identifying string you want as it's created and use
private void MyButtonHandler(object sender, EventArgs e)
{
string buttonClicked = (sender as Button).Tag;
}