Cell Double Click event is not running. only Cellclick event gets executed - c#

i have both the events cellclick and celldoubleclick for a datagridview in my window forms application.
The problem is that when i double click , only the cellclick event triggers, as it cannot detemine if its single click or double click.
i searched for this and found that timers could be the solution..but how to do that ?
plz help

You probably ought to try to find out why double-click isn't getting fired. Answering your question: you'll indeed need a timer whose Interval you set to the double-click time:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
timer1.Interval = SystemInformation.DoubleClickTime;
timer1.Tick += delegate { timer1.Enabled = false; };
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
if (timer1.Enabled) {
timer1.Enabled = false;
// Do double-click stuff
//...
}
else {
timer1.Enabled = true;
// Do single-click stuff
//...
}
}
}

I established a little solution for this:
add timer in your form
public Form1()
{
timer1.Interval = SystemInformation.DoubleClickTime;
}
bool double_click = false;
//
DataGridViewCellEventArgs sendedEvent = null;
private void DatagridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
double_click = true;
sendedEvent = e;
}
private void DatagridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
sendedEvent = e;
//
timer1.Enabled = true;
//
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (!double_click)
{
// DO your simple click stuff and USE sendedEvent if needed
}
else
{
double_click = false;
// DO your Doubleclick stuff and USE sendedEvent if needed
}
timer1.Stop();
timer1.Enabled = false;
}
Hope this will help ^^

Related

Detect if ListView or CollectionView is scrolling by user

Is there any way to detect if ListView or CollectionView is scrolling by user and not from ScrollTo method?
I am using ScrollTo as the example below:
colViewCategories.ScrollTo(categoryItem, null, ScrollToPosition.Center, true);
Or if i can disable Scrolled Event till ScrollTo Method will stop to scroll.
I finally found my answer.
bool scrollAnimationIsRaised=false;
int previousindex = 0;
private System.Timers.Timer tmr = new System.Timers.Timer();
private void InitTimer
{
tmr.Interval = 500; //waiting 500ms after scrollTo has Stop for not having conflicts
tmr.Elapsed += Tmr_Elapsed;
}
private void ButtonClick(object sender, EventArgs e)
{
scrollAnimationIsRaised = true;
listview.ScrollTo(50);
}
private void ListviewItems_Scrolled(object sender, ItemsViewScrolledEventArgs e)
{
if(scrollAnimationIsRaised)
{
//ScrollToEvent Is Fired
if (e.LastVisibleItemIndex == previousindex)
tmr.Start();
previousindex = e.LastVisibleItemIndex;
}
else
{
//ScrollTo event has finished
}
}
private void Tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
tmr.Stop();
scrollAnimationIsRaised = false;
}

Enabling the SHOP Button from form2 closing event

I have a little problem that I can't enable the shop button when the form2 closes.
So, I was making a shop for my clicker game. When you press the SHOP button your given a form2 (which was supposed to be the shop, WIP) and the shop button (button2) should be disabled because you can spam the shop button when its not disabled. After closing the shop the button was supposed to be enabled again, but I can't get it to work.
Note: the Form 2 Close button for the shop is sCloseBtn.
Form 1 Code:
private void button1_Click(object sender, EventArgs e)
{
i += 1;
if (i == 1)
{
label4.Text = "Cake";
}
else
{
label4.Text = "Cakes";
}
label3.Text = Convert.ToString(i);
}
public void button2_Click(object sender, EventArgs e)
{
WindowsFormsApplications2.Form2 secondForm;
secondForm = new WindowsFormsApplications2.Form2();
if (isInGui == false) {
secondForm.Show();
button2.Enabled = false;
isInGui = true;
}
else if (isInGui == true)
{
button2.Enabled = true;
isInGui = false;
}
}
Form 2 Code:
private void sCloseBtn_Click(object sender, EventArgs e)
{
Close();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
}
All help is appreciated :)
One way to solve that would be to move the event handler "Form2_FormClosing" to Form1,
public partial class Form1 : Form
{
// ...whatever is in your form already...
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
button2.Enabled = true;
isInGui = false;
}
// ...
}
and to assign it to secondForm's event after its creation, ie.
secondForm = new WindowsFormsApplications2.Form2();
secondForm.FormClosing += Form2_FormClosing;

