Visual Studio 2015 - PictureBoxen in gleichbleibenden Abständen verschieben

  • C#
  • .NET (FX) 4.5–4.8

Es gibt 26 Antworten in diesem Thema. Der letzte Beitrag () ist von Marmelade.

    @Marmelade Entweder Du schreibst diesen Code in die Form_Load oder so:
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    @Marmelade Dies funktioniert bei mir:

    C#-Quellcode

    1. public Form1()
    2. {
    3. InitializeComponent();
    4. this.KeyPreview = true;
    5. }
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    C-Quellcode

    1. namespace WindowsFormsApplication3
    2. {
    3. public partial class Form1 : Form
    4. {
    5. public Form1()
    6. {
    7. InitializeComponent();
    8. this.KeyPreview = true;
    9. }
    10. int R = 50;
    11. int G = 150;
    12. int B = 0;
    13. private delegate void KeyDownProc(object sender, KeyEventArgs e);
    14. private KeyDownProc kdp;
    15. private void Form_OnKeyr(object sender, KeyEventArgs e)
    16. {
    17. switch (e.KeyCode)
    18. {
    19. case Keys.Left:
    20. R = R - 50;
    21. if (R == -50)
    22. {
    23. R = R + 50;
    24. }
    25. pictureBox1.Location = new Point(R, 100);
    26. break;
    27. case Keys.Right:
    28. R = R + 50;
    29. if (R == 250)
    30. {
    31. R = R - 50;
    32. }
    33. pictureBox1.Location = new Point(R, 100);
    34. break;
    35. }
    36. }
    37. private void Form_OnKeyg(object sender, KeyEventArgs e)
    38. {
    39. switch (e.KeyCode)
    40. {
    41. case Keys.Up:
    42. G = G - 50;
    43. if (G == -50)
    44. {
    45. G = G + 50;
    46. }
    47. pictureBox2.Location = new Point(100, G);
    48. break;
    49. case Keys.Down:
    50. G = G + 50;
    51. if (G == 200)
    52. {
    53. G = G - 50;
    54. }
    55. pictureBox2.Location = new Point(100, G);
    56. break;
    57. }
    58. }
    59. private void Form_OnKeyb(object sender, KeyEventArgs e)
    60. {
    61. switch (e.KeyCode)
    62. {
    63. case Keys.Left:
    64. B = B - 50;
    65. if (B == -50)
    66. {
    67. B = B + 50;
    68. }
    69. pictureBox3.Location = new Point(B, 0);
    70. break;
    71. case Keys.Right:
    72. B = B + 50;
    73. if (B == 250)
    74. {
    75. B = B - 50;
    76. }
    77. pictureBox3.Location = new Point(B, 0);
    78. break;
    79. }
    80. }
    81. private void pictureBox1_Click(object sender, EventArgs e)
    82. {
    83. this.kdp = this.Form_OnKeyr;
    84. }
    85. private void pictureBox2_Click(object sender, EventArgs e)
    86. {
    87. this.kdp = this.Form_OnKeyg;
    88. }
    89. private void pictureBox3_Click(object sender, EventArgs e)
    90. {
    91. this.kdp = this.Form_OnKeyb;
    92. }
    93. private void Form1_KeyDown(object sender, KeyEventArgs e)
    94. {
    95. this.kdp(sender, e);
    96. }
    97. }
    98. }

    Ich weiß einfach nicht was ich falsch mache

    @Marmelade Sieh Dir mal Deine Prozedur InitializeComponent() an:
    Spoiler anzeigen

    C#-Quellcode

    1. private void InitializeComponent()
    2. {
    3. this.pictureBox1 = new System.Windows.Forms.PictureBox();
    4. this.pictureBox2 = new System.Windows.Forms.PictureBox();
    5. this.pictureBox3 = new System.Windows.Forms.PictureBox();
    6. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
    7. ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
    8. ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit();
    9. this.SuspendLayout();
    10. //
    11. // pictureBox1
    12. //
    13. this.pictureBox1.BackColor = System.Drawing.Color.Red;
    14. this.pictureBox1.Location = new System.Drawing.Point(13, 13);
    15. this.pictureBox1.Name = "pictureBox1";
    16. this.pictureBox1.Size = new System.Drawing.Size(36, 33);
    17. this.pictureBox1.TabIndex = 0;
    18. this.pictureBox1.TabStop = false;
    19. this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
    20. //
    21. // pictureBox2
    22. //
    23. this.pictureBox2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0)))));
    24. this.pictureBox2.Location = new System.Drawing.Point(13, 61);
    25. this.pictureBox2.Name = "pictureBox2";
    26. this.pictureBox2.Size = new System.Drawing.Size(36, 33);
    27. this.pictureBox2.TabIndex = 1;
    28. this.pictureBox2.TabStop = false;
    29. this.pictureBox2.Click += new System.EventHandler(this.pictureBox2_Click);
    30. //
    31. // pictureBox3
    32. //
    33. this.pictureBox3.BackColor = System.Drawing.Color.Blue;
    34. this.pictureBox3.Location = new System.Drawing.Point(12, 100);
    35. this.pictureBox3.Name = "pictureBox3";
    36. this.pictureBox3.Size = new System.Drawing.Size(36, 33);
    37. this.pictureBox3.TabIndex = 2;
    38. this.pictureBox3.TabStop = false;
    39. this.pictureBox3.Click += new System.EventHandler(this.pictureBox3_Click);
    40. //
    41. // Form1
    42. //
    43. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    44. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    45. this.ClientSize = new System.Drawing.Size(669, 516);
    46. this.Controls.Add(this.pictureBox3);
    47. this.Controls.Add(this.pictureBox2);
    48. this.Controls.Add(this.pictureBox1);
    49. this.Name = "Form1";
    50. this.Text = "Form1";
    51. this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
    52. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
    53. ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
    54. ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit();
    55. this.ResumeLayout(false);
    56. }
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!