teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

4

Overideの実装例を追記

2018/01/03 12:43

投稿

sheephuman
sheephuman

スコア135

answer CHANGED
@@ -1,3 +1,25 @@
1
+ OnMouseWheelをOverRideしたときの実装
2
+
3
+  ```ここに言語を入力
4
+ {
5
+ protected override void OnMouseWheel(MouseEventArgs e)
6
+ {
7
+ try
8
+ {
9
+ HandledMouseEventArgs wEventArgs = e as HandledMouseEventArgs;
10
+ wEventArgs.Handled = true;
11
+
12
+ int Wheel = e.Delta / 120;
13
+ CurrentCell = Rows[CurrentCell.RowIndex -Wheel].Cells[0];
14
+ catch
15
+ { }
16
+ }
17
+
18
+ ```
19
+  とてもスムーズに動きました。助かりました。
20
+  随分と手間どってしまいました。 
21
+
22
+
1
23
  追記 スクロールイベントで調整したら意外と上手く行きました。
2
24
  ```ここに言語を入力
3
25
  private void dataGridView1_Scroll(object sender, ScrollEventArgs e)

3

自己解決。

2018/01/03 12:43

投稿

sheephuman
sheephuman

スコア135

answer CHANGED
@@ -1,3 +1,34 @@
1
+ 追記 スクロールイベントで調整したら意外と上手く行きました。
2
+ ```ここに言語を入力
3
+ private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
4
+ {
5
+ int CurrentRow = dataGridView1.CurrentCell.RowIndex;
6
+
7
+ Text = CurrentRow.ToString();
8
+ if (dataGridView1.FirstDisplayedScrollingRowIndex < 10)
9
+ {
10
+
11
+ if (CurrentRow < 10)
12
+ {
13
+ dataGridView1.FirstDisplayedScrollingRowIndex = 0;
14
+ }
15
+ }
16
+ else if(dataGridView1.FirstDisplayedScrollingRowIndex >10)
17
+ {
18
+ if(CurrentRow > 10)
19
+ {
20
+
21
+ dataGridView1.FirstDisplayedScrollingRowIndex = CurrentRow-3;
22
+
23
+ }
24
+
25
+
26
+ }
27
+ }
28
+ ```
29
+
30
+
31
+
1
32
  結局、このような感じで調整しました。もっと良い方法があればお願いします。挙動が微妙です。
2
33
 
3
34
  ```

2

インデント調整

2018/01/03 11:36

投稿

sheephuman
sheephuman

スコア135

answer CHANGED
@@ -2,15 +2,13 @@
2
2
 
3
3
  ```
4
4
  if (3 < CurrentRow && wheel == -1) //ホイール下移動時
5
- {
5
+ {
6
+ dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.CurrentCell.RowIndex-3 ;
7
+    }
6
8
 
9
+   else if(wheel == 1)  //ホイール上移動時
10
+     {
7
- dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.CurrentCell.RowIndex-3 ;
11
+ dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.CurrentCell.RowIndex-6;
8
- }
12
+    }
9
- else if(wheel == 1)  //ホイール上移動時
10
- {
11
13
 
12
- dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.CurrentCell.RowIndex-6;
13
-
14
- }
15
-
16
14
  ```

1

コメントアウト

2018/01/03 11:07

投稿

sheephuman
sheephuman

スコア135

answer CHANGED
@@ -1,12 +1,12 @@
1
- 結局、このような感じで調整しました。もっと良い方法があればお願いします。
1
+ 結局、このような感じで調整しました。もっと良い方法があればお願いします。挙動が微妙です。
2
2
 
3
3
  ```
4
- if (3 < CurrentRow && wheel == -1)
4
+ if (3 < CurrentRow && wheel == -1) //ホイール下移動時
5
5
  {
6
6
 
7
7
  dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.CurrentCell.RowIndex-3 ;
8
8
  }
9
- else if(wheel == 1)
9
+ else if(wheel == 1)  //ホイール上移動時
10
10
  {
11
11
 
12
12
  dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.CurrentCell.RowIndex-6;