C# specified time label change the structure

int sn = 0;
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "Konfigürasyon Yükleniyor.";
timer1.Interval = 1000;
timer1.Enabled = true;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (sn == 3)
{
label1.Text = "Ayarlar Alınıyor";
}
if (sn == 5)
{
label1.Text = "Program Başlatılıyor";
}
sn++;
timer1.Stop();
}
When I open the form I want to change the label when I select the text range.
I assume that event handler is attached in designer to this timer1.
As far as I can understand, this label is never set, because you stop Timer after it hits first time.
In this case variable sn = 0 so non of if condition from your event handler is met.
I think to solve problem you sould remove this timer1.Stop() from event handler.
You probably want
private void timer1_Tick(object sender, EventArgs e) {
if (sn == 3)
label1.Text = "Ayarlar Alınıyor";
else if (sn == 5) {
label1.Text = "Program Başlatılıyor";
timer1.Stop(); // <- stop timer here on the 5th second, not on the 1st one
}
sn++;
}

How to pause and resume a BackgroundWorker?

This is how I did it in my code:
In the backgroundWorker DoWork event I did:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
_busy.WaitOne();
this.Invoke(new MethodInvoker(delegate { label2.Text = "Website To Crawl: "; }));
this.Invoke(new MethodInvoker(delegate { label4.Text = mainUrl; }));
webCrawler(mainUrl, levelsToCrawl, e);
}
Then in the pause button click event I did:
private void button4_Click(object sender, EventArgs e)
{
_busy.Reset();
}
In the resume button click event I did:
private void button5_Click(object sender, EventArgs e)
{
_busy.Set();
}
But it's not working when I click to start the process:
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
button1.Enabled = false;
this.Text = "Processing...";
label6.Text = "Processing...";
label6.Visible = true;
button2.Enabled = false;
checkBox1.Enabled = false;
checkBox2.Enabled = false;
numericUpDown1.Enabled = false;
button3.Enabled = true;
}
Nothing happen only when I click the resume button the process start then when I click the pause button nothing happen.
I want that when I click the start process button it will start the backgroundWorker regular then when clicking the pause button it will pause and the resume button it will resume.
What did I do wrong ? Can someone fix my code ?
In your BackgroundWorker thread code, you need to find places that are safe to "pause" execution. The ManualResetEvent is the right way to code. This other post might help:
Is there a way to indefinitely pause a thread?
Basically, in a few choice points in your worker thread code where you want to allow it to pause, try inserting:
_busy.WaitOne(Timeout.Infinite);
And when you want to pause (from your main thread) you use:
_busy.Reset();
And to resume:
_busy.Set();
You should be able to do this using the ManualResetEvent like this ...
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
_busy.WaitOne();
test(mainUrl, levelsToCrawl, e);
}
... and then when you want to pause the thread call _busy.Reset() ... and when you want to restart it call _busy.Set().
Additionally, you can place _busy.WaitOne(); anywhere you want to pause.
I've been looking for the answer of this thread but I come up with my own solution i made and i just wanna share it with you. hope this works.
I have a background worker and i want to pause it when i hit close button of my form. asking "You are about to cancel the process" so it should pause the process.
declare bool pauseWorker = false; on your class.
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
while (condition)
{
if (pauseWorker == true)
{
while (pauseWorker == true)
{
if (pauseWorker == false) break;
}
}
else
{
//continue process... your code here
}
}
}
private void frmCmsnDownload_FormClosing(object sender, FormClosingEventArgs e)
{
if (bgWorker.IsBusy)
{
pauseWorker = true; //this will trigger the dowork event to loop that
//checks if pauseWorker is set to false
DiaglogResult x = MessageBox.Show("You are about cancel the process", "Close", MessageBoxButtons.YesNo);
if (x == DialogResult.Yes) bgWorker.CancelAsync();
else
{
e.Cancel = true;
pauseWorker = false; //if the user click no
//the do work will continue the process
return;
}
}
}
Therefore the main solution here is the boolean declaration that controls the DoWork event of BGWorker.
Hope this solution helps your problem. Thank you.
I use a simple class that utilizes System.Thread.Monitor and lock()...
public class ThreadPauseState {
private object _lock = new object();
private bool _paused = false;
public bool Paused {
get { return _paused; }
set {
if(_paused != value) {
if(value) {
Monitor.Enter(_lock);
_paused = true;
} else {
_paused = false;
Monitor.Exit(_lock);
}
}
}
}
public void Wait() {
lock(_lock) { }
}
}
Using it is very simple...
private ThreadPauseState _state = new ThreadPauseState();
private void btnPause_Click(object sender, EventArgs e) {
_state.Paused = true;
}
private void btnResume_Click(object sender, EventArgs e) {
_state.Paused = false;
}
private void btnCancel_Click(object sender, EventArgs e) {
backgroundWorker1.CancelAsync();
_state.Paused = false; // needed if you cancel while paused
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
var worker = (BackgroundWorker)sender;
for(var _ = 0; _ < 100; _++) {
_state.Wait();
if(worker.CancellationPending) return;
Thread.Sleep(100); // or whatever your work is
}
}
This works for me:
bool work = true;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.ProgressChanged += myChangeFunction;
backgroundWorker1.RunWorkerAsync();
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
while (true && work)
{
// Your code here
backgroundWorker1.ReportProgress(0);
Thread.Sleep(1000);
}
e.Cancel = true;
}
private void myChangeFunction(object sender, ProgressChangedEventArgs e)
{
// Here you can change label.text or whatever thing the interface needs to change.
}
private void Stop()
{
work = false;
}
private void Start()
{
work = true;
backgroundWorker1.RunWorkerAsync();
}
NOTE: If you want to change something of the interface, you have to put it in the myChangeFunction(), because in the DoWork() function will not work. Hope this helps.

