Get an image from resources of project - c#

I have RadioButton which has a background. Now I need to change background on MouseEnter event. I can do that with
private void button_MouseEnter(object sender, EventArgs e)
{
button.BackgroundImage = Image.FromFile("D:/img/sample.png");
}
But I already have that image as resource in project and I don't know how to get to it.

try as follow:-
private void button_MouseEnter(object sender, EventArgs e)
{
button.BackgroundImage = <YourNameSpace>.Properties.Resources.<ResourceName>;
}

In case you use winforms, include the images to your resource:
public Form1()
{
InitializeComponent();
button1.MouseEnter += new EventHandler(button1_MouseEnter);
button1.MouseLeave += new EventHandler(button1_MouseLeave);
}
void button1_MouseLeave(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img1));
}
void button1_MouseEnter(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img2));
}

Related

Upload Code to Arduino UNO with a C# Form

I build a project in C# with which one can control a RGB led. My goal is to build a function in which one can upload code that controls the LED. Therefore I need help with the following proble :
There is a button and a rich text box on the form. The text of the rich text box contains source code, which should get uploaded to the arduino if you press the button. I know how to open the port but i dont know how to upload the source code.
My code :
namespace RGBLedColorPicker
{
public partial class Form1 : Form
{
SerialPort port = new SerialPort("COM3", 9600);
private Color defaultColor = Color.FromArgb(0, 0, 0);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
port.Open();
this.SetColor(defaultColor);
}
catch
{
MessageBox.Show("Cant find Arduino.Closing programm");
Application.Exit();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
this.SetColor(defaultColor);
port.Close();
}
private void panel1_Click(object sender, EventArgs e)
{
}
private void SetColor(Color color)
{
port.Write(new[] { color.R, color.G, color.B }, 0, 3);
}
private void button1_Click(object sender, EventArgs e)
{
this.SetColor(Color.Red);
}
private void button2_Click(object sender, EventArgs e)
{
this.SetColor(Color.Green);
}
private void button3_Click(object sender, EventArgs e)
{
this.SetColor(Color.Blue);
}
private void button4_Click(object sender, EventArgs e)
{
this.SetColor(Color.Red);
Thread.Sleep(2000);
this.SetColor(Color.Green);
Thread.Sleep(2000);
this.SetColor(Color.Blue);
Thread.Sleep(2000);
MessageBox.Show("Show beendet","beendet",MessageBoxButtons.OK);
this.SetColor(defaultColor);
}
private void button5_Click(object sender, EventArgs e)
{
using (Create f2 = new Create())
{
f2.ShowDialog(this);
}
}
private void button6_Click(object sender, EventArgs e)
{
this.SetColor(defaultColor);
}
private void button7_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
SetColor(colorDialog1.Color);
}
}
}
}

How can I amend a record that I have inputting?

