Change button image

In Form1, I have several buttons with a similar image on them to indicate a specific object, say, a tennis court. However, let's say now I click on another button in a different form to order this particular court, how can I change the image of the button on Form1 to another image to show that it is booked?

+4
source share
4 answers

You can use for this event.

The reservation action will result in an event that indicates that the property has been booked.
Form1 will have an event handler registered on it and change the button image to display the state of the object.

Edit (how to do this with events):

public class FacilityStateChangeEventArgs : EventArgs { public FacilityStateChangeEventArgs(bool booked) { this.Booked = booked; } public bool Booked { get; protected set; } // ... other properties if you need them } public class Facility { private bool booked = false; public bool Booked { get { return this.booked; } protected set { if (this.booked == value) return; // Changes the state and fires the event. this.booked = value; FireChange(); } } public event EventHandler<FacilityStateChangeEventArgs> StateChange; // You will use this method when booked gets changed public void FireChange() { if (this.StateChange != null) this.StateChange(this, new FacilityStateChangeEventArgs(this.Booked)); } } // The form with the image button. public class FormWithButton { Button button1 = new Button(); public void Whatever() { // You will get the facility from your bussiness instances. Facility facility = new Facility(); facility.StateChange += new EventHandler<FacilityStateChangeEventArgs>(facility_StateChange); } void facility_StateChange(object sender, FacilityStateChangeEventArgs e) { if (e.Booked) button1.Image = null; // booked image else button1.Image = null; // free image } } 
+1
source

change the button image property to display another resource. For instance, /

  using namespace.Properties; namespace namespace { private void button1_Click(object sender, EventArgs e) { button1.Image = Resources.pictureName; } } 

You can save information about whether this court is booked in the database, and then return the image depending on the bool field in the database.

EG you have a database with a table called a court, since the fields are id (pk), name and isBooked (bool)

when loading the page you could

  sqlconnection con = new sqlconnection("insert connstring here"); sqlcommand com = new sqlcommand("select isBooked from court where id = @id", con); con.open(); sqldatareader reader = com.executereader(); while(reader.read()) { bool booked = (bool)reader["isBooked"]; } if(booked = true) //one picture as above else //another picture 

Forgive me for the inaccurate code, this is just an example

0
source

Well, I assume that you have launched the reservation form from Form 1, where you show the button with the court. Thus, the code will look something like this in Form1 (where you have the image button in court):

 FormBooking frm = new FormBooking(); frm.Controls["nameofbooking_button"].Click += (se,ev) =>{ //The button with the court image CourtImageButton.Image = //Your new image } frm.Show(); 

Now, when this button is pressed on the reservation form, you will see an image button with the image of the vessel.

And if its 2005, which is .Net 2.0, so we don’t have lambda, so here is the code:

 FormBooking frm = new FormBooking(); frm.Controls["nameofbooking_button"].Click += new EventHandler(ChangeImage); frm.Show(); 

then some where in your class Form1:

 private void ChangeImage(object sender, EventArgs e) { //The button with the court image this.CourtImageButton.Image = Image.FromFile(@"C:\courtbooked.png"); } 
0
source

just use this if you are not communicating with the database

Form one button Click to display another form here form2

  Form2 frmtwo = new Form2(this); frmtwo.ShowDialog(); 

then in the second form constructor add this

  Form Frmtwo; public Form2(Form frm) { InitializeComponent(); Frmtwo = frm; } 

then add this code to the button where you want to display the image in the first form

 PictureBox pc = (PictureBox)Frmtwo.Controls["pictureBox1"]; pc.ImageLocation = @"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\1.jpg"; 
0
source

Source: https://habr.com/ru/post/1339547/


All Articles