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);
}
}
}
}
Related
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));
}
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.
I am new to C#.
Just doing my first project.
This is my code.
I just want to fill the textbox once the progress bar finish loading.
But its not working now.
Can anyone tell me what I am doing worong.
My code is as below.
private void button1_Click(object sender, EventArgs e)
{
this.timer1.Start();
// int n = timer1.Interval;
int m = progressBar1.Value;
if(m==0)
{
textBox2.Text = "test";
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
this.progressBar1.Increment(1);
}
}
Try This:
int progressVal=0;
private void button1_Click(object sender, EventArgs e)
{
this.timer1.Start();
// int n = timer1.Interval;
}
private void timer1_Tick(object sender, EventArgs e)
{
this.progressBar1.Increment(1);
progressVal= progressBar1.Value;
if(progressVal==progressBar1.Maximum)
{
timer1.Stop();
textBox2.Text = "Loading done!";
}
}
I'm trying to create a media player that can load MULTIPLE CLIPS at a time.
Just load. Not play.
When you load the program, I was hoping that the user can click "load" and select different files for each of the buttons.
Once the clips have all been loaded, the user can go through and click a "play" button that corresponds with the Load Button.
I also hoped that beside each "play" button, there be a Loop Checkbox (if ticked, it loops the video)
The Program so far:
Current Source:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MediaPlayer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
MediaPly _mp = null;
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
_mp = new MediaPly();
}
}
private void button2_Click(object sender, EventArgs e)
{
_mp.LoadFile(openFileDialog1.FileName, this.panel1);
}
private void button3_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
_mp = new MediaPly();
}
}
private void button4_Click(object sender, EventArgs e)
{
_mp.LoadFile(openFileDialog1.FileName, this.panel1);
}
private void button5_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
_mp = new MediaPly();
}
}
private void button6_Click(object sender, EventArgs e)
{
_mp.LoadFile(openFileDialog1.FileName, this.panel1);
}
}
}
You are saving all clips to the same variable (_mp).
Try to save each in a different variable:
MediaPly _mp = null;
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
_mp = new MediaPly();
}
}
private void button2_Click(object sender, EventArgs e)
{
_mp.LoadFile(openFileDialog1.FileName, this.panel1);
}
MediaPly _mp2 = null;
private void button3_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
_mp2 = new MediaPly();
}
}
private void button4_Click(object sender, EventArgs e)
{
_mp2.LoadFile(openFileDialog1.FileName, this.panel1);
}
MediaPly _mp3 = null;
private void button5_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
_mp3 = new MediaPly();
}
}
private void button6_Click(object sender, EventArgs e)
{
_mp3.LoadFile(openFileDialog1.FileName, this.panel1);
}
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