【问题标题】:Sliding the Panel滑动面板
【发布时间】:2015-10-28 20:33:12
【问题描述】:

我正在使用 WinForms。在我的表单中,我有一个按钮和一个面板。当我单击该按钮时,我想将面板向右滑动。我的代码有问题。目前我在下面收到红色错误线 = panel2.Location.X + 1;

Error Message: Cannot implicitly convert type int to System.Drawing.Point

我尝试使用与扩展面板类似的方法来移动面板。我在我的代码中提供了这一点。如何移动面板?

private void btn_Right_Click(object sender, EventArgs e)
{   
    // Make Panel Grow
    //while (panel1.Width < 690)
    //{
    //    panel1.Width = panel1.Width + 1;
    //}

    while (panel2.Location.X < 690)
    {
        panel2.Location = panel2.Location.X + 1;
    }
}

【问题讨论】:

  • 那么红色的错误是什么意思..?我们不是这里的读者..
  • 您正在尝试将 int 分配给 Point
  • 抱歉,错误:无法将 int 类型隐式转换为 System.Drawing.Point @MethodMan
  • 为什么不做如下panel2.Location = new Point(x,y);

标签: c# .net winforms panel move


【解决方案1】:

您收到错误消息,因为您尝试使用整数设置位置。您将需要一个新的点实例:

panel2.Location = new Point(panel2.Location.X, panel2.Location.Y + 1);

【讨论】:

  • 谢谢你,成功了。我要查找新的点实例。仍在学习这些术语 :) @c.wider
  • 有没有办法像这样编写代码 panel2.Location = new Point(panel2.Location.X +0.5, panel2.Location.Y); .....(0.5 )所以滑动会更慢。而不是 + 1
  • 不,控件的位置只能用整数表示,但我认为你的问题不是滑动太快而是刷新问题。尝试在每组位置后刷新控件,如下所示:panel2.Refresh()
【解决方案2】:

尝试使用 .Left 而不是 .Location.X

这适用于 VB...

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
    If sender.text = ">" Then
        Do Until Panel1.Left > Me.Width - 50
            Panel1.Left += 1
        Loop
        sender.text = "<"
    Else
        Panel1.Left = 511
        sender.text = ">"
    End If
End Sub

我很惊讶它是平滑的 - 但是面板是空的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 2011-11-08
    • 1970-01-01
    相关资源
    最近更新 更多