Button Background Image Check? - c#

Pls i have a problem with trying to check the background Image of a button. I want to delete the button if its image matches an Image in the Resource Folder. I have Tried
private void DeleteCard(Button btn)
{
if (btn.BackgroundImage.Equals( FunWaysInc.Properties.Resources.Card_1))
{
playersCards.Remove(Properties.Resources.Card_1);
MessageBox.Show(playersCards.Count.ToString());
}
else
{
MessageBox.Show("NO");
}
}
private void frmQuickSpark_Load(object sender, EventArgs e)
{
Button tstbtn = new Button();
tstbtn.BackgroundImage = Properties.Resources.Card_1;
DeleteCard(tstbtn);
}
but the message box displayed is the one that displays "NO"..
Pls what is happening????

when adding the button
button.Tag = "ToDelete";
then later
foreach (Button b in this.Controls.OfType<Button>())
{
if(b.Tag == "ToDelete")
{
//delete
}
}

Here what you have to do. You need to enumerate all your images and store pointer in Tag property of the button.
private Dictionary<string, Image> _table = new Dictionary<string, Image>();
private void frmQuickSpark_Load(object sender, EventArgs e)
{
_table.Add("Image1", Properties.Resources.Card_1);
_table.Add("Image2", Properties.Resources.Card_2);
Button btn = new Button();
SetButtonImage(btn);
DeleteCard(btn);
}
private void SetButtonImage(Button button)
{
button.BackgroundImage = _table["Image1"];
button.BackgroundImage.Tag = "Image1";
}
private void DeleteCard(Button btn)
{
if (btn.BackgroundImage.Tag == "Image1")
{
playersCards.Remove(btn); // not sure what your logic of removal
MessageBox.Show(playersCards.Count.ToString());
}
else
{
MessageBox.Show("NO");
}
}

I already found an answer to my question.. I just modified my code
private void DeleteCard(Image img)
{
playersCards.Add(Properties.Resources.Card_1);
if (img == playersCards[0])
{
playersCards.Remove(Properties.Resources.Card_1);
MessageBox.Show(playersCards.Count.ToString());
}
else
{
MessageBox.Show("NO");
}
}
private void frmQuickSpark_Load(object sender, EventArgs e)
{
Button tstbtn = new Button();
tstbtn.BackgroundImage = Properties.Resources.Card_1;
Image img = tstbtn.BackgroundImage;
DeleteCard(img);
}
It works perfectly.

Related

C# - Create dynamic buttons and events

I have a form with a tabControl and inside of each tab is a flowLayoutPanel where I can drag and drop files and a button is created for each dropped file. Afterwards when I click on a button, the file that i dropped should open. I have managed to do this for one file only.. My problem is how can I tell which button was clicked and to open the file/app stored in the path for each button.. How can I differentiate in the button_click event the clicked button and the path of the app to open?
Code for this part so far:
Process myProcess = new Process();
string path_app;
public Form1()
{
InitializeComponent();
this.DragEnter += new DragEventHandler(Form1_DragEnter);
this.DragDrop += new DragEventHandler(Form1_DragDrop);
}
void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
e.Effect = DragDropEffects.All;
}
void Form1_DragDrop(object sender, DragEventArgs e)
{
string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[];
foreach (string s in fileList)
{
Button button = new Button();
button.Click += new EventHandler(this.button_Click);
flowLayoutPanel1.Controls.Add(button);
path_app = String.Format("{0}", s);
}
}
private void button_Click(object sender, System.EventArgs e)
{
myProcess.StartInfo.FileName =path_app;
myProcess.Start();
}
Also my tabControl has the possibility to add new tabs but how can I get the selected tab and the inside flowLayoutPanel to know where to create the button?
And by the way, is there a problem of how I open the files? I understood that i have to take into consideration the working directory..
Thank you for your help!
You can utilize Tag property of the Button:
void Form1_DragDrop(object sender, DragEventArgs e)
{
foreach (String s e.Data.GetData(DataFormats.FileDrop))
{
Button button = new Button();
button.Click += new EventHandler(this.button_Click);
flowLayoutPanel1.Controls.Add(button);
path_app = String.Format("{0}", s);
// Add to Tag any data you want to pin to the button
button.Tag = path_app;
}
}
private void button_Click(object sender, System.EventArgs e)
{
// Obtain via Tag
String path_app = ((sender as Button).Tag as String);
myProcess.StartInfo.FileName = path_app;
myProcess.Start();
}
You could use button.Tag = "theFancyPath" and in the EventHandler cast the object sender as Button to access the Tag property.
If you need more then you could inherit from Button:
public class ButtonWithPathProperty : Button
{
public FileInfo PathToOpen { get; private set; }
public ButtonWithPathProperty(FileInfo path)
{
PathToOpen = path;
this.Click += new EventHandler(this.button_Click);
}
private void button_Click(object sender, System.EventArgs e)
{
var yourPath = this.PathToOpen;
}
}
This is not tested btw :)

