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!";
}
}
Related
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);
}
}
}
}
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 would like to have a progress bar, where the value of the bar would rise if the button was being pressed down(MouseDown Event), the bar needs to rise simultaneousely.
Any ideas? I tried with a timer but this is all I have currently
private void button1_Click(object sender, EventArgs e)
{
progressBar1.PerformStep();
progressBar1.UseWaitCursor = true;
}
private void button2_Click(object sender, EventArgs e)
{
progressBar1.Value = 0;
}
public void button1_MouseDown(object sender, MouseEventArgs e)
{
timer1.Start();
}
public void button1_MouseUp(object sender, MouseEventArgs e)
{
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
}
It should look like this:
void timer_Tick(object sender, EventArgs e) {
progressBar1.PerformStep();
}
void button1_MouseDown(object sender, MouseEventArgs e) {
timer.Start();
}
void button1_MouseUp(object sender, MouseEventArgs e) {
timer.Stop();
}
The following program will update the progress bar each 20 ms the mouse is held down and get 10 100% after 2 seconds
public partial class Form1 : Form
{
private Timer pbTimer;
private int pbProgress = 0;
public Form1()
{
InitializeComponent();
pbTimer = new Timer();
pbTimer.Tick += new EventHandler(ProgressUpdate);
pbTimer.Interval = 20;
this.MouseDown += Form1_MouseDown;
this.MouseUp += Form1_MouseUp;
}
private void ProgressUpdate(object sender, EventArgs e)
{
if (pbProgress < 100)
{
progressBar1.Value = ++pbProgress;
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
pbTimer.Start();
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
pbTimer.Stop();
progressBar1.Value = 0;
pbProgress = 0;
}
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
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