When playing with clickevents in visual studio i came accross this error:
private void pictureBox1_Click(object sender, EventArgs e)
{
Testcounter = 0;
pictureBox1.MouseClick += myMouseClickEventFunction;
}
private void myMouseClickEventFunction(object sender, MouseEventArgs e)
{
int x = colors.GetUpperBound(0) + 1;
int y = colors.GetUpperBound(1) + 1;
Testcounter++;
var point = new Point(e.X - pictureBox1.Width/2, e.Y - pictureBox1.Height/2);
for (int i = 0; i < x; i++)
{
for (int u = 0; u < y; u++)
{
if (cirkles[i, u].Contains(point))
{
changeIndex(i, u);
}
}
}
this.Refresh();
}
The first time i click my picturebox the counters value is 1, the second time the value is 2, 3th time 3,... Does anyone has any idea why this happends? thnx
pic1
pic2
Because by executing this
pictureBox1.MouseClick += myMouseClickEventFunction;
You're adding the handler one more time with each click. Which should mean, that if you click it once, you add it once and it executes once. But with the second click, you add it one more time, so this time it will execute two times and that's why your counter is increasing to 2. What you need is to move your click handler somewhere else and register it only one time, which means that the best place to move it should be in the initialization of the form. (In public MainForm(){} or whatever form you're using the code in)
P.S.: Sorry for the poor english, I hope you understood me.
Related
I would like to make a clicks counter with a button, that's my code.
I want to make a click counter, with the help of a button, which at each click, would display in a text box a number (1 then 2 ...) Can you help me?
private vous btn_click(object sender, EventArgs e)
{
do
{
int i = 0;
label1.Text = i.ToString();
break;
}
while (i < 10);
{
i++;
}
Just to put you on the right track. The following displays from 1 to 10 and 'round again. ;-)
int x = 0;
void btn_click(object sender, EventArgs e)
{
label1.Text = ++x.ToString();
if(x==10) x=0;
}
Please try to learn some basics first.
i create a subclass datagridview to override the mousewheel event to catch mouse scroll then send key UP or DOWN. i create a datatable to be bind as datasource for mydatagridview by button click
private void button1_Click(object sender, EventArgs e)
{
DataTable myDataTable = new DataTable();
int NUM_ROWS = 150;
int NUM_COLS_TO_CREATE = 10;
for (int i = 0; i < NUM_COLS_TO_CREATE; i++)
{
myDataTable.Columns.Add("x" + i, typeof(string));
}
for (int i = 0; i < NUM_ROWS; i++)
{
var theRow = myDataTable.NewRow();
for (int j = 0; j < NUM_COLS_TO_CREATE; j++)
{
theRow[j] = "whatever";
}
//add the row *after* populating it
myDataTable.Rows.Add(theRow);
}
MyDataGridView1.DataSource = myDataTable;
}
the code that override the mousewheel event as this
public partial class MyDataGridView : DataGridView
{
protected override void OnMouseWheel(MouseEventArgs e)
{
if (e.Delta < 0)
SendKeys.Send("{DOWN}");
else
SendKeys.Send("{UP}");
}
}
Its working fine if we use mouse wheel to scroll each item in a SLOW way, but if you scroll too FAST using the mouse wheel, somewhat the datagridview becomes lagging.
As example from row 1 to 5, it will jump the row from 1 to 3,then 3 to 5 something like that, here come another weird issue. i use "Navicat" a lot in my daily basis..
so if i open both my application and Navicat. the mouse wheel scrolling now become very smooth on my application even if i scroll too fast. but then if i close Navicat then scrolling become lagging again. what was causing this? i am very sorry if i cant explained it well, all i want is just want to makes the scrolling each item smooth. Any suggestion?
as #Bozhidar mentioned that i should better handling the MouseWheel event instead or overriding it. so i've come up with the solution just in case anyone need it too.
in Form_Load add
MyDataGridView1.MouseWheel += new MouseEventHandler(MyDataGridView1_MouseWheel);
then place this anywhere inside your class
private void MyDataGridView1_MouseWheel(object sender, MouseEventArgs e)
{
HandledMouseEventArgs hme = (HandledMouseEventArgs)e;
hme.Handled = true;
int rowIndex = MyDataGridView1.CurrentCell.RowIndex;
int cellIndex = MyDataGridView1.CurrentCell.ColumnIndex;
MyDataGridView1.CurrentCell = MyDataGridView1.Rows[e.Delta < 0 ? Math.Min(rowIndex + 1, MyDataGridView1.RowCount - 1) : Math.Max(rowIndex - 1, 0)].Cells[cellIndex];
}
The SendKeys method is not as reliable when it comes to precise timing - see the official documentation. Try setting the "SendKeys" app setting to "SendInput" in order to force the new behavior.
But you'd be better off handling the MouseWheel event instead of overriding it. You need to hook it by hand - not sure why it isn't present into the Property Window. Given your DataGridView is named dgv:
private void Form1_Load(object sender, EventArgs e)
{
dgv.MouseWheel += Dgv_MouseWheel;
}
Next, have you considered FirstDisplayedScrollingRowIndex? Just set it as appropriate in the event, and set the Handled flag, like this:
private void dgv_MouseWheel(object sender, MouseEventArgs e)
{
dgv.FirstDisplayedScrollingRowIndex += 3;
var he = (HandledMouseEventArgs);
he.Handled = true;
}
Here's an approach that preserves the default scrolling behavior of scrolling the viewport, but not changing the row selection.
It also performs much faster than the built-in mouse wheel handler when millions of rows are being handled in a virtual grid:
private void Form1_Load(object sender, EventArgs e)
{
DataGridView1.MouseWheel += DataGridView1_MouseWheel;
}
private void DataGridView1_MouseWheel(object sender, MouseEventArgs e)
{
var hme = e as HandledMouseEventArgs;
hme.Handled = true;
int displayedRowIndex = DataGridView1.FirstDisplayedScrollingRowIndex;
// each "detente" scroll appears to create a delta of 120
// dividing delta by 120 to get the number of rows scrolled
// taking the negative of the delta so that it can be added to the displayedRowIndex intuitively as negative is down, positive is up
var rowDelta = -(e.Delta / 120);
var newDisplayedRowIndex = e.Delta < 0 ? Math.Min(displayedRowIndex + rowDelta, DataGridView1.RowCount - 1) : Math.Max(displayedRowIndex + rowDelta, 0);
DataGridView1.FirstDisplayedScrollingRowIndex = newDisplayedRowIndex;
}
I have 10 strings, all named q1,q2,q3, etc.
My question is, on button click, how do I make them cycle and display within a button?
Current code:
private void nButton_Click(object sender, EventArgs e)
{
for (int g = 0; g <= 10; g++)
{
rBox.Text = q(g);
}
}
Clearly q(g) does not cycle appropriately, so I have come to you, Oracles of code, how would I accomplish this?
** Alternatively, if I wanted to remove the for loop, and instead would just want to increment g by one every time until 10, I assume the structure would resemble something like the following:
private void nButton_Click(object sender, EventArgs e)
{
g++
rBox.Text = q(g);
}
However the question persists, how would I cycle through these strings?
EDIT: I've discovered these neat things called Lists, so I simply created a new list with
List<string> questionNumber = new List<string>();
Then add the string
questionNumber.Add(q1);
As lastly display it through the text box with simple incrementation
private void nButton_Click(object sender, EventArgs e)
{
g++;
rBox.Text = questionNumber[g];
}
The easiest way would be putting them into an array and iterate over the array whenever you wanna operate on your strings.For example:
var values = new [] { q1, q2, q3, ... };
for (int g = 0; g < 10; g++)
{
rBox.Text += values[g];
}
If your intention was to display one string at a time, on each click you can do so by creating a counter variable outside of the click event and increment it per click and just fecth the string at that index:
int index = 0;
private void nButton_Click(object sender, EventArgs e)
{
if(index != values.Length)
{
rBox.Text = values[index];
index++;
}
}
You need to declare values a field or property of your class, and initialize it with your strings.In fact you can completely remove the variables and just use an array or list to store your values.
I want to move a label ( named Label2 ) to the right by pressing a button and when I press the button again I want it to move again ( Hope You Understand )
This is my code:
private void button3_Click(object sender, EventArgs e)
{
int x = 28;
x++;
label2.Location = new Point(x, 63);
}
But it doesn't work. What am I doing wrong?
This is because you are using a local int x, so it will be reset to 28 each time you click the button.
Move the declaration of x outside the button3_Click() method so that 'x' is a field. Then it will retain its value between each button click.
Obviously you will need to give it a better name; perhaps currentLabelLeft.
This will move it right by 1 pixel each time:
private void button3_Click(object sender, EventArgs e)
{
int x = label2.Location.X;
x++;
label2.Location = new Point(x, 63);
}
Create two css classes and set them as follows (if you have to set it from code behind):
label2.CssClass = "move_to_right"
i want to develop a video player which has to play different parts in a video (specified by their start n end positions). I am using directx.audiovideoplayback dll for this purpose.
the starting and ending positions are stored in an array.
eg- {2,6,8,19} tells that segment between 2nd to 6th second has to be played and then 8th to 19th second should be played.
my problem is that despite me giving condition that
if(video_obj.CurrentPosition==endtime) video_obj.Stop();
the video isnt stopping..
the video is playing from 2nd position to end of file.
code is
public static int[] seconds = { 3,8,12,16};
public static int start, end;
private void btnPlay_Click(object sender, EventArgs e)
{
vdo.Owner = panel1;
panel1.Width = 300;
panel1.Height = 150;
vdoTrackBar.Minimum = 0;
vdoTrackBar.Maximum = Convert.ToInt32(vdo.Duration);
if (vdo != null)
{
vdo.Play();
timer1.Start();
}
}
private void vdoTrackBar_Scroll(object sender, EventArgs e)
{
if (vdo != null)
{
vdo.CurrentPosition = vdoTrackBar.Value;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
int i;
for (i = 0; i < seconds.Length; i = i + 2)
{
start = seconds[i];
vdo.CurrentPosition = start;
end = seconds[i + 1];
vdoTrackBar.Value = (int)vdo.CurrentPosition;
MessageBox.Show("Starts at " + vdo.CurrentPosition + " and Ends at " + end);
if (vdo.Paused)
vdo.Play();
if (vdo.Playing)
{
if (vdo.CurrentPosition == vdo.Duration)
{
timer1.Stop();
vdo.Pause();
vdoTrackBar.Value = 0;
}
if (vdo.CurrentPosition == end)
{
timer1.Stop();
vdo.Pause();
}
vdoTrackBar.Value += 1;
}
}
Help! Somewhere something is wrong and i have no clue about it
How do i correct it?
Video starts playing when i
So there still isn't enough info in the post to really definitively say whats going wrong. I'm guessing that your timer isn't ticking with enough resolution to happen to tick exactly when the for loop in the timer tick would coincide with the video's current position.
How often does the timer tick? What's the precision of the video's current position?