I am creating a clock for clocking into a business. Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
String Code;
String Name;
String InOut;
Boolean Luke = true;
String csvPath = "C:/users/luke/documents/C#/csvProject.csv";
StringBuilder Header = new StringBuilder();
StringBuilder csvData = new StringBuilder();
public Form1()
{
InitializeComponent();
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
TopMost = true;
Header.AppendLine("Timestamp, Name");
File.AppendAllText(csvPath, Header.ToString());
textBox1.Font = new Font("Arial", 30, FontStyle.Bold);
}
private void button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
Code = Code + button.Text;
textBox1.Text = Code;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
FormBorderStyle = FormBorderStyle.Sizable;
WindowState = FormWindowState.Normal;
TopMost = false;
}
}
private void button13_Click(object sender, EventArgs e)
{
//clear
Code = null;
textBox1.Text = Code;
}
private void button10_Click(object sender, EventArgs e)
{
//in or out
DateTime timeStamp = DateTime.Now;
if (Code == "123")
{
Name = "Luke";
}
Button button = (Button)sender;
csvData.AppendLine(timeStamp + "," + Name + "," + button.Text);
File.AppendAllText(csvPath, csvData.ToString());
Code = null;
textBox1.Text = Code;
}
private void button14_Click(object sender, EventArgs e)
{
}
}
}
My layout consists of a number pad, in button, and out button. When the user presses the in button after they enter their code, the program should write in the CSV file: Timestamp, Name, In. When I tested the code by clocking in, the program writes one row correctly. When I clock in and then clock out, it creates two rows of me clocking in and one row of me clocking out. I was wondering if anyone could help me find what is going wrong in the code. Thanks.
You need to empty csvData after writing it to the file.
Related
I am using the EasyModbus library from GitHub to communicate to the PLC, I tried the required functions and I accomplish to run them on visual studio 2019 individually, and worked fine.
I did a basic program to switch on and off an output of the PLC by using two buttons. And also reading/monitoring an output from the same PLC by changing two colors(yellow On and red Off) on C# form1.
Then, when I click the on and off buttons they work fine but on the other hand, the reading/monitoring function didn't work. I need someone to illustrate some modifications how to run these functions simultaneously and at the same time changing the on/off status of the output and also reading the status of an output of the PLC.
Thank you
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using EasyModbus;
using System.Windows.Forms;
namespace PLC
{
public partial class Form1 : Form
{
ModbusClient modbusClient;
public Form1()
{
InitializeComponent();
ON.Visible = true; //yellow
OFF.Visible = true;//red
modbusClient = new ModbusClient("COM5");//communication settings
modbusClient.UnitIdentifier = 1;
modbusClient.Baudrate = 19200;
modbusClient.Parity = System.IO.Ports.Parity.None;
modbusClient.StopBits = System.IO.Ports.StopBits.One;
modbusClient.Connect();
// reading function begin
var value = modbusClient.ReadCoils(0, 1);// read coil zero and if it is true change color yellow else change color to red
if (value[0] == true)
{
ON.Visible = true;
OFF.Visible = false;
}
else
{
ON.Visible = false;
OFF.Visible = true;
}
// end of reading function
}
private void button1_Click(object sender, EventArgs e)// on button
{
modbusClient.WriteSingleCoil(0, true);//toggle coil zero to on
}
private void button2_Click(object sender, EventArgs e)// off button
{
modbusClient.WriteSingleCoil(0, false);// toggle coil zero to off
}
}
}
Try to use timers.
Here is your modified code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using EasyModbus;
using System.Windows.Forms;
namespace StackOverflowAnswer
{
public partial class Form1 : Form
{
ModbusClient modbusClient;
public Form1()
{
InitializeComponent();
ON.Visible = true; //yellow
OFF.Visible = true;//red
// ### changed to TCP - I have no COM ports ###
modbusClient = new ModbusClient("127.0.0.1", 502); //communication settings
//modbusClient.UnitIdentifier = 1;
//modbusClient.Baudrate = 19200;
//modbusClient.Parity = System.IO.Ports.Parity.None;
//modbusClient.StopBits = System.IO.Ports.StopBits.One;
modbusClient.Connect();
#region Modified section
var readModBusTimer = new Timer()
{
Interval = 500,
Enabled = true
};
readModBusTimer.Tick += ReadModBusTimer_Tick;
readModBusTimer.Start();
// reading function begin
ReadCoils();
// end of reading function
}
private void ReadCoils()
{
var value = modbusClient.ReadCoils(0, 1);// read coil zero and if it is true change color yellow else change color to red
if (value[0] == true)
{
ON.Visible = true;
OFF.Visible = false;
}
else
{
ON.Visible = false;
OFF.Visible = true;
}
}
private void ReadModBusTimer_Tick(object sender, EventArgs e)
{
ReadCoils();
}
#endregion
private void button1_Click(object sender, EventArgs e)// on button
{
modbusClient.WriteSingleCoil(0, true);//toggle coil zero to on
}
private void button2_Click(object sender, EventArgs e)// off button
{
modbusClient.WriteSingleCoil(0, false);// toggle coil zero to off
}
}
}
I want to show current form Title inside the textBox
But the code below doesnt do it,it only works if I set it on Button1_click.
And After clicking the button it will change to the form title
But I need it to set the form title in the textbox instantly on load
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace ShowTitle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Process currentp = Process.GetCurrentProcess();
textBox1.Text = currentp.MainWindowTitle;
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
This simple code demonstrate that when your event handler for Form.Load event is called there is no MainWindowTitle to read from the currentp, while if you execute the same code in the Form.Shown event handler there is a MainWindowTitle in the currentp variable
Form f;
TextBox t;
void Main()
{
f = new Form();
f.Text = "This is a test";
t = new TextBox();
f.Controls.Add(t);
f.Load += onLoad;
f.Shown += onShow;
f.Show();
}
void onLoad(object sender, EventArgs e)
{
Process currentp = Process.GetCurrentProcess();
if(!string.IsNullOrWhiteSpace(currentp.MainWindowTitle))
t.Text = currentp.MainWindowTitle;
else
t.Text = "NO TITLE";
}
void onShow(object sender, EventArgs e)
{
// Uncomment these line to see the differences
// if(!string.IsNullOrWhiteSpace(currentp.MainWindowTitle))
// t.Text = currentp.MainWindowTitle;
// else
// t.Text = "NO TITLE";
}
I have a text file of 11 bytes now i want to split and write the text to multiple files say 2 bytes for each file and 3 for the last file how to do this using c#
For now here is my code which writes the whole file to a new file
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
this.Text = textBox1.Text;
}
private void button1_Click(object sender, EventArgs e)
{
var fileName = Text;
FileInfo fi = new FileInfo(fileName);
var size = fi.Length;
int i = Convert.ToInt32(size);
int j = Convert.ToInt32(comboBox1.SelectedItem);
decimal ireminder = (decimal)i % j;
MessageBox.Show(ireminder.ToString());
StreamReader sr = new StreamReader(Text);
var line = sr.ReadLine();
StreamWriter sw = new StreamWriter("D:\\Test.txt");
sw.WriteLine(line);
sw.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
im kinda new to programming and As university project,i have to write a program which changes a file info like a virus and then undo the changes just like anti virus.
i wrote the code for changing attribute on read only,But what about Hidden or system file ?
and what is the way for undoing it !
where im going wrong in coding ??
Here is my main form code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OpenFileDialog fDialog;
void button1_Click(object sender, EventArgs e) // Browse button
{
fDialog = new OpenFileDialog();
fDialog.Title = "Open a Text File";
fDialog.Filter = "TXT Files|*.txt|doc Files|*.doc";
fDialog.InitialDirectory = #"C:\";
if (fDialog.ShowDialog() == DialogResult.OK)
{
MessageBox.Show(fDialog.FileName.ToString());
}
textBox1.Text = fDialog.FileName;
fDialog.AddExtension = true;
fDialog.CheckFileExists = true;
fDialog.CheckPathExists = true;
}
private void textBox1_TextChanged(object sender, EventArgs e)//the path showing text box
{
}
private void button2_Click(object sender, EventArgs e)//read-only button
{
fDialog.ReadOnlyChecked = true;
}
private void button3_Click(object sender, EventArgs e) //Hidden button
{
}
}
}
Am working in a project where I should give a crystal reports RPT file as input and get the properties of that report.
I am facing a problem in loading the report itself so I have used isLoaded() function to check whether the report is loaded or not.
I have used the following code:
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;
using System.Web;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
namespace sampleretreival
{
public partial class Form1 : Form
{
public string flName;
public Form1()
{
InitializeComponent();
Retrieve.Enabled = false;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstFiles.SelectedItems.Count > 0)
{
Retrieve.Enabled = true;
}
else
{
Retrieve.Enabled = false;
}
}
private void Browse_Click(object sender, EventArgs e)
{
openFileDialog1 = new OpenFileDialog();
openFileDialog1.Multiselect = false;
openFileDialog1.Filter = "Crystal Report Files | *.rpt";
openFileDialog1.ShowDialog();
flName = openFileDialog1.FileName;
if (flName.Length != 0)
{
lstFiles.Items.Insert(0, flName);
Retrieve.Enabled = true;
}
else
{
MessageBox.Show("Please select the crystal report files for analysis.", "SAMPLE", MessageBoxButtons.OK, MessageBoxIcon.Information);
Browse.Focus();
}
}
private void Retrieve_Click(object sender, EventArgs e)
{
int a=1;
ReportDocument rpt = new ReportDocument();
rpt.Load(flName);
int count = 5;
if (rpt.IsLoaded)
{
MessageBox.Show(count.ToString());
}
else
{
MessageBox.Show(a.ToString());
}
}
}
}
After compiling, I clicked the Browse button to select the report from the disk but when I click Retrieve button, the program keeps on running. I am not getting any output or any error.