I am creating a To Do List using Windows Forms.
This is what I have so far.
The code for this form is as follows
namespace To_Do_List
{
public partial class To_Do_List : Form
{
public To_Do_List()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e) //this is the exit button on the toolstrip
{
Application.Exit(); //exits the program when the Exit button is clicked in dropdown menu
}
private void button4_Click(object sender, EventArgs e)//This creates the button to open the about form
{
AboutMyProgram aboutForm = new AboutMyProgram();
aboutForm.ShowDialog(); //opens about form
}
private void button3_Click(object sender, EventArgs e) //This creates the button to open the form which ammends tasks
{
AmmendItem CreateForm = new AmmendItem(this);
CreateForm.Show();
}
private void button1_Click(object sender, EventArgs e) // This creates the button to open the form which creates new tasks
{
AddItem CreateForm = new AddItem(this);
CreateForm.Show();
}
public void listView1_SelectedIndexChanged_1(object sender, EventArgs e) // This creates the table
{
}
private void button2_Click(object sender, EventArgs e) //This Allows the user to delete entries
{
if (listView1.SelectedItems != null)
{
var confirmation = MessageBox.Show(
"Are you sure you want to delete this?",
"WARNING", MessageBoxButtons.YesNo, MessageBoxIcon.Question
);
if (confirmation == DialogResult.Yes)
{
for (int i = listView1.Items.Count - 1; i >= 0; i--)
{
if (listView1.Items[i].Selected)
{
listView1.Items[i].Remove();
i--;
}
}
}
}
}
}
}
To add a task, the form looks like this
And again, the code is as follows
namespace To_Do_List
{
public partial class AddItem : Form
{
To_Do_List home;
public AddItem(To_Do_List parent)
{
InitializeComponent();
home = parent;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label1_Click_1(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void openListToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void sortByDateToolStripMenuItem_Click(object sender, EventArgs e)
{
}
public void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
}
public void textBox1_TextChanged(object sender, EventArgs e)//Title Box
{
}
public void /*Description of Task*/richTextBox1_TextChanged(object sender, EventArgs e)
{
}
public void /*Priority Box*/comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close(); //causes the window to close but keeps the application running
}
private void aboutToDoListToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutMyProgram aboutForm = new AboutMyProgram();
aboutForm.ShowDialog(); //opens about form displaying copyright information
}
public void button1_Click(object sender, EventArgs e) //create task button
{
ListViewItem item1 = new ListViewItem(Title.Text);
item1.SubItems.Add(Description.Text);
item1.SubItems.Add(Priority.Text);
item1.SubItems.Add(Date.Text);
string value = "";
bool isChecked = Completed.Checked;
if (isChecked)
item1.SubItems.Add(Completed.Text);
else
value = null;
home.listView1.Items.Add(item1);
this.Close();
}
private void Date_ValueChanged(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e) //Closes the window
{
this.Close();
}
}
}
The Amend form is exactly the same as the New Task form. How would I go about being able to select a record, pressing the amend button and actually changing?
I'm pretty stumped.
Also, this isn't really part of this question but I'll ask it just in case someone knows.
How would I go about being able to actually save these records so that they are there when I open the application back up again?
Thank you very much for reading, I know that I have just dumped everything but I'm really new to Windows Forms so I didn't want to accidentally miss something important out.
Edit
Am I on the right road with something like this?
public void button1_Click(object sender, EventArgs e)
{
To_Do_List editform = new To_Do_List(Title.Text);
editform.Description.Text = listView1.SelectedItems[0].SubItems[0].Text;
Still can't get it to work :/
This code for popup window.
public partial class frmToDoDetails : Form
{
public string TaskTitle { get; set; }
public string Description { get; set; }
public bool EditMode { get; set; }
public frmToDoDetails()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
TaskTitle = txtTitle.Text;
Description = txtDesc.Text;
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
private void frmToDoDetails_Load(object sender, EventArgs e)
{
if (EditMode)
{
txtTitle.Text = TaskTitle;
txtDesc.Text = Description;
}
else {
txtTitle.Text = string.Empty;
txtDesc.Text = string.Empty;
}
txtTitle.Focus();
}
}
And this for list
public partial class frmToDo : Form
{
public frmToDo()
{
InitializeComponent();
}
private void btnNew_Click(object sender, EventArgs e)
{
frmToDoDetails frm = new frmToDoDetails();
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string[] arr = new string[2];
ListViewItem itm;
arr[0] = frm.TaskTitle;
arr[1] = frm.Description;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
}
private void frmToDo_Load(object sender, EventArgs e)
{
listView1.View = View.Details;
listView1.GridLines = true;
listView1.FullRowSelect = true;
//Add column header
listView1.Columns.Add("Title", 100);
listView1.Columns.Add("Desc", 70);
}
private void listView1_DoubleClick(object sender, EventArgs e)
{
ListViewItem currentItem= listView1.SelectedItems[0];
frmToDoDetails frm = new frmToDoDetails();
frm.TaskTitle = currentItem.SubItems[0].Text;
frm.Description = currentItem.SubItems[1].Text;
frm.EditMode = true;
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
currentItem.SubItems[0].Text=frm.TaskTitle;
currentItem.SubItems[1].Text=frm.Description;
}
}
}
I am not added your complete fields(priority, date,etc..).
I hope it will help you.

How to stop GIF at specific Frames in winfoms

basically my question is how to stop GIF at specific frame.
i have a picture box that is already set its image with GIF and i created a 2 events Enter and Leave
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
pictureBox1.Image = Properties.Resources.LOGODEFAULT1;
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.Image = Properties.Resources.LOGOLEAVE1;
}
what i want is to stop the GIF at last frame(all gif's consist of 7 frames)
i tried the ImageAnimator and ImageStop
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
ImageAnimator.Animate(pictureBox1.Image, OnFrameChanged1);
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
ImageAnimator.StopAnimate(pictureBox1.Image, OnFrameChanged2);
}
private void OnFrameChanged1(object sender, EventArgs e)
{
pictureBox1.Image = Properties.Resources.LOGODENTER;
}
private void OnFrameChanged2(object sender, EventArgs e)
{
pictureBox1.Image = Properties.Resources.LOGOLEAVE1;
}
I think you can use the Image.SelectActiveFrame Method.

How can I add (append) a word to the same sentence, using one button (click) per word

