不可调用的成员

时间:2011-10-28 17:47:51

标签: c# winforms

  

可能重复:
  Update label location in C#?

我正在创建一个自定义窗体,当我尝试更改标签的位置时,我收到错误:错误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;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

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;