C# Winforms change label based on running process - c#

I have a dotnet 2.0 app in C# that I have developed in Visual Studio 2008, it's pretty simple:
I would like to change the label name currently called "label1" to "Running" or "Stopped" when my process loop.exe is running or stopped.
When I press start, it will run loop.exe and the stop button will obviously stop it.
I've read a lot of topic on C# Winforms but I cannot get this working and I have no idea what do to now. I think that I need to add a backgroundworker but I don't know how to check the process and update the label programmatically.
Here's my clean/current code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
namespace APP_NAME
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Start_Click(object sender, EventArgs e)
{
Process.Start("loop.exe");
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Process.Start("taskkill", "/F /IM loop.exe");
Process.Start("taskkill", "/F /IM azcopy.exe");
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button3_Click(object sender, EventArgs e)
{
Process.Start("notepad.exe", #"C:\ProgramData\APP_NAME\settings\settings.xml");
}
private void button4_Click(object sender, EventArgs e)
{
Process.Start("explorer.exe", #"C:\ProgramData\APP_NAME\logs");
}
}
}
I hope I made myself clear, thank you.

You can update the text shown by a label by accessing it's Text property. So, change your method like:
private void Start_Click(object sender, EventArgs e)
{
Process.Start("loop.exe");
label1.Text = "Running";
}

//hi
//Do this to see the label value changes in real time in the window
private void Start_Click(object sender, EventArgs e)
{
Process.Start("loop.exe");
label1.Text = "Running";
lable1.Refresh();
}

Related

By pressing a button i want to show the user all the data inserted to the boxes

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 Apostolh_Texnikou
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void GN_TextChanged(object sender, EventArgs e)
{
}
private void CP_TextChanged(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
I want the user to click in the ready button and a window to pop out.
In the window the user must be able to copy the text.
I found how to make a pop window but its just a window which doesn't allow
copy paste. thank you in advance
You might want to look into a more complex type of dialog box. Perhaps one with a textbox? This will require launching another form. (see how to display textBox control in MessageBox?)

Windows Form is Really Laggy

I have a windows Form Project that runs smoothly and fine on framework 4.5.1
But yet I have a project on framework 2.0 that is so laggy even after i changed the target framework to 4.5.1
so why is one project slow and the other is normal?
Update :
here is the Main From
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DataBaseLab_Library_Project
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button5_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
MessageBox.Show(" ", "About US");
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
Add AddForm = new Add();
AddForm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
Search SearchForm = new Search();
SearchForm.Show();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
}
}
and here is its design
Update 2 :
Form "Add.cs" that dose not lag .
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DataBaseLab_Library_Project
{
public partial class Add : Form
{
public Add()
{
InitializeComponent();
}
private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
AddBook AddBookForm = new AddBook();
AddBookForm.Show();
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
MainForm mainForm = new MainForm();
mainForm.Show();
}
private void label3_Click(object sender, EventArgs e)
{
}
private void publisherButt_Click(object sender, EventArgs e)
{
this.Close();
AddPublishercs AddPublisherForm = new AddPublishercs();
AddPublisherForm.Show();
}
private void authorButt_Click(object sender, EventArgs e)
{
//this.Close();
//AddAuthor AddAuthorForm = new AddAuthor();
//AddAuthorForm.Show();
}
private void Add_Load(object sender, EventArgs e)
{
}
}
}
Try removing the panel1 paint eventhandler. This will get called (even tho its empty) ever time the panel has to draw itself, which is all the time in a designer environment.
Remove this code:
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
There will also be code inside of InitializeComponent() similar to this
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1);
Remove it as well. Clean and rebuild solution. Close all designer windows. Try again.

is there a way to stop a timer on visual studios at a certain point instead of it keep going

basically I mean I want a picture to move when I press a button and the picture to sop moving at a certain point on the form application, not cancel it the picture to move then just stop with the application still running. on Microsoft visual studios c# windows form application
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 Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Left += 2;
}
}
}
You can call timer1.Stop() to stop the timer.
You need to set the Interval property of the timer to raise the TickEvent once after that Interval is Elapsed.
In TickEvent you can Stop the Timer or do whatever you want to do.
Try This :
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval=5000;//to raise tick event for 5 sec's
timer1.Start();
timer1.Tick += new System.EventHandler(this.timer1_Tick);
}
protected void timer1_Tick(Object sender,EventArgs e)
{
//Stop the timer here
timer1.Stop();
}

Object reference not set to an instance of an object (Calculator project error)

