Modifying picturebox on click [C#] - c#

I have an array of pictureboxes and they all have the same onclick method. The method is supposed to edit the picturebox.
My code looks like this
box.Click += new System.EventHandler(boxClick);
private void boxClick(object sender, EventArgs e)
{
sender.Image = brush.CurrentImage;
}
Not very complex, but for some reason the IDE is telling me that object is not containing a definition for Image.
But sender definitely has a Image property (I can even see it when debugging...)
Can someone please tell me what I'm doing wrong? I'm sure it's possible to change a control's property on click...
Thanks

Description
Right, object has no property .Image
Sample
You have to cast the sender to PictureBox like this
private void boxClick(object sender, EventArgs e)
{
(sender as PictureBox).Image = brush.CurrentImage;
}

sender is an object, which does not have the property Image.
You have to cast the object sender to a PictureBox, and then you can access its Image property.
private void boxClick(object sender, EventArgs e)
{
(sender as PictureBox).Image = brush.CurrentImage;
}

Related

How to get tab index from click on textbox

I have a problem. I want to get a tabIndex of textbox, when I click on him.
The problem is ,that I need to use private void klik(object sender, EventArgs e) and there is nothing like TabControl1.SelectedTab which I found out on google. Thanks for help.
In this case, your sender object is of the type TextBox, so you can cast it into a new TextBox object, and access it's tabindex.
private void textBox1_Click(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
int tabIndex = tb.TabIndex;
}

adding an action for click event on picturebox in c#

I get a pictureboxe and try to add it an action to change background Image upon click event happen, but there is no action. So, this is the code:
pieces bishopBB = new pieces();
public Form1()
{
// object of picturebox
bishopBB.Location = new Point(300, 455);
bishopBB.Parent = this;
bishopBB.Click += new System.EventHandler(pictureboxes_Click)
InitializeComponent();
}
private void pictureboxes_Click(object sender, EventArges e)
{
backgroundImage = Properties.Resources.black;
}
Looking at the name and other indicators I would assume (and hope) that pictureboxes_Click is a common click handler for many PictureBoxes.
So to access the one that has been clicked you need to cast sender to PictureBox and then set the BackgroundImage:
private void pictureboxes_Click(object sender, EventArges e)
{
((PictureBox)sender).BackgroundImage = Properties.Resources.black;
}
I'm a little amazed though that your spelling of backgroundImage compiles. The correct spelling BackgroundImage refers to the current class, usually the Form and it should also show, unless your Form has a black background already..
To change the current image of the PictureBox control, you need to refer to the control and reference the BackgroundImage property (not 'backgroundImage'):
private void pictureboxes_Click(object sender, EventArges e)
{
this.pictureboxes.BackgroundImage = Properties.Resources.black;
}
To change the background image of the form:
private void pictureboxes_Click(object sender, EventArgs e)
{
this.BackgroundImage = Properties.Resources.black;
}
To do this you can use the on_click property of the picturebox. You can then use the like picbox.image = whatever the image location is

custom tooltip in c# .net win forms

I am looking to simulate a custom tooltip the like of you see in websites using c# .NET 4.5 windows forms.This tooltip will basically show status of some Tasks like how many tasks are pending,tasks in process, completed etc.To do this i am using a borderless win form.This winform will have some texts, images etc.I want it to reveal itself on button's mouseHover event and disappear on MouseLeave event.My problem is that on Mousehover event numerous instances of that tooltip form is getting generated and on MouseLeave they are not getting closed.My code is
private void B_MouseHover(object sender, EventArgs e)
{
frmSecQStatToolTipDlg tooltip = new frmSecQStatToolTipDlg();
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
frmSecQStatToolTipDlg tooltip = new frmSecQStatToolTipDlg();
tooltip.Close();
}
My code is not working, hence please tell me how to do this the correct way.Thanks
You're generating a new instance of the form class every time you get a hover event, and every time you get a leave event. If you want to continue to use this approach I would recommend you use a variable on your main form object to store the reference to your tooltip form. Secondly, you need to not generate a new instance whenever the event handler is called, but only when necessary. I would create your instance the first time your Hover event is called for a particular control, and then dispose of it when your Leave handler is called -- this is under the assumption that the tooltip dialog's constructor loads up different information for each control being hovered over. Like so:
frmSecQStatToolTipDlg f_tooltip;
private void B_MouseHover(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg == null)
{
f_tooltip = new frmSecQStatToolTipDlg();
}
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
if(f_tooltip != null)
{
f_tooltip.Close();
f_tooltip = null;
}
}
You should keep a global field for this form, and should not dispose or close it. Just hide it on some events and show again.
Sample Code:
frmSecQStatToolTipDlg tooltip;
private void B_MouseHover(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg == null)
{
tooltip = new frmSecQStatToolTipDlg();
}
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg != null)
{
tooltip.Hide();
}
}
With this logic you'll not have to create tooltip instance again and again and it will not take time to popup if you frequently do this activity.
Declare your tooltip once as readonly and use it without asking anytime if it is null or not.
If you need to Dispose it, implement the IDisposable pattern:
https://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.110).aspx
private readonly frmSecQStatToolTipDlg _tooltip = new frmSecQStatToolTipDlg() ;
private void B_MouseHover(object sender, EventArgs e)
{
_tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
_tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
_tooltip.Hide();
}

How can I make a pictureBox that, when clicked, will display some text to a label?

I'm using Visual Studio, so I already have drag-and-dropped some pictureBoxes in there, and they have names and pictures. I just want to be able to click them and have a label say the name of the person in the picture that was clicked.
This is what I currently have (for the pictureBox only)
private void captainFalcon_Click(object sender, EventArgs e)
{
Label.Show("Captain Falcon");
}
FINAL EDIT: Everything is working now, I just followed everyone's suggestions!
Is this what you need?
private void captainFalcon_Click(object sender, EventArgs e)
{
Label.Text = "Captain Falcon";
}
Something like below will work.
private void pictureBox1_Click(object sender, EventArgs e)
{
label1.Text = "Captain Falcon";
}
Obviously change the control names (pictureBox1 and label1) to match yours.
Use
private void captainFalcon_Click(object sender, EventArgs e)
{
xxxxxx.Text ="Captain Falcon";
}
and instead of xxxxx use your label name.
Select your label on designer, look at its properties, and use its "(Name)" property.
For example if your label name is characterName, then your code will be
characterName.Text ="Captain Falcon";

How do I change the image of a PictureBox when a CheckBox is checked?

I am busy making a simple game in Visual C#, and I have no idea how to do this. Is there a way to set a PictureBox's image when a CheckBox is checked? What is the actual code for setting the image?
Thanks,
Varmitharen
It depends how you've got your image, but you can use:
this.pictureBox.Load(imageFileName);
or
this.pictureBox.Image = image;
where image is of type Image
Just to complete the answer:
this.checkbox.CheckedChanged += new EventHandler(Checkbox_CheckedChanged);
private void Checkbox_CheckedChanged(object sender, EventArgs e)
{
// Change image here
}
There is a CheckedChanged event of a CheckBox that you can subscribe to with a handler that changes the image. Link: CheckBox.CheckedChanged
this.MyCheckbox.CheckedChanged += new EventHandler(MyCheckbox_CheckedChanged);
private void MyCheckbox_CheckedChanged(object sender, EventArgs e)
{
this.MyPictureBox.Image = // Your image here
}

Categories

Resources