My code when ran, opens a form and when that form opens, it opens and reads data from a excel spreadsheet. During the initial load, it reads the specific data from it's current cells. To test the theory of reading extra data, I manually re-read the data using a button and calling specific cells. Here is my code below:
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 test_read_data_from_excel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
OpenFile();
}
public void OpenFile()
{
Excel excel = new Excel(#"Test.xlsx", 1);
textBox1.Text = excel.ReadCell(1, 0);
textBox2.Text = excel.ReadCell(1, 1);
textBox3.Text = excel.ReadCell(3, 2);
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Excel excel = new Excel(#"Test.xlsx", 1);
textBox1.Text = excel.ReadCell(2, 0);
textBox2.Text = excel.ReadCell(2, 1);
textBox3.Text = excel.ReadCell(4 , 2);
}
}
}
How can I get the form to automatically read the data and then every 10 seconds display the next line or cells of data? P.S. I used a button to check to see if the reading of the next line of data or cells works correctly.
For the timer functionality, try something like this:
private void InitializeTimer()
{
// Call this procedure when the application starts.
// Set to 10 seconds.
Timer1.Interval = 10000; // .Interval is in milliseconds
Timer1.Tick += new EventHandler(Timer1_Tick);
// Enable timer.
Timer1.Enabled = true;
}
private void Timer1_Tick(object Sender, EventArgs e)
{
//Call any excel code you want to run every 10 seconds here
}
Related
I want develop a calculator by using c#, and I do not use method click for all button from 0 to 9 I want I have just one method and if I click the each button wrote in textbox by using sender and tags.
best regards
enter code here
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 Final
{
public partial class Form1 : Form
{
bool names;
int counter;
string name;
double ans, num;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text += "1";
counter++;
again();
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text += "2";
counter++;
again();
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text += "3";
counter++;
again();
}
You can have just one handler for all digit buttons, and then you can extract its value like this:
int num = int.Parse(((Button)sender).Text);
This assumes that you set the Text property of the buttons to: 0,1,2..9
You can access the Tag property just like the Text:
var txt = ((Button)sender).Tag).ToString();
textBox1.Text += txt;
Set .Tag to correspondent value and then retrieve it from sender by casting it to Button type.
private void button_Click(object sender, EventArgs e)
{
var button = (Button)sender;
textBox1.Text += button.Tag.ToString();
counter++;
again();
}
public Form1()
{
InitializeComponent();
button1.Tag = 1;
button1.Click += button_Click;
button2.Tag = 2;
button2.Click += button_Click;
// and so on for other buttons
}
I am making a desktop app in c# in which when the user selects specific name of background from the menu strip, then the background shall turn to that required background. The problem is that i cannot save the user input, i tried settings but i cannot find "system.drawing.image" in settings so is there any way i can save the user changed background ? No external backgrounds a user is allowed to change, just the ones in the resource folder. Here is my code which shows error that system.drawing.color cannot take place of drawing.image.
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 TAC
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
panel1.Location = new Point(165, 157);
panel2.Location = new Point(289, 158);
panel3.Location = new Point(47, 275);
panel4.Location = new Point(47, 402);
this.BackgroundImage = Properties.Settings.Default.FormImage;
}
private void bLUEToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackgroundImage = TAC.Properties.Resources.tex1;
}
private void gREENToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackgroundImage = TAC.Properties.Resources.tex2;
}
private void oRANGEToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackgroundImage = TAC.Properties.Resources.tex3;
}
private void rEDToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackgroundImage = TAC.Properties.Resources.tex4;
}
private void pURPLEToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackgroundImage = TAC.Properties.Resources.tex5;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.FormImage = this.BackgroundImage;
}
}
}
Use Save method to save settings
Properties.Settings.Default.Save();
If you want add an image to settings :
Add new setting with type string and use like this:
Save an image to setting ( when you close form)
MemoryStream ms = new MemoryStream();
Propertis.Resources.MyImage.Save(ms,ImageFormat.Jpeg);
Properties.Settings.Default.BackImg = Convert.ToBase64String(ms.ToArray());
Properties.Settings.Default.Save();
And read image from setting and set to background(in form load)
string img = Properties.Settings.Default.BackImg ;
byte[] i = Convert.FromBase64String(img);
this.BackgroundImage = Image.FromStream(new MemoryStream(i));
How add custom settings ?
http://www.codeproject.com/Articles/29130/Windows-Forms-Creating-and-Persisting-Custom-User
This is a link for the OpenPop what I'm using in my project:
OpenPop
This is a page with examples:
Example
And this is my code. In form1 designer I added a progressBar.
What I want to do is to get all the messages I have on my server and show in a progressBar the progress of the messages retrieving.
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 Pop3_Emails
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
backgroundWorker1.RunWorkerAsync();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
OpenPop.Pop3.Pop3Client PopClient = new OpenPop.Pop3.Pop3Client();
PopClient.Connect("mail.bezeqint.net", 110, false);
PopClient.Authenticate("meemail", "password",
OpenPop.Pop3.AuthenticationMethod.UsernameAndPassword);
int messageCount = PopClient.GetMessageCount();
List<Message> allMessages = new List<Message>(messageCount);
for (int i = messageCount; i > 0; i--)
{
allMessages.Add(PopClient.GetMessage(i));
}
backgroundWorker1.ReportProgress(0,PopClient.GetMessageCount().ToString());
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label1.Text = e.UserState.ToString();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
}
}
The first problem is that i'm getting two errors on the line:
allMessages.Add(PopClient.GetMessage(i));
Error 1 The best overloaded method match for
'System.Collections.Generic.List.Add(System.Windows.Forms.Message)'
has some invalid arguments
Error 2 Argument 1: cannot convert from 'OpenPop.Mime.Message' to
'System.Windows.Forms.Message'
The second problem is how to report to the progressBar the progress ? Somehow from inside the FOR loop.
I think, first question is solved by AlexK.
Second question can by solved by setting:
backgroundWorker1.WorkerReportsProgress = true;
and in your for loop you can calculate and report progress by:
int nProgress = (messageCount - i) * 100 / messageCount;
backgroundWorker1.ReportProgress(0, nProgress);
So now, you can show your progress:
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
int nProgress = (int)e.UserState;
label1.Text = nProgress.ToString();
progressBar1.Value = nProgress;
}
But what i also see is, that you are adding all downloaded messages to local variable List<Message> allMessages. So they get lost when downloading has finished. May be you should move it into the class...
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'm trying to display the data received on textbox. But I realized that when data were being received on Br#y terminal it look fine (eg. 14:02:33.43 > T 11 22.32) but running on the software the time stamp is missing.
Am I missing out anything which lead to this?
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.Data.OleDb;
using System.IO;
namespace SerialCom
{
public partial class Form1 : Form
{
string RxString; //Variable
public Form1()
{
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e)
{
serialPort1.PortName = "COM4";
serialPort1.BaudRate = 9600;
serialPort1.Open();
if (serialPort1.IsOpen)
{
btnStart.Enabled = false;
btnStop.Enabled = true;
txtData.ReadOnly = false;
}
}
private void btnStop_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
btnStart.Enabled = true;
btnStop.Enabled = false;
txtData.ReadOnly = true;
}
}
private void txtData_KeyPress(object sender, KeyPressEventArgs e)
{
if (!serialPort1.IsOpen) return; // If the port is closed, don't try to send a character.
char[] buff = new char[8]; // If the port is Open, declare a char[] array with one element.
buff[0] = e.KeyChar; // Load element 0 with the key character.
serialPort1.Write(buff, 0, 1); // Send the one character buffer.
e.Handled = true;// Set the KeyPress event as handled so the character won't
// display locally. If you want it to display, omit the next line.
}
private void DisplayText(object sender, EventArgs e)
{
txtData.AppendText(RxString);
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadExisting();
this.Invoke(new EventHandler(DisplayText));
StreamWriter MyStreamWriter = new StreamWriter(#"c:\testing.txt",true); //True tell SW to append to file instead of overwriting
MyStreamWriter.Write(RxString);
MyStreamWriter.Flush();
MyStreamWriter.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort1.IsOpen)
serialPort1.Close();
}
}
}
Basically, I was wrong by saying that the time stamp is originally included in the data i received.. I need to add in the time stamp on my own.. This was to act as a record of when did i actually received the following data.
As a result, what's missing is this
string time = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.ff")
Hope this helps people who are struggling with the problem too.