Ok, I'm pretty sure my code is all correct, so I'm not sure why I'm getting this error. :P Here is my code -->
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Calculator
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); //This is where I am getting the error at
}
}
}
That's in my program.cs file. Here's what's in my Form1.cs file. -->
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 Calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn0_Click(object sender, EventArgs e)
{
txtbox.Text += btn0.Text;
}
private void btn1_Click(object sender, EventArgs e)
{
txtbox.Text += btn1.Text;
}
private void btn2_Click(object sender, EventArgs e)
{
txtbox.Text += btn2.Text;
}
private void btn3_Click(object sender, EventArgs e)
{
txtbox.Text += btn3.Text;
}
private void btn4_Click(object sender, EventArgs e)
{
txtbox.Text += btn4.Text;
}
private void btn5_Click(object sender, EventArgs e)
{
txtbox.Text += btn5.Text;
}
private void btn6_Click(object sender, EventArgs e)
{
txtbox.Text += btn6.Text;
}
private void btn7_Click(object sender, EventArgs e)
{
txtbox.Text += btn7.Text;
}
private void btn8_Click(object sender, EventArgs e)
{
txtbox.Text += btn8.Text;
}
private void btn9_Click(object sender, EventArgs e)
{
txtbox.Text += btn9.Text;
}
private void btnPoint_Click(object sender, EventArgs e)
{
if (txtbox.Text == "0")
{
txtbox.Text = "0.";
}
else
{
txtbox.Text += btnPoint.Text;
}
}
private void btnPlus_Click(object sender, EventArgs e)
{
//Add plus code here
}
private void btnMinus_Click(object sender, EventArgs e)
{
//Add minus code here
}
private void btnTimes_Click(object sender, EventArgs e)
{
//Add times code here
}
private void btnDivide_Click(object sender, EventArgs e)
{
//Add divide code here
}
private void btnEqual_Click(object sender, EventArgs e)
{
//Add equal code here
}
private void btnClear_Click(object sender, EventArgs e)
{
txtbox.Clear();
}
}
}
The error occurs when I attempt to enter in a number and then a decimal. For example, I press "2" and then ".". What happens is the error "Object reference not set to an instance of an object." pops up. Help is appreciated, thanks. Sorry if it's a stupid error. :P
Several helpful ideas have been given to you in the comments to your question, so I will try to aggregate what is the most preferred analysis route here and add my own comments:
First, the line where the exception occur (Application.Run(new Form1());) is not the real place to look for the bug. Application.Run is a WinForm infrastructure method which does some magic and then runs your code.
Second, the best way to track down where the issue is is by setting a breakpoint in the btnPoint_Click method. I assume you are new to WinForm and Visual Studio, so my suggestion is searching for "Winform Debug" in youtube - there are many videos showing how to do exactly that. Basically the idea is clicking with the mouse on the left side of the line you want to break into (or one line below if it's a method declaration like in your case), and then, run your application from within Visual Studio by pressing F5. This will enable Visual Studio to automatically attach to your WinForm process, and let you see variables in runtime.
This will allow you to continue line by line (clicking F10) and navigating to the exact place in code where the exception occur.
Last, since this happens when clicking on the '.' button, the only two variables that can cause this type of exception are txtbox and btnPoint, and if one of them is null - that would be it.
If you hover with the mouse on the varibale name during debugging (following the instructions above), you should see whether the content of the variable is null or not (null will simply say 'null').

button push increment number to a text box c# winform

This is for a project I'm doing for class, I'm trying to create a win form that will have 2 buttons one that will increment in the text box when the button is pushed and one that will decrement when a different button is pushed. I'm having trouble finding the right line that will do what I want. Is there someone that could help me out?
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 Project10TC
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Teancum Project 10");
}
private void button1_Click(object sender, EventArgs e)
{
int i = 1;
textBox1.Text = Convert.ToString(i++);
}
private void button2_Click(object sender, EventArgs e)
{
int i = 1;
textBox1.Text = Convert.ToString(i--);
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Clear();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Since its a class project, I can only give you a hint.
You need to define the variable i outside of your button click events. Use the same variable in both events.
Also look at the difference between i++ and ++i
Declare the i variable as a field. Besides I would use ++i instead of i++. Otherwise you have different values in the textbox and in the variable. Also, no need to use Convert.ToString().
public partial class Form1 : Form
{
int i;
public Form1()
{
InitializeComponent();
i = 0;
}
//...
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = (++i).ToString();
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = (--i).ToString;
}
}

Categories