I dont know if I'm doing this correctly but I have a grid and I loop through the grid to see if the items matched. If they do I want to make the row flash every 3 seconds. Right now what I have in my code just pretty much highlight the row but no flashing. Can anyone help take a look?
public static void CheckRow(int item, DataGridViewRow row)
{
List<int> col = new List<int>();
//call to db and add to col
foreach (var item in col)
{
if (item == col.Item)
{
currentRow = row;
Timer t = new Timer();
t.Interval = 3000;
t.Tick += new System.EventHandler(Highlight);
t.Start();
}
}
}
private static void Highlight(object sender, EventArgs e)
{
currentRow.DefaultCellStyle.BackColor = Color.Brown;
}
Wouldn't you need to change the color again (to the original) to have a flashing effect?
You should use Threading. Look at the code :)
bool go = false; //for changing cell color
int count = 10; //to stop timer (blinking)
public blinkForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
Thread a = new Thread(blink);
a.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = false;
if (dataGridView1.Columns.Count == 0)
{
//generate new columns for DataGridView
dataGridView1.Columns.Add("user", "User");
dataGridView1.Columns.Add("pcStatus", "PC Status");
dataGridView1.Columns.Add("service", "Servis");
//generate new rows for DataGridView
dataGridView1.Rows.Add("Ali", "PC007", "chrome.exe");
dataGridView1.Rows.Add("Vusal", "PC010", "photoshop.exe");
dataGridView1.Rows.Add("Rahim", "PC015", "chrome.exe");
}
}
private void blink(object o)
{
while (count > 0)
{
while (!go)
{
//change color for binking
dataGridView1.Rows[0].Cells["service"].Style.BackColor = Color.Tomato;
go = true;
//stop for 0.5 second
Thread.Sleep(500);
}
while (go)
{
//change color for binking
dataGridView1.Rows[0].Cells["service"].Style.BackColor = Color.LimeGreen;
go = false;
//stop for 0.5 second
Thread.Sleep(500);
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
count--;
if (count == 0)
{
//stop blinking after 10 second
timer1.Stop();
}
}
Perhaps this, no?
private static void Highlight(object sender, EventArgs e)
{
currentRow.DefaultCellStyle.BackColor = Color.Brown;
System.Threading.Thread.Sleep(2000);
currentRow.DefaultCellStyle.BackColor = Color.White;
}
Related
I have a list
static List<Participants> soop = ParticipantRepository.GetAllParticipants();
It has some 800 items. Then there's a label and a timer. At timer_tick, I want to display one of the items randomly. Here's the code for that event
private void timer1_Tick(object sender, EventArgs e) {
foreach (var participants in soop)
{
a = participants.RollNumber;
label1.Text = a;
break;
}
counter++;
if (counter == 200) {
timer1.Stop();
pictureBox5.Visible = false;
counter = 0;
}
}
I have not been able to achieve the random functionality so far because only one RollNumber is being displayed and then the timer takes its time and runs out. What am I doing wrong?
I would suggest using the random class.
Random randomGen = new Random();
private void timer1_Tick(object sender, EventArgs e)
{
var i = randomGen.Next(0, soop.Count);
label1.Text = soop[i].RollNumber;
counter++;
if (counter == 200)
{
timer1.Stop();
pictureBox5.Visible = false;
counter = 0;
}
}
At each tick the timer1_Tick is called, so your foreach loop starts back from the beginning and you end up displaying the first item every time. Instead, you can store the index of the last item you displayed. You already have counter so let's use it:
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = soop[counter % soop.Count].RollNumber;
counter++;
if (counter == 200) {
timer1.Stop();
pictureBox5.Visible = false;
counter = 0;
}
}
I am having a problem . I want to use if statement to check if a button is clicked. For Example:
public void button1_Click(object sender, EventArgs e)
{
while (1)
{
...
...
...
if (Button2 == clicked)
{
break;
}
}
}
But it's not working like this, because the ".click" can only be on the left side of "+=" or "-=". Any idea how i can check if Button2 is clicked?
the code is loking like this: and i want to check button2 to stop the "programm".
the check for the Button2 is nearly at the end of the code ;)
public void button1_Click(object sender, EventArgs e)
{
Random rnd = new Random();
int EmFilterPos;
int ExFilterPos;
string String1;
int[] EmLB = new int[126];
int[] ExLB = new int[126];
int LBEmAnzahl = 0;
int LBEmTot = 0;
int LBExAnzahl = 0;
int LBExTot = 0;
UInt32 C_Zyklen;
UInt32 Zyklen;
Roche.DetectionControl2.Device_Filterwheels.ELBPowerState LB_On = Roche.DetectionControl2.Device_Filterwheels.ELBPowerState.LBOn;
Roche.DetectionControl2.Device_Filterwheels.ELBPowerState LB_Off = Roche.DetectionControl2.Device_Filterwheels.ELBPowerState.LBOff;
Roche.DetectionControl2.Device_Filterwheels.fiweGetLBResponse LightBarrier;
string Text = String.Format("Filterrad-Dauertest\r\nGestart am {0:d} um {0:t}\r\n\r\n", DateTime.Now);
System.IO.File.WriteAllText(#"TestLogFile\Filterrad_Dauertest1.txt", Text);
Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweInitFilter();
System.Threading.Thread.Sleep(50);
while (Zyklen <= 20)
{
for (int q=1;q<8;q++)
{
Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweMove(q,q);
System.Threading.Thread.Sleep(50);
Zyklen++;
}
for (int w=0;w<7;w++)
{
ExFilterPos = rnd.Next(1,8);
EmFilterPos = rnd.Next(1,8);
Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweMove(ExFilterPos,EmFilterPos);
System.Threading.Thread.Sleep(50);
Zyklen++;
}
C_Zyklen = Zyklen;
if ((C_Zyklen % 2) < 14)
{
Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweInitFilter();
System.Threading.Thread.Sleep(50);
using (System.IO.StreamWriter file = new System.IO.StreamWriter (#"TestLogFile\Filterrad_Dauertest1.txt", true))
{
file.Write("Init bei: ");
String1 = String.Format("{0,7}",Zyklen);
file.Write(String1);
file.Write(file.NewLine);
}
ExFilterPos = 60;
EmFilterPos = 60;
Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweRawMove(ExFilterPos,EmFilterPos);
System.Threading.Thread.Sleep(50);
Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweSetLB(LB_On);
while (EmFilterPos != -60)
{
LightBarrier = Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweGetLB();
if (LightBarrier.LBEm == Roche.DetectionControl2.Device_Filterwheels.ELBState.LBbright)
{
LBEmAnzahl++;
LBEmTot += EmFilterPos;
}
if (LightBarrier.LBEx == Roche.DetectionControl2.Device_Filterwheels.ELBState.LBbright)
{
LBExAnzahl++;
LBExTot += ExFilterPos;
}
ExFilterPos--;
EmFilterPos--;
Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweRawMove(ExFilterPos,EmFilterPos);
}
EmFilterPos = LBEmTot / LBEmAnzahl;
ExFilterPos = LBExTot / LBExAnzahl;
using (System.IO.StreamWriter file = new System.IO.StreamWriter (#"TestLogFile\Filterrad_Dauertest1.txt", true))
{
file.Write("Nullstelle Mittelposition Em-Filter: ");
file.Write(EmFilterPos);
file.Write(file.NewLine);
file.Write("Nullstelle Mittelposition Ex-Filter: ");
file.Write(ExFilterPos);
file.Write(file.NewLine);
file.Write(file.NewLine);
}
Instrument.N1_DetectionControl2_1_Device_Filterwheels.fiweSetLB(LB_Off);
}
if (Button2 == clicked) // or something like this
break;
}
using (System.IO.StreamWriter file = new System.IO.StreamWriter (#"TestLogFile\Filterrad_Dauertest1.txt", true))
{
file.Write("Beendet am {0:d} um {0:t}\r\n", DateTime.Now);
}*/
}
Hm...
bool b1clicked = false, b2clicked = false;
public void button2_Click(object sender, EventArgs e)
{
b2clicked = true;
}
public void button1_Click(object sender, EventArgs e)
{
b1clicked = true;
if (b1clicked && b2clicked)
{
//...
}
}
Beside the weird behavior you want..and since you are not using Threads, you have the following options:
Local functions (.Net > 4.7)
private void B_Click(object sender, EventArgs e)
{
bool clickFlag = false;
void Click(object sender2, EventArgs e2)
{
clickFlag = true;
}
b2.Click += Click;
while (!clickFlag)
{
Thread.Sleep(1);
}
b2.Click -= Click;
//Continue with your stuff
}
Threads
Thread newThread;
private void Button1_Click()
{
newThread = new Thread(YourBreakableProcess);
newThread.Start();
}
private void Button2_Click()
{
newThread.Join();
}
private void YourBreakableProcess()
{
//Your breakable process
}
Async methods.
I hope you find a solution. Cheers.
Edit:
Since what you want is to interrupt the process of whatever you are doing, the only option you have is Local fuctions as shown above, if you are not tied to a specific framework version.
BackgroundWorker and check in every step if the button 2 was pressed with the flag thing mentioned in other answer.
Threads, and make a thread.Join when the button 2 is pressed.
Edit 2:
Updated answer with Threads, I will recommend that if you go with this option it is much better to use a BackgroundWorker instead as you will have the whole control of the process breaking it only in the place where it would be fine to break it.
You can achieve this using a flag variable. Declare and initialize flag value to false.On button2 click change flag value to true as follows,
private bool flag= false;
private void button2_Click(object sender, EventArgs e)
{
flag= true;
}
public void button1_Click(object sender, EventArgs e)
{
//Use flag to check whether button 2 has clicked or not
if (flag)
{
}
else
{
}
}
I am working on a simple mediaplayer application. It works great but I want to add some extra features. I have added a trackbar control.How can i set trackbar length the same as the music's length ?
Like if the song is halfways the trackbars halfways.This is what I have so far
string[] files, indexed_files;
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK) {
files = ofd.SafeFileNames;
indexed_files = ofd.FileNames;
for (int i = 0; i < files.Length; i++)
{
listBox1.Items.Add(files[i]);
}
}
button4.Enabled = true;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = indexed_files[listBox1.SelectedIndex];
progressBar1.Maximum =(int) axWindowsMediaPlayer1.currentMedia.duration;
axWindowsMediaPlayer1.PlayStateChange += axWindowsMediaPlayer1_PlayStateChange;
}
void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
trackBar1.Value = (int)axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
}
int index = 0;
private void button4_Click(object sender, EventArgs e)
{
if (listBox1.Items.Count != 0) {
axWindowsMediaPlayer1.URL = indexed_files[index];
trackBar1.Maximum = (int)axWindowsMediaPlayer1.currentMedia.duration;
index++;
index = (index % listBox1.Items.Count);
}
}
This will bring you the desired outcome.In my example i just placed the url in the form load for demonstration purposes.The openstatechanged event its to set the trackbar maximum since you need to wait for the player to load the file,after that the code its pretty self-explanatory:
public partial class Form1 : Form
{
Timer t;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = "YourUrlHere";
t = new Timer();
t.Interval = 1000;
t.Tick += new EventHandler(t_Tick);
}
void t_Tick(object sender, EventArgs e)
{
trackBar1.Value = (int)this.axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
}
private void axWindowsMediaPlayer1_OpenStateChange(object sender, AxWMPLib._WMPOCXEvents_OpenStateChangeEvent e)
{
if (axWindowsMediaPlayer1.openState == WMPLib.WMPOpenState.wmposMediaOpen)
{
trackBar1.Maximum = (int)axWindowsMediaPlayer1.currentMedia.duration;
t.Start();
}
}
}
Yes its a timer:),and probably it is best to set it bellow 1000 for reasons of delay.
So you should now add a timer and insert the following code in timer Tick event handler:
trackbar.Value = this.axWindowsMediaPlayer1.ctlControls.CurrentPosition;
I'm populating a datagridview with
MyDataGridView.DataSource = MyList;
where MyList is a List < MyClass > .
Now i want a progressbar to be filled while datagridview is populating.
I can do MyProgessBar.Maximum = MyList.Count, but how can I set MyProgressBar value every time a row is drawn?
here's solution with backgroundworker...I didnt find anything else but this works for what I have to do
private void PopulateDataGridView()
{
MyProgressBar.Value = 0;
MyProgressBar.Maximum = MyList.Count;
MyBackGroundWorker.RunWorkerAsync();
MyDataGridView.DataSource = MyList;
}
and then
private void MyBackGroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
while (true)
{
MyBackGroundWorker.ReportProgress(MyDataGridView.Rows.Count);
if (MyDataGridView.Rows.Count == MyProgressBar.Maximum)
{
break;
}
System.Threading.Thread.Sleep(100);
}
}
private void MyBackGroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
MyProgressBar.Value = MyDataGridView.Rows.Count;
}
Can you help? In C#, after clicking on button1, checkBoxWMVFile (the time interval)
should be switched on and off.
private void button1_Click(object sender, EventArgs e)
{
if (timercheckbox.Enabled == true)
{
timercheckbox = new Timer();
timercheckbox.Start();
timercheckbox.Interval = 10000; // 10 second
if(timercheckbox.Enabled)
{
timercheckbox.Start();
checkBoxWMVFile.Checked = true;
}
else
{
timercheckbox.Stop();
checkBoxWMVFile.Checked = false;
}
}
}
As I understood, you need something like this
private Timer _timer = new Timer();
public Form1()
{
InitializeComponent();
_timer.Interval = 10000;
_timer.Tick += new EventHandler(_timer_Tick);
}
private void button1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
checkBox1.Checked = false;
if(_timer.Enabled)
_timer.Stop();
}
else
{
checkBox1.Checked = true;
if (!_timer.Enabled)
_timer.Start();
}
}
void _timer_Tick(object sender, EventArgs e)
{
//do something here
throw new NotImplementedException();
}
In order for your code to work, you have to reverse the logic i.e.
private void button1_Click(object sender, EventArgs e)
{
if(checkBoxWMVFile.Checked == false)//if the textbox is not checked
{
if (timercheckbox == null)//If this is the first time
{
timercheckbox = new Timer();//Create a timer
timercheckbox.Interval = 10000; // Set the interval, before starting it.
timercheckbox.Start();//Start it
}
else timercheckbox.Start();//If it is not the first time, just start it
checkBoxWMVFile.Checked = true;//Check the checkbox
}
else//the checkbox is checked
{
timercheckbox.Stop();//Stop the timer
checkBoxWMVFile.Checked = false;//Uncheck the checkbox
}
}
This code works correctly:
// InitializeComponent
this.timercheckbox.Interval = 5000;
this.timercheckbox.Tick += new System.EventHandler(this.timercheckbox_Tick);
private void timercheckbox_Tick(object sender, EventArgs e)
{
checkBoxWMVFile.Checked = false;
checkBoxWMVFile.Checked = true;
}
private void button1_Click(object sender, EventArgs e)
{
if( timercheckbox.Enabled == true )
{
timercheckbox.Enabled = false;
button1.Text = "Start Auto save";
}
else
{
timercheckbox.Interval = (int)numericChnageTime.Value;
timercheckbox.Enabled = true;
checkBoxWMVFile.Checked = true;
button1.Text = "Stop Auto save";
}
}