I have three forms, From 1 is a main form and Form2 is splash screen and Form3 is a mini form.
So what i am trying to do is when ever i send a mail this From3 will pop up which has a gif image
and a label. So i am successfully able to make the From3 for 5 sec, in which the label will
change for different words for each second.
In form 3
i am using timer1 to run the mini form only for 5 seconds. and timer two to run only for 1 sec.
I guess we can do this in a better way which is pretty simple and easy.. Any good ideas and
help are most welcome!!!
Note:- Also when i again press the button send mail.. the label is starting from - Done!!.. Any helps.. The first time it starts from Connecting to smtp server..but on second time onwards its staring from Done!! then label going to Connecting to smtp server.. and so on!!
Here is my code:
Form1
private void sendmail_Click(object sender, EventArgs e)
{
//mail basic function here!!!!
smtp.Send(msg);
_f3.ShowDialog();//- - ->> goes to mini form Form3
smtp.Dispose();
MessageBox.Show("Email Successfully Sent!!!", "Mail!!!.");
}
Form3
private void Form3_Load(object sender, EventArgs e)
{
timer1.Interval = 5000;
timer1.Enabled = true;
timer1.Tick += new EventHandler(timer1_Tick);
timer2.Interval = 1000;
timer2.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
timer1.Stop();
}
int StopTime = 0;
private void timer2_Tick(object sender, EventArgs e)
{
StopTime++;
if (StopTime == 1)
{
label1.Text = " Connecting to smtp server..";
}
if (StopTime == 2)
{
label1.Text = " Fetching recipients..";
}
if (StopTime == 3)
{
label1.Text = " Attaching G-code files..";
}
if (StopTime == 4)
{
label1.Text = " Done!!";
StopTime = 0;
timer2.Stop();
}
}
Does Form3 close after it displays Done? You could do something like this:
private void Form3_Load(object sender, EventArgs e)
{
SetLabel1Text(); //reset label text
timer1.Interval = 5000;
timer1.Enabled = true;
timer1.Tick += new EventHandler(timer1_Tick);
timer2.Interval = 1000;
timer2.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
timer1.Stop();
}
int StopTime = 0;
private void timer2_Tick(object sender, EventArgs e)
{
StopTime++;
SetLabel1Text();
if (StopTime == 4)
{
StopTime = 0;
timer2.Stop();
}
}
private void SetLabel1Text()
{
string[] label1Text = { " Connecting to smtp server..", " Connecting to smtp server..", " Fetching recipients..", " Attaching G-code files..", " Done!!" };
label1.Text = label1Text[StopTime]; //populate label from array of values
}
Related
Long Story Short,
The app I am making will Launch a Game.
The Start button will then Check to make sure the games EXE is running..
IF it is running, it will run a script to press buttons 1, 2, and 3..
After that it will loop that script, but first checking if the game has not crashed (if it crashed it wont run the loop)
My Issue:
While the loop is running, which is using System.Threading.Thread.Sleep,
does not let other functions of the app preform (in this case showing and hiding button, and changing label colors.) The main reason this is important is the button it wont is the Stop button which is suppose to end the looping script.
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
Timer timer;
Stopwatch sw;
public Form1()
{
InitializeComponent();
button4.Visible = false;
button2.Enabled = false;
}
private void timer_Tick(object sender, EventArgs e)
{
label2.Text = sw.Elapsed.Seconds.ToString() + "seconds";
Application.DoEvents();
}
// ===============================================
// BUTTON FUNCTIONS
// ===============================================
// Launch GAME
private void button1_Click(object sender, EventArgs e)
{
// Launch GAME
Process.Start(#"C:\Windows\System32\Notepad.exe");
button2.Enabled = true;
}
// START BOT
private void button2_Click(object sender, EventArgs e)
{
status.Text = #"Starting Bot..";
timer = new Timer();
timer.Interval = (1000);
timer.Tick += new EventHandler(timer_Tick);
sw = new Stopwatch();
timer.Start();
sw.Start();
BotReady(sender, e);
}
// PLAYER DIED
private void button3_Click(object sender, EventArgs e)
{
KillBot(sender, e);
status.Text = #"lol u ded";
}
// STOP THE BOT
private void button4_Click(object sender, EventArgs e)
{
KillBot(sender, e);
status.Text = #"Bot Stopped";
button2.Visible = true;
button4.Visible = false;
button2.Enabled = true;
}
// KILL GAME AND BOT (IF IT CRASHED OR SOMETHING)
private void button5_Click(object sender, EventArgs e)
{
}
// ===============================================
// OTHER FUNCTIONS
// ===============================================
// Target GAME application
private void TargetAQ(object sender, EventArgs e)
{
// this part doesnt work yet.
// Selection.Application and Tab to it
}
// CHECK IF GAME IS STILL RUNNING, KILL BOT IF GAME IS NOT DETECTED
public void CheckStatus(object sender, EventArgs e)
{
Process[] GAME = Process.GetProcessesByName("notepad");
if (GAME.Length == 0)
// GAME NOT running
{
KillBot(sender, e);
button2.Enabled = false;
status.Text = #"GAME is not Running, Bot Stopped.";
uGAME.ForeColor = Color.Red;
button2.Visible = true;
button4.Visible = false;
button2.Enabled = false;
}
else
// GAME IS running
{
status.Text = #"GAME is Running!";
uGAME.ForeColor = Color.Green;
button2.Visible = false;
button4.Visible = true;
}
}
// Verify bot and GAME are running before starting
public void BotReady(object sender, EventArgs e)
{
CheckStatus(sender, e);
if (uGAME.ForeColor == Color.Green)
{
status.Text = #"Bot Started!";
Botting(sender, e);
}
else { status.Text = #"GAME is not running"; }
}
//THE BOT ACTIONS
public void Botting(object sender, EventArgs e)
{
// Check to make sure everything is still running
CheckStatus(sender, e);
TargetAQ(sender, e);
if (uGAME.ForeColor == Color.Green)
{
// all is running, then you good.
Script(sender, e);
}
//GAME died, kill scripts
else {
KillBot(sender, e);
status.Text = #"GAME Crashed:(";
}
}
//Things it does in-game
public void Script(object sender, EventArgs e)
{
status.Text = #"Bot in progress..";
// Use skills in game rotation
System.Threading.Thread.Sleep(3000);
SendKeys.Send("1");
System.Threading.Thread.Sleep(3000);
SendKeys.Send("2");
System.Threading.Thread.Sleep(3000);
SendKeys.Send("3");
// Go back and check the game has not crashed before re-running the script
Botting(sender, e);
}
// STOP THE BOT AND TIME COUNTER
public void KillBot(object sender, EventArgs e)
{
// kill the clock and bot
status.Text = #"Stopping bot...";
timer.Stop();
sw.Stop();
label2.Text = sw.Elapsed.Seconds.ToString() + " seconds";
status.Text = #"Bot Stopped";
}
}
}
So i have this program that needs to be connected to a certain server in our network, but in this case I will use 8.8.8.8. To make it easy to see if the connection is up I have a label that should say "yes" if connected and "no" if not. However I can't get it to work, this is the code I am using:
Sorry that wasn't descriptive enough indeed. It doesn't appear to do anything... Like the label doens't change at all. I've tried it on 2 different machine on Visual Studio 2017 and 2015. I can't wrap my head around it.
private void Form2_Load(object sender, EventArgs e)
{
timer1.Interval = 4000;
timer1.Enabled = true;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
Ping ping = new Ping();
PingReply pingStatus = ping.Send(IPAddress.Parse("8.8.8.8"));
if (pingStatus.Status == IPStatus.Success)
{
label6.Text = "yes";
label6.ForeColor = Color.Blue;
}
else
{
label6.Text = "no";
label6.ForeColor = Color.Red;
}
}
Edit: changed != to ==
You will want to try something like the following;
private void Form2_Load(object sender, EventArgs e)
{
timer1.Interval = 4000;
timer1.Enabled = true;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
string Address = "8.8.8.8";
PingHost(Address);
}
public bool PingHost(string Address)
{
bool Canping = false;
Ping ping = new Ping();
try
{
PingReply reply = ping.Send(Address);
label6.Text = reply.Status.ToString();
label6.ForeColor = Color.Blue;
}
catch (PingException e)
{
label6.Text = e.ToString();
label6.ForeColor = Color.Red;
}
return Canping;
}
This way by using NetworkInformation you can handle the returns a bit better.
However, having this check every 4 seconds might not be the best approach as this might have a knock on effect on performance but I guess this would all depend on what the application is needed for and the network you are pinging.
EDIT
The OP code does work but this maybe a more efficient way to handle this.
How do i get my winforms app to click the button on the form based on an interval which is defined in a text box on my form?
I have tried:
private void Form1_Load(object sender, EventArgs e)
{
Timer tm;
tm = new Timer();
tm.Interval = 1000;
tm.Tick += new EventHandler(button1_Click);
}
Once the timer starts and they click Yes to the dialog box, then i want the timer to restart again.
I have the following:
if (result1 == DialogResult.Yes)
{
string pastebuffer = DateTime.Now.ToString();
pastebuffer = "### Edited on " + pastebuffer + " by " + txtUsername.Text + "###";
Clipboard.SetText(pastebuffer);
}
else if (result1 == DialogResult.No)
{
//do something else
}
Add interval from textBox and start the timer, all from a click of a button:
private void button1_Click(object sender, EventArgs e)
{
timer1.Stop(); // if you need to stop, then stop it here
timer1.Interval = int.Parse(textBox1.Text);
timer1.Start();
}
To stop the timer, use
tm.Stop();
Then you can change whatever the interval is, or any other properties as such
tm.Interval = int.Parse(textBox1.Text); // Will fail on non-int input...
then use,
tm.Start();
Use this code. Hope it will help. Define Time tm on class level so that you can access outside the Form1_load function.
Timer tm;
private void Form1_Load(object sender, EventArgs e)
{
tm = new Timer();
tm.Interval = 1000;
tm.Tick += new EventHandler(button1_Click);
}
if (result1 == DialogResult.Yes)
{
'if you want to restart your timer than add here.
tm.stop()
tm.Interval = int.Parse(newinterval.text);
string pastebuffer = DateTime.Now.ToString();
pastebuffer = "### Edited on " + pastebuffer + " by " + txtUsername.Text + "###";
Clipboard.SetText(pastebuffer);
tm.start()
}
else if (result1 == DialogResult.No)
{
//do something else
}
I need to hide current form after many second and then show any form
I'm writing this code but it doesn't work.
namespace tempprj
{
public partial class ProfileFrm : Telerik.WinControls.UI.RadForm
{
public ProfileFrm()
{
InitializeComponent();
}
private void ProfileFrm_Load(object sender, EventArgs e)
{
Frm2 child = new Frm2();
Thread.Sleep(3000);
this.Hide();
child.ShowDialog();
}
}
}
Thread.Sleep(3000);
is going to prevent your project from doing anything at all for 3 seconds (not counting other threads) and freeze the UI. I suggest using the standard .NET timer.
http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx
This is a solution to my question:
private void ProfileFrm_Load(object sender, EventArgs e)
{
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Enabled = true;
timer1.Interval = 4000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
this.Hide();
Frm2 f = new Frm2();
f.ShowDialog();
}
how can i set timeout for webBrowser navigate (url) event
c# netframework 4.0
By using a Timer of course. For example:
public void NavigateTo(Uri url) {
webBrowser1.Navigate(url);
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e) {
timer1.Enabled = false;
MessageBox.Show("Timeout on navigation");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
if (e.Url == webBrowser1.Url && timer1.Enabled) {
timer1.Enabled = false;
// etc..
}
}
I am using following approach based on Navigating and Navigated events. The time between these two events are observed for redirecting to home pgae.
//Navigation Timer
timer2.Enabled = true;
timer2.Interval = 30000;
br.DocumentCompleted += browser_DocumentCompleted;
br.DocumentCompleted += writeToTextBoxEvent;
br.Navigating += OnNavigating;
br.Navigated += OnNavigated;
br.ScriptErrorsSuppressed = true;
br.Navigate(ConfigValues.websiteUrl);
private void OnNavigating(object sender, WebBrowserNavigatingEventArgs e)
{
//Reset Timer
timer2.Stop();
timer2.Start();
WriteLogFunction("OnNavigating||||||"+e.Url.ToString());
}
private void OnNavigated(object sender, WebBrowserNavigatedEventArgs e)
{
//Stop Timer
timer2.Stop();
WriteLogFunction("NAVIGATED <><><><><><><> " + e.Url.ToString());
}
private void timer2_Tick(object sender, EventArgs e)
{
WriteLogFunction(" Navigation Timeout TICK");
br.Stop();
br.Navigate(ConfigValues.websiteUrl);
}
Reference
Create a time-out for webbrowser loading method
webbrowser timeout if page wont load