I am trying to refresh a form after every 30 minutes if the datagrid is empty. My code is as below:
private void Form1_Load(object sender, EventArgs e)
{
BindDataGrid();
if (dataGrid_FileList.RowCount <=0)
{
Timer refreshTimer = new Timer();
refreshTimer.Interval = 30000; //30 seconds in milliseconds
refreshTimer.Tick += new EventHandler(refreshTimer_Tick);
refreshTimer.Start();
}
}
void refreshTimer_Tick(object sender, EventArgs e)
{
this.Controls.Clear();
this.InitializeComponent();
BindDataGrid();
if (dataGrid_FileList.RowCount>0)
{
InhouseDownloadeer_Shown(this, null);
}
}
This code works well when RowCount of datagrid is <=0 but it's continuing even after the datagrid contains rows > 0. How can I prevent refreshTimer_Tick if the datagrid contains rows?
You can also stop a Timer. Declare the timer variable outside the method.
Timer refreshTimer = new Timer();
private void Form1_Load(object sender, EventArgs e)
{
refreshTimer.Interval = 30000;
refreshTimer.Tick += new EventHandler(refreshTimer_Tick);
}
Now simply call refreshTimer.Stop(); when needed in another method.
PS asp.net and winforms are not the same thing.
You call the if statement just after the databind methode. And I think it executes before your datagrid binds its rows.
For me you should remove this code from your refreshTimerTick event :
if (dataGrid_FileList.RowCount>0)
{
InhouseDownloadeer_Shown(this, null);
}
Like :
void refreshTimer_Tick(object sender, EventArgs e)
{
this.Controls.Clear();
this.InitializeComponent();
BindDataGrid();
}
And put it in the DataBount event
void dataGrid_DataBound(object sender, EventArgs e)
{
if (dataGrid_FileList.RowCount>0)
{
InhouseDownloadeer_Shown(this, null);
}
}
Related
Im starting multiple timer with this Code.
All timers run the same Tick_event where i identify the Timer by tag.
ArrayList list = new ArrayList();
private void button1_Click(object sender, EventArgs e)
{
Timer t = new Timer();
t.Tag = ID;
t.Interval = 60000;
t.Tick += tm_Tick;
list.Add(t);
t.Enabled = true;
t.Start();
}
And on tick event i get the tag ID number with this code, and it works great.
private void tm_Tick(object sender, EventArgs e)
{
Console.WriteLine((sender as Timer).Tag);
}
But how can i access the specific timer with the ID, to stop it?
its .NET Winforms
Why not use a Dictionary
Dictionary<String, Timer> list = new Dictionary<String, Timer>();
private void button1_Click(object sender, EventArgs e)
{
Timer t = new Timer();
t.Tag = ID;
list.Add(ID, t);
...
}
Then to stop it:
list[tag].Stop();
Note: Make sure you Dispose your timers at some point
I'm having trouble passing values entered in form2(citacao) to form1(principal).
Principal.cs (form1)
richEditControl1.Document.AppendText(citacao.valor_edit[0]);
Citacao.cs (form2)
public string[] valor_edit = new string[3];
private void simpleButton2_Click(object sender, EventArgs e)
{
valor_edit[0] = memoEdit1.Text;
valor_edit[1] = comboBox1.SelectedItem.ToString();
valor_edit[2] = textEdit1.Text;
}
But when I click the button nothing happens , the values are not inserted into the richedit I like it.
I already have this on form (Pass DataGrid to ComboBox)
Form1 (principal)
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
citacao cita = new citacao(this);
cita.Show();
}
form2(citação)
public citacao(principal gridForm)
{
InitializeComponent();
frm1 = gridForm;
}
// LOAD ALL FONTS (Referencias);
private void citacao_Load(object sender, EventArgs e)
{
comboBox1.Items.Clear();
foreach (DataGridViewRow row in frm1.DataGridView1.Rows)
{
comboBox1.Items.Add(row.Cells[0].Value.ToString());
}
comboBox1.SelectedIndex = 0;
}
let's see whether I understood your situation :)
declare your variable in Form 1 as a class variable
private citacao cita;
then initialize it in the button press event
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
cita = new citacao(this);
// subscribe to the closing event
cita.FormClosing += form_FormClosing;
cita.Show();
}
// when Form 2 will be closed you can execute your important line in the event
void form_FormClosing(object sender, FormClosingEventArgs e)
{
// BUT! you have to use the variable name!
richEditControl1.Document.AppendText(cita.valor_edit[0]);
}
EDIT:
Ok after looking at the entire code:
please remove the button3! and this entire code:
private void button3_Click(object sender, EventArgs e)
{
cita = new citacao(this);
richEditControl1.Document.AppendText(citacao.valor_edit); // this line is the problem!
}
The function AppendText probably needs a string as parameter and you give the entire array!
If you subscribe to the closing event in Form1 / principal and also implement
the event, your data will be transmitted automatically as soon as the Form 2 disappears from the screen :)
I'm trying to make a label refresh every second so the countdown updates, having some trouble. I'm extremely new to C# apologies for the noob questions.
private void Form1_Load(object sender, EventArgs e)
{
bool ephCD = true;
int ephHours = (DateTime.Today.AddDays(1) - DateTime.Now).Hours;
int ephMinu = (DateTime.Today.AddDays(1) - DateTime.Now).Minutes;
int ephSecs = (DateTime.Today.AddDays(1) - DateTime.Now).Seconds;
label1.Text = ephHours.ToString() + ":" + ephMinu.ToString() + ":" + ephSecs.ToString();
while (ephCD == true)
{
label1.Refresh();
}
}
When launching this the program doesn't even appear.
Why does the program not appear?
You are performing an infinite loop in Form_Load. This means that the form will never finish loading, and your program will be stuck.
Your refresh loop needs to be on a separate thread, or ideally toss the loop and use a Timer instead of spin locking the CPU on an infinite loop.
Timer myTimer = new Timer(1000);
void Form1_Load()
{
myTimer.Elapsed += UpdateLabel;
myTimer.Start();
}
private void UpdateLabel(object sender, ElapsedEventArgs e)
{
//Update label here
}
Updating the label in a while statement is not a good option, a better approach would be to use Timer class
var aTimer = new System.Timers.Timer(1000);
aTimer.Elapsed += OnTimedEvent;
aTimer.Enabled = true;
private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
//update the label
}
This will do, just copy and paste:
private void Form1_Load(object sender, EventArgs e)
{
// To update the first time.
label1.Text = (DateTime.Today.AddDays(1)- DateTime.Now).ToString(#"hh\:mm\:ss");
var timer = new Timer {Interval = 1000};
timer.Tick += (o, args) =>
{
label1.Text = (DateTime.Today.AddDays(1)- DateTime.Now).ToString(#"hh\:mm\:ss");
};
timer.Start();
}
I ended up with this simpler solution:
<script>
var myTimer = setInterval(Atualizar, 20000);
function Atualizar() {
__doPostBack('UpdatePanelNew', '');
}
</script>
Make sure you wrap what you want to update within an UpdatePanel.
This code will request a postback in every 20s. So in the code behind I can do this:
protected void Page_Load(object sender, EventArgs e)
{
myLabel.InnerText = GetInDatabaseTheValueIwant();
}
so I have this code
public void Update_Click(object sender, EventArgs e)
{
using (PccBiometricsHandler.Form1 ShowProgress = new PccBiometricsHandler.Form1())
{
menu.Items[2].Enabled = false;
ShowProgress.ShowDialog();
ShowProgress.FormClosed += new FormClosedEventHandler(MyForm_FormClosed);
}
}
public void MyForm_FormClosed(object sender, FormClosedEventArgs e)
{
updaterAccess();
menu.Items[2].Enabled = true;
}
so after I click Update it will run the child form Form1
which is this:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
notifyIcon1.Visible = true;
notifyIcon1.BalloonTipTitle = "Update Complete";
notifyIcon1.BalloonTipText = "Successfully Update";
notifyIcon1.ShowBalloonTip(500);
timer1.Interval = 4000;
timer1.Enabled = true;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
notifyIcon1.Dispose();
this.Close();
}
so as you can see it runs on a backgroundworker with a timer to close the child Form1
now my problem is that after closing the Child Form1 it doesn't run the MyForm_FormClosed which it should enable menu.Items[2] again and updaterAccess()
I think I'm missing something in my mainForm
Attached the event handler before firing ShowDialog
public void Update_Click(object sender, EventArgs e)
{
using (PccBiometricsHandler.Form1 ShowProgress = new PccBiometricsHandler.Form1())
{
menu.Items[2].Enabled = false;
ShowProgress.FormClosed += new FormClosedEventHandler(MyForm_FormClosed); //Attached the event handler before firing ShowDialog
ShowProgress.ShowDialog();
}
}
ShowDialog synchronously shows a modal dialog, meaning it blocks until the form is closed (the following code is not run until the form is closed). Therefore, when ShowDialog returns the form is already closed.
You can attach the event handler before calling ShowDialog() as #Jade suggests, which will work, but honestly you do not need to use the event system at all. Simply wait for ShowDialog to return then perform the actions you would when the form is closed:
public void Update_Click(object sender, EventArgs e)
{
using (PccBiometricsHandler.Form1 ShowProgress = new PccBiometricsHandler.Form1())
{
menu.Items[2].Enabled = false;
ShowProgress.ShowDialog();
}
updaterAccess();
menu.Items[2].Enabled = true;
}
If you want to do this in VB:
AddHandler ShowProgress.FormClosed, AddressOf MyForm_FormClosed
I've made this program in C#:
namespace Spammer
{
public partial class Form1 : Form
{
int delay, y = 1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
delay = int.Parse(textBox2.Text);
timer1.Interval = delay;
timer1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
String textt = textBox1.Text;
SendKeys.SendWait(textt);
}
}
}
It works fine most of the time, and it can really send keys quickly.
But when I insert a delay of, for example, 10 MS, it's very hard to click the "Stop" button to stop it. The only way to stop the sending is to close the program and I don't want to do that.
Is there anyway I can send keys very quickly, like 5-10 MS, without it impairing my ability to press the buttons inside the program? I can't click while it's sending quickly...
The problem is that you're using SendWait. That will wait for the target application to respond - and while that's happening, your application won't be able to respond to user input. If you use Send instead of SendWait, your UI thread won't be blocked waiting for the key press to be processed.
I was able to reproduce the issue. The app is sending a keystroke every 10 milliseconds. To me, this is not at all surprising that the app is causing freezes. A keystroke every 10 milliseconds is quite a barrage to the active App. Threading is not going to help. Why is this behavior surprising?
In other words, I don't expect things to work out well when I overload the message pump.
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Spammer//your own namesapce
{
public partial class Form1 : Form
{
int delayInMilliseconds, y = 1;
private Timer timer1;
public Form1()
{
InitializeComponent();
//StartTimerWithThreading();
SetupTimer();
}
void StartTimerWithThreading()
{
Task.Factory.StartNew(() =>
{
SetupTimer();
});
}
void SetupTimer()
{
timer1 = new Timer();//Assume system.windows.forms.timer
textBox2.Text = "10";//new delay
timer1.Tick += timer1_Tick;//handler
}
private void button1_Click(object sender, EventArgs e)
{
delayInMilliseconds = int.Parse(textBox2.Text);
timer1.Interval = delayInMilliseconds;
timer1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
String textt = textBox1.Text;
SendKeys.SendWait(textt);
}
}
}
The simple solution is instead of adding code to a Click event handler for your button, we need a MouseDown event handler:
//MouseDown event handler for the button2
private void button2_MouseDown(object sender, EventArgs e) {
timer1.Enabled = false;
}
Or you can keep using the Click event handler but we send the key only when the MouseButtons is not Left like this:
private void timer1_Tick(object sender, EventArgs e) {
String textt = textBox1.Text;
if(MouseButtons != MouseButtons.Left) SendKeys.Send(textt);
}
//then you can freely click your button to stop it.