Can't close a Popup after clicking from a user control

I have a user control (AddNewTransaction) inside a Popup (NewTransaction in DebtPage.xaml) that I open with this code:
[DebtPage.xaml.cs]
public void Button_Click_1(object sender, RoutedEventArgs e)
{
if (!NewTransaction.IsOpen) { NewTransaction.IsOpen = true; }
NewTransaction.HorizontalOffset = (Window.Current.Bounds.Width) / 2 - (New_Transaction_Grid.Width / 2);
NewTransaction.VerticalOffset = (Window.Current.Bounds.Height) / 2 - (New_Transaction_Grid.Height / 2);
NewTransaction.Visibility = Visibility.Visible;
RetanguloBranco_Background.Visibility = Visibility.Visible;
}
The Popup opens and there's a custom control (AddNewTransaction) with a "Save" button. The button suppose to save data AND close the Popup. But it just save, the Popup keeps opened.
[AddNewTransaction.xaml.cs]
private static DebtPage _debtPage;
public static DebtPage debtPage
{
get
{
if (_debtPage == null)
{
_debtPage = new DebtPage();
}
return _debtPage;
}
}
private void BotaoSave_Click(object sender, RoutedEventArgs e)
{
SalvarTransaction(); //It's working!
debtPage.ClosePopup_NewTransaction(); //It is not!
}
[DebtPage.xaml.cs]
public void ClosePopup_NewTransaction()
{
NewTransaction.IsOpen = false;
NewTransaction.Visibility = Visibility.Collapsed;
RetanguloBranco_Background.Visibility = Visibility.Collapsed;
}
Thanks!
I've solved the problem with this:
private void BotaoSave_Click(object sender, RoutedEventArgs e)
{
SalvarTransaction();
Grid gridParent = this.Parent as Grid;
Popup parent = gridParent.Parent as Popup;
if (parent != null)
{
parent.IsOpen = false;
}
}
But I still want to know why ClosePopup_NewTransaction(); does nothing.

How can I refactor this C# code below?

