Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[c#] ScrollBar über Keys ansprechen

Empfohlene Antworten

Hallo.

Ich möchte nur ein kleines Rechteck über die Form flitzen lassen. Daher habe ich die x und y Koordinaten per ScrollBar.Value verändern lassen. Nun wollte ich dass man die ScrollBars auch per Key.Down etc. bedienen kann, was meiner Ansicht nach auch eine Bewegung des Rechtecks zur Folge haben sollte.

Nun habe ich folgendes Problem:

Das Rechteck bewegt sich nur nach links und rechts. Key.Down und Key.Right verschieben nach rechts und Up und Left nach links.


private void hScrollBar1_ValueChanged(object sender, EventArgs e)
{
Graphics grfx = this.CreateGraphics();
grfx.Clear(System.Drawing.SystemColors.Control);
SolidBrush brush = new SolidBrush(Color.Black);
int x;
int y;
x = hScrollBar1.Value;
y = vScrollBar1.Value;
grfx.FillRectangle(brush, x, y, 5, 5);
}

private void vScrollBar1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
vScrollBar1.Value += 1;
}
if (e.KeyCode == Keys.Down)
{
vScrollBar1.Value -= 1;
}
}
private void hScrollBar1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
hScrollBar1.Value += 1;
}
if (e.KeyCode == Keys.Left)
{
hScrollBar1.Value -= 1;
}
}
[/PHP]

Warum verschiebt sich die Grafik nur nach rechts und links?

Ok jetzt kam mir die Lösung. Habe vergessen dass die ScrollBar nur reagiert wenn der Fokus drauf ist. Da dieser auf der hScrollBar war und diese std. über Links und Rechts reagiert hab ich nen Knoten im Kopf bekommen.


private void vScrollBar1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{vScrollBar1.Value -= 1;}
if (e.KeyCode == Keys.Down)
{vScrollBar1.Value += 1;}
if (e.KeyCode == Keys.Right)
{hScrollBar1.Focus();}
if (e.KeyCode == Keys.Left)
{hScrollBar1.Focus();}
}
private void hScrollBar1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{hScrollBar1.Value += 1;}
if (e.KeyCode == Keys.Left)
{hScrollBar1.Value -= 1;}
if (e.KeyCode == Keys.Up)
{vScrollBar1.Focus();}
if (e.KeyCode == Keys.Down)
{vScrollBar1.Focus();}
}
[/PHP]

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Konto

Navigation

Suchen

Suchen

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.