Timer, click, mousedown, mouseup events not working together

Looking for some help on a problem Im having
sorry if this question has already been asked, I can not find anything similar.
The idea is when a picturebox is clicked changed the image to ON.
If the picture box is held for more than 2 seconds to open a new form and leave the picturebox as OFF.
However if the picturebox is clicked ON and then held for 2 seconds and then returns i need the picturebox state to remain ON.
Here is what I have tried so far.
I believe for this to work correctly I need to stop MouseUp event from occuring.
Is there a way I can stop MouseUp when Tick occurs?
Is there a easier / better way to do this?
Any help would be appreciated.
private void time_HoldDownInternal_Tick(object sender, EventArgs e)
{
time_HoldDownInternal.Enabled = false;
time_HoldDownInternal.Interval = 1000;
form1show.Visible = true;
}
private void pb_pictureBoxTest_MouseDown(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = true;
time_HoldDownInternal.Enabled = true;
}
private void pb_pictureBoxTest_MouseUp(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = false;
//MessageBox.Show("mouse up");
time_HoldDownInternal.Enabled = false;
time_HoldDownInternal.Interval = 1000;
}
private void pb_pictureBoxTest_Click(object sender, EventArgs e)
{
if (mainMenuVariables.mousedown == true)
{
if (mainMenuVariables.pictureBox == false)
{
mainMenuVariables.pictureBox = true;
pb_pictureBoxTest.Image = new Bitmap(mainMenuVariables.pictureBoxOn);
return;
}
if (mainMenuVariables.pictureBox == true)
{
mainMenuVariables.pictureBox = false;
pb_pictureBoxTest.Image = new Bitmap(mainMenuVariables.pictureBoxOff);
return;
}
}
if (mainMenuVariables.mousedown == false)
{
//nothing
}
}
Rather than starting a timer, just record the current time on mouse down. Then in mouse up, check if it has been 2 seconds. e.g:
private void pb_pictureBoxTest_MouseDown(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = true;
mainMenuVariables.mousedowntime = DateTime.Now;
}
private void pb_pictureBoxTest_MouseUp(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = false;
var clickDuration = DateTime.Now - mainMenuVariables.mousedowntime;
if ( clickDuration > TimeSpan.FromSeconds(2))
{
// Do 'hold' logic (e.g. open dialog, etc)
}
else
{
// Do normal click logic (e.g. toggle 'On'/'Off' image)
}
}

Categories