I have 12 buttons in my Form1, and each button has a textbox next to it. The button event calls a method called dialogueOpen which handles getting the an object from form2 and placing a string value in a textbox.
How can I place the value returned in a textbox depending on what button the user clicked on? So if it is button1 a user clicked on, then the text returned should be placed in textbox1 and if it is button2 the user clicked on then the text returned should be placed in textbox2. The point is avoid using a string name to check as the buttons can all be called "browse".
Right now my code below does that but it is quite repetitive is there is a better of doing this?
private void dailogueOpen(String btnName)
{
if (listBox1.SelectedItem == null)
{
MessageBox.Show("Please Select a form");
}
else
{
var selectedItem = (FormItems)listBox1.SelectedItem;
var form2result = new Form2(myDataSet, selectedItem);
var resulOfForm2 = form2result.ShowDialog();
if (resulOfForm2 == DialogResult.OK)
{
switch (btnName)
{
case "btn1":
textBox1.Text = form2result.getValue();
break;
case "btn2":
textBox2.Text = form2result.getValue();
break;
case "btn3":
textBox3.Text = form2result.getValue();
break;
case "btn4":
textBox4.Text = form2result.getValue();
break;
case "btn5":
textBox5.Text = form2result.getValue();
break;
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
String name = "btn1";
dailogueOpen(name);
}
private void button2_Click(object sender, EventArgs e)
{
String name = "btn2";
dailogueOpen(name);
}
private void button3_Click(object sender, EventArgs e)
{
String name = "btn3";
dailogueOpen(name);
}
private void button4_Click(object sender, EventArgs e)
{
String name = "btn4";
dailogueOpen(name);
}
private void button5_Click(object sender, EventArgs e)
{
String name = "btn5";
dailogueOpen(name);
}
EDIT: I just noticed your event handlers. More refactoring ensues:
Yes, there is. You need to somehow associate textboxes to buttons. For example, create a dictionary like so:
Dictionary<Button, TextBox> _dict;
_dict[button1] = textBox1;
_dict[button2] = textBox2;
...
Use one event handler for all events:
private void button_click(object sender, EventArgs e)
{
dialogeOpen((Button)sender);
}
Change dialogueOpen to accept a Button instead of a string and
_dict[btn].Text = form2Result.getValue();
replace your eventhandlers to
private void ButtonClick(object sender, EventArgs e)
{
var button = sender as Button;
if (button == null) return;
String name = button.Text;// Tag, name etc
dailogueOpen(name);
}
1 You use the same delegate on all button
Nota (Thank's to Marty) : When You're in the Form Designer, select all buttons, and then assing then "Generic_Click" for all of them, or you can use code below.
this.btn1.Click += new System.EventHandler(Generic_Click); //the same delegate
this.btn2.Click += new System.EventHandler(Generic_Click);
this.btn3.Click += new System.EventHandler(Generic_Click);
....
private void Generic_Click(object sender, EventArgs e)
{
var control = (Button)sender;
if( control.Name == "btn1")
{
....
}
else if( control.Name == "btn2")
{
....
}
else if( control.Name == "btn3")
{
....
}
}
I would first use just one event handler for the buttons, it would look like this:
protected void ButtonClick(object sender, EventArgs e)
{
Button clickedButton = (Button) sender;
string selectedId = clickedButton.ID;
string[] idParameters = selectedId.Split('_');
string textBoxId = "textbox" + idParameters[1];
dailogueOpen(textBoxId);
}
What I did here is use a pattern for the names of the textboxes, so for instance if you have buttons with ids like: button_1 ,button_2, ..., button_n, you can infer what the corresponding textbox is.
If you click button_1, by spliting its id you'll know that its corresponding textbox is the one whose id is textbox1.
Then the dialogueOpen function would look like this:
private void dailogueOpen(string textBoxId)
{
if (listBox1.SelectedItem == null)
{
MessageBox.Show("Please Select a form");
}
else
{
var selectedItem = (FormItems)listBox1.SelectedItem;
var form2result = new Form2(myDataSet, selectedItem);
var resulOfForm2 = form2result.ShowDialog();
if (resulOfForm2 == DialogResult.OK)
{
TextBox textBox = (TextBox)this.Form.FindControl("MainContent").FindControl(textBoxId);
textBox.Text = resulOfForm2.getValue();
}
}
Where MainContent is the id of container where the textboxes are.
All in all:
I would use a pattern for button and texboxes id.
According to the button being clicked I infer its corresponding texbox id.
Then find the texbox and update its value.
You can have dictionary and one event method on all button clicks
Dictionary<Button, TextBox> dx = new Dictionary<Button, TextBox>;
private void ButtonClick(object sender, EventArgs e)
{
var button = sender as Button;
if (button == null) return;
dx[button].Text = form2result.getValue();
}
and constructor like this:
public ClassName()
{
dx.Add(button1, textBox1);
dx.Add(button2, textBox2);
dx.Add(button3, textBox3);
}
I think the first thing you can do is improve readability by removing the need for the switch statement:
private void dailogueOpen(TextBox textBox)
{
if (listBox1.SelectedItem == null)
{
MessageBox.Show("Please Select a form");
}
else
{
var selectedItem = (FormItems)listBox1.SelectedItem;
var form2result = new Form2(myDataSet, selectedItem);
var resulOfForm2 = form2result.ShowDialog();
if (resulOfForm2 == DialogResult.OK)
{
textBox.Text = form2result.getValue();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
dailogueOpen(textBox1);
}
private void button2_Click(object sender, EventArgs e)
{
dailogueOpen(textBox2);
}
private void button3_Click(object sender, EventArgs e)
{
dailogueOpen(textBox3);
}
private void button4_Click(object sender, EventArgs e)
{
dailogueOpen(textBox4);
}
private void button5_Click(object sender, EventArgs e)
{
dailogueOpen(textBox5);
}
This then gives you a reasonable method signature to introduce the dictionary (suggested by two other people) to map Button to TextBox, which would in turn allow you to use a single event handler (suggested by two other people) for all buttons.
private void button_Click(object sender, EventArgs e)
{
Button button = sender as Button;
if (button == null) return;
String name = button.Text;// Tag, name etc
dailogueOpen(name);
}
private void dailogueOpen(String btnName)
{
if (listBox1.SelectedItem == null)
{
MessageBox.Show("Please Select a form");
}
else
{
var selectedItem = (FormItems)listBox1.SelectedItem;
var form2result = new Form2(myDataSet, selectedItem);
var resulOfForm2 = form2result.ShowDialog();
if (resulOfForm2 == DialogResult.OK)
{
SetTxt(btnName,form2result.getValue());
}
}
}
private void SetTxt(string btnName, string value)
{
int lenght = "Button".Length;
string index = btnName.Substring(lenght); //remove Button
TextBox t = (TextBox)this.Controls.Find("textBox" + index, true)[0];
if (t != null)
t.Text = value;
}

Creating a new form, switching focus between the new and the old form with a button

I have a form and I want to get an instance of the same form as stated in the code below. And I have a button: every time I press this button, if a new form is created, I want it to focus to that window, if not, I want to create a new form.
I managed to create a new form but if I want to focus on it, the code did not work, any ideas?
private void btn_Click(object sender, EventArgs e)
{
if (opened == false)
{
Text = "form1";
var form = new myformapp();
form.Show();
opened = true;
form.Text = "form2";
}
else
{
if (Application.OpenForms[1].Focused)
{
Application.OpenForms[0].BringToFront();
Application.OpenForms[0].Focus();
}
if (Application.OpenForms[0].Focused)
{
Application.OpenForms[1].BringToFront();
Application.OpenForms[1].Focus();
}
}
}
You can try shortening your code without the need to introduce more variables with this example:
void button1_Click(object sender, EventArgs e) {
bool found = false;
for (int i = 0; i < Application.OpenForms.Count; ++i) {
if (Application.OpenForms[i].GetType() == typeof(myformapp) &&
Application.OpenForms[i] != this) {
Application.OpenForms[i].Select();
found = true;
}
}
if (!found) {
myformapp form = new myformapp();
form.Show();
}
}
Updated code from Francesco Baruchelli's comment.
If I understand correctly what you are trying to do, you can keep a static List with the opened forms. Everytime an instance of your Form is opened you add it to the List, and everytime it is closed you remove it. The when you press the button you can check the size of the List. If it is 1 you create a new Form, open it and set the focus on it. If the size is already 2, you look in the List for the instance which is different from the one executing the click event. The code could be something like this:
private static List<Form1> openForms = new List<Form1>();
private void button1_Click(object sender, EventArgs e)
{
Form1 frm = null;
if (openForms.Count == 2)
{
foreach (Form1 aForm in openForms)
if (aForm != this)
{
frm = aForm;
break;
}
}
else
{
frm = new Form1();
frm.Show();
}
frm.Focus();
}
private void Form1_Load(object sender, EventArgs e)
{
openForms.Add(this);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
openForms.Remove(this);
}

c# . if (image == Properties.Resources.image)

I want to do an if stament on an image
if (SortName.Image == Properties.Resources.RadioEmpty)
{
SortName.Image = Properties.Resources.Radio;
}
else
{
SortName.Image = Properties.Resources.RadioEmpty;
}
but its on working any idea what I'm doing wrong ?
o.k additional information
1.
//SortName = A picture box
//Properties.Resources.RadioEmpty = Resources\RadioEmpty.png
//Properties.Resources.Radio = Resources\Radio.png
2.
Nope no errors
3.I wanted to use a custom image for a radio button. A have a picture box with the above code on click. RadioEmpty is the default so I check to see if the picturebox's image is the same as image form the Resources folder is so do code.
i advise you use tag for this issue see this code
private void Form1_Load(object sender, EventArgs e)
{
//in form load the radio is checked or unckecked
//here my radio is unchecked at load
pictureBox1.Image = WindowsFormsApplication5.Properties.Resources.Add;
pictureBox1.Tag = "UnChecked";
}
private void pictureBox1_Click(object sender, EventArgs e)
{
//after pictiurebox clicked change the image and tag too
if (pictureBox1.Tag.ToString() == "Checked")
{
pictureBox1.Image = WinFormsApplication.Properties.Resources.Add;
pictureBox1.Tag = "UnChecked";
}
else
{
pictureBox1.Image = WinFormsApplication.Properties.Resources.Delete;
pictureBox1.Tag = "Checked";
}
}
compare the names. Something like this (unverified)
if (SortName.Image.Name.Equals(Properties.Resources.RadioEmpty.Name))
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap bm1;
Bitmap bm2;
private void button1_Click(object sender, EventArgs e)
{
bm1 = new Bitmap(Properties.Resources.firegirl1);
bm2 = new Bitmap(Properties.Resources.Zemli2);
pictureBox1.Image = bm1;
pictureBox2.Image = bm2;
if (pictureBox1.Image==pictureBox2.Image)
{
MessageBox.Show("Some");
}
else
{
MessageBox.Show("Differ");
}
}
}

Categories

Resources