I greatly appreciate any help. I have 20+ buttons, each with a word, or a space or period. Each time I click on a button, the pre-existing word is wiped out and replaced with the new word. I need each word and/or space to remain in place until I click the "Clear" button.
Maybe this has been previously asked/answered under different search terms? I tend to believe I need to identify a string variable, but have no idea how to begin.
==============
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class frmSentenceBuilder : Form
{
public frmSentenceBuilder()
{
InitializeComponent();
}
private void frmSentenceBuilder_Load(object sender, EventArgs e)
{
}
private void btnA_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnA.Text;
}
private void btn_a_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btn_a.Text;
}
private void btnAn_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnAn.Text;
}
private void btn_an_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btn_an.Text;
}
private void btnThe_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnThe.Text;
}
private void btn_the_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btn_the.Text;
}
private void btnman_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnman.Text;
}
private void btnwoman_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnwoman.Text;
}
private void btndog_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btndog.Text;
}
private void btncat_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btncat.Text;
}
private void btncar_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btncar.Text;
}
private void btnbicycle_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnbicycle.Text;
}
private void btnbeautiful_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnbeautiful.Text;
}
private void btnbig_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnbig.Text;
}
private void btnsmall_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnsmall.Text;
}
private void btnstrange_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnstrange.Text;
}
private void btnlookedat_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnlookedat.Text;
}
private void btnrode_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnrode.Text;
}
private void btnspoketo_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnspoketo.Text;
}
private void btnlaughedat_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnlaughedat.Text;
}
private void btndrove_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btndrove.Text;
}
private void btnSpace_Click(object sender, EventArgs e)
{
lblSentenceText.Text = " ";
}
private void btnperiod_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnperiod.Text;
}
private void btnexclam_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnexclam.Text;
}
private void btnClear_Click(object sender, EventArgs e)
{
lblSentenceText.Text = "";
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Use lb1SentenceText.Text+=whatever.Text;.
+= is equivalent to lbSentence.Text = lblSentenceText.Text + whatever.Text.
Basically, it appends or concatenates the right hand side string to the string on the left hand side. Hope it makes sense?
So:
string rhs="Hello " ;
string lhs = "World";
string rhs = rhs + lhs;//Hello World
Please be inspired. You have a lot of redundant code.
btnA.Click += AppendButtonText;
btn_a.Click += AppendButtonText;
...
private void AppendButtonText(object sender, EventArgs e)
{
var button = sender as Button;
if (button != null)
{
lblSentenceText.Text += button.Text;
}
}
On the button click events, change it to += instead of = (except in the clear button). This is equivalent to writing something = something + newValue;.
Try:
lb1SentenceText.Text = lb1SentenceText.Text + *something*.text
The += operator means add to, and the variable modified is equal to the added (Int, String) appended to the original value (x = 1; x += 3; x is now 4)
Try this:
button.Click += new System.EventHandler(ButtonClick);
button1.Click += new System.EventHandler(ButtonClick);
// And for each button, one of those.
private void ButtonClick(object sender, System.EventArgs e)
{
// Do whatever you want to do here, you can place the TEXT to be appended on the button, if so:
lb1SentenceText.Text += sender.Text;
}
//Simple.Create a global variable and within each button click event do this;
string yourStrVar = ""; //Must be global
yourStrVar+= ((Button)sender).Text

Retrieve data from one form and use it in another form

Alrighty. Here is my problem. I have just about everything done. I just need to take input from the form, and then use it in an algorithm in the second form. I have everything else written up, I just need to know how to connect the 2 so I can write up the last of the code. I've done some research, but none of it seems to go with what I'm trying to do.
Here is the main form.
namespace Airplanes
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
}
private void Arrival_Click(object sender, EventArgs e)
{
ArrivalForm newForm;
newForm = new ArrivalForm();
newForm.ShowDialog();
}
private void Fuel_Click(object sender, EventArgs e)
{
Fuelform newForm2;
newForm2 = new Fuelform();
newForm2.ShowDialog();
}
private void Status_Click(object sender, EventArgs e)
{
}
private void Items_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void NameBox_TextChanged(object sender, EventArgs e)
{
}
private void FuelBox_TextChanged(object sender, EventArgs e)
{
}
private void GateBox_TextChanged(object sender, EventArgs e)
{
}
private void Singlebutton_CheckedChanged(object sender, EventArgs e)
{
}
private void PrivateButton_CheckedChanged(object sender, EventArgs e)
{
}
private void CommercialButton_CheckedChanged(object sender, EventArgs e)
{
}
}
}
And here is the form I'm trying to connect to the main form.
namespace Airplanes
{
public partial class Fuelform : Form
{
public Fuelform()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Fuelform_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Thanks in advance for any answers.
There are a couple of ways...the easiest would probably be to pass the data in through the constructor of your new form.
FuelForm newForm2 = new FuelForm(myData);
And then change the constructor for your FuelForm:
public FuelForm(int myData) // or whatever data type you need
{
// Deal with myData
}
In Source form
destinationForm df = new destinationForm ();
df .myValue= "My Value";
df .ShowDialog();
in Destination Form
private string destVariable;
public string myValue
{
get { return destVariable; }
set { destVariable= value; }
}
then you can use destVariable everywhere in destination form

Categories