简体   繁体   中英

C# Position Question

Ive got this following code in VB, want to use it into c# and I thought I had it right but when I click the event it only takes me to record #2, I have several records in my table.

private void pictureBox2_Click(object sender, EventArgs e)
{
    sF1411BindingSource.Position = +1;
}

Same for when I hit back. If I'm on record 12 for example, I click the event and bam to the 1st record. I'm a Vb guy and I'm trying to teach myself c# here so maybe I'm over looking something.

private void pictureBox1_Click(object sender, EventArgs e)
{
    sF1411BindingSource.Position = -1;
}

Thanks!

+= and -= for incrementing and decrementing is the same in C# and VB.

sF1411BindingSource.Position += 1; 

sF1411BindingSource.Position -= 1;

如果要增加值,请使用+ =: Position += 1;

I think that you want to do += and -=:

private void pictureBox2_Click(object sender, EventArgs e)
{
    sF1411BindingSource.Position += 1;
}

and

private void pictureBox1_Click(object sender, EventArgs e)
{
    sF1411BindingSource.Position -= 1;
}

You should use Position += 1; instead

Private void pictureBox2_Click(object sender, EventArgs e) { sF1411BindingSource.Position += 1; }


private void pictureBox1_Click(object sender, EventArgs e) { sF1411BindingSource.Position -= 1; }

Maybe you mean:

private void pictureBox2_Click(object sender, EventArgs e)
{
    sF1411BindingSource.Position +=1;
}  

?

Is your intent that the method pictureBox2_Click will add 1 to sF1411BindingSource.Position ? If that's the case you need to use the += operator . Use the -= operator to decrement by 1.

private void pictureBox2_Click(object sender, EventArgs e)
{
     sF1411BindingSource.Position += 1; 
}

Also, I think you might want to check whether you're at the last or first position before incrementing or decrementing the Position.

As others have correctly pointed out, you should use += and -= operators, which are translated by compiler into addition and assignment expressions:

int a = 10;
int b = 20;
a += b;

is a shortcut to writing

int a = 10;
int b = 20;
a = a + b;

However, seeing that you only want to increase and decrease position by one, you may use shorter C-style ++ and -- operators:

private void pictureBox2_Click(object sender, EventArgs e)
{
    sF1411BindingSource.Position++;
}

private void pictureBox1_Click(object sender, EventArgs e)
{
    sF1411BindingSource.Position--;
}

This may go a bit off-topic, but I also suggest you to take advantage of C# lambda expressions for event handling. Since you don't do much in the event handlers, you can define them as one-liners in the constructor:

public MyForm ()
{
    InitializeComponent();

    nextRecordBox.Click += (sender, e) =>
        bindingSource.Position++;
    prevRecordBox.Click += (sender, e) =>
        bindingSource.Position--;
} 

These features are only available starting from C# 3.0 but you can use C# 2.0 anonymous delegates for the same purpose. They save a lot of space from being occupied with one-line methods.

As for the concrete example, I suggest using MovePrevious and MoveNext methods instead. They are defined in BindingSource class too, and are more verbose.

You might also want to jump to the start when user clicks 'Next' on last record. This is how it would be done:

nextRecordBox.Click += (sender, e) => {
        if (bindingSource.Position == bindingSource.Count - 1) // is last
            bindingSource.MoveFirst ();
        else bindingSource.MoveNext ();
    };

And, please , use good names for UI elements.

Thanks, Dan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM