Possible Duplicate:
Update label location in C#?




我正在创建一个自定义Windows窗体,并且尝试更改标签的位置时收到错误消息:错误1不可使用的成员'System.Windows.Forms.Control.Location'不能像方法一样使用。 C:\ Users \ Ran \ Documents \ Visual Studio 2010 \ Projects \ SyncCustomForm \ SyncCustomForm \ SyncControl1.cs 50 24 SyncCustomForm

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SyncCustomForm
{
    public partial class SyncControl : UserControl
    {

        public SyncControl()
        {
            InitializeComponent();
        }

        public ProgressBar prbSyncProgress
        {
            get { return prbProgress; }
        }
        public Label lblException
        {
            get { return lblMessage; }
        }
        public Label lblStatus
        {
            get { return lblS; }
        }
        public Button btnPause
        {
            get { return btnP; }
        }
        public Button btnStop
        {
            get { return btnS; }
        }
        public GroupBox grbxSync
        {
            get { return gbxSync; }
        }

        private void SyncControl_Load(object sender, EventArgs e)
        {

            lblMessage.Location.X = 50;
        }
    }
}

最佳答案

Location属性是一个结构,而X是该结构的属性,在这种情况下,您不能独立设置X的值。

您需要这样做:

lblMessage.Location = new Point(50, 50); // both X and Y will be set this way


或者,如果您只想设置X值,请设置Left属性:

lblMessage.Left = 50;


如果您直接引用该结构,则只能设置该结构的属性:

var loc = lblMessage.Location;
loc.X = 50;
lblMessage.Location = loc;

关于c# - 不可发言的成员,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7932896/

10-13 04:39