Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#变量标签?_C#_.net - Fatal编程技术网

C#变量标签?

C#变量标签?,c#,.net,C#,.net,假设我有一个方框,上面写着启用或禁用。 如何使文本根据状态而变化?如果我理解正确,您会问如何让标签或其他一些UI文本自动更新以反映“状态变量”。这只是实现您所描述内容的一种方法: box.Text = (box.Enabled ? "ENABLED" : "DISABLED"); 我将通过拥有一个中央状态对象来实现和。当应用程序初始化时,将事件处理程序附加到这些接口公开的事件,并且当属性(Foo)更改时,其中一个事件处理程序可以更改标签的文本 jeffamaphone说了什么,但我应该补充一点

假设我有一个方框,上面写着启用或禁用。
如何使文本根据状态而变化?

如果我理解正确,您会问如何让标签或其他一些UI文本自动更新以反映“状态变量”。这只是实现您所描述内容的一种方法:

box.Text = (box.Enabled ? "ENABLED" : "DISABLED");
我将通过拥有一个中央状态对象来实现和。当应用程序初始化时,将事件处理程序附加到这些接口公开的事件,并且当属性(Foo)更改时,其中一个事件处理程序可以更改标签的文本


jeffamaphone说了什么,但我应该补充一点,任何时候状态发生变化时,您都必须确保运行相同的代码。确保这种情况发生的最简单方法是将box.Text属性绑定到您感兴趣的状态对象。这样,对对象所做的任何更改都会立即反映在文本中


帮助我开始数据绑定。。。。因为我喜欢常见问题。

在过去的几个月里,我使用了一个比实现一个完整类来管理状态稍微轻一些的解决方案。我通常定义一个枚举来指示UI中可用的状态类型,然后我有一个函数根据所选状态对UI进行更改。这种方法非常成功,而且在需要编写的代码量方面也不算太多

如果我想知道UI中有哪些状态可用,我可以检查枚举的值

public enum SystemState
{
    /// <summary>
    /// System is under the control of a remote logging application.  
    /// </summary>
    RemoteMode,

    /// <summary>
    /// System is not under the control of a remote logging application.
    /// </summary>
    LocalMode
}

public interface IView
{
    void SetState(SystemState state);
}


//method on the UI to modify UI
    private void SetState(SystemState state)
    {
        switch (state)
        {
            case SystemState.LocalMode:

                //for now, just unlock the ui
                break;
            case SystemState.RemoteMode:

                //for now, just lock the ui
                break;
            default:
                throw new Exception("Unknown State requested:" + state);
        }
    }


    //now when you change state, you can take advantage of intellisense and compile time checking:
    public void Connect()
    {
            SetState(SystemState.RemoteMode);
    }
公共枚举系统状态
{
/// 
///系统由远程日志应用程序控制。
/// 
远程模式,
/// 
///系统不受远程日志应用程序的控制。
/// 
本地模式
}
公共接口IView
{
无效设置状态(系统状态);
}
//方法来修改UI
私有无效设置状态(SystemState状态)
{
开关(状态)
{
案例SystemState.LocalMode:
//现在,只需解锁ui
打破
案例SystemState.RemoteMode:
//现在,只需锁定ui即可
打破
违约:
抛出新异常(“请求的未知状态:+状态”);
}
}
//现在,当您更改状态时,可以利用intellisense和编译时检查:
公共void Connect()
{
设置状态(SystemState.RemoteMode);
}

“假设我有一个盒子”?你是说文本框吗?你是说Windows窗体、WPF、Silverlight、ASP.NET吗?“你怎么能指望得到这样一个问题的答案呢?”约翰·桑德斯——你比我强@我知道你主要是想得到答案,没关系。但是为了给你一个有用的答案,你必须向人们提供更多的细节。我不习惯用C#编程,因此我不知道词汇。我道歉。我所说的是您在VisualStudio中使用表单创建者创建的“盒子”。它有一个启用和禁用按钮。你知道!你在VisualStudio上拖动的框!它是文本框、复选框还是什么?你说的是形式本身吗?我想问题是如何让文本在变化时反映状态。也许。不清楚。这是最简单的解释。考虑一下,如果这个家伙希望有一大堆这样的复选框,他可能想用这个方法派生自己的CuffCudieDebug框(用“发送者”代替“CHECKBOX1”)。所以他不必复制粘贴。我严重怀疑这会有助于他明显的理解水平。
public void CheckBox1CheckedChanged(object sender, EventArgs e)
{
    if (checkBox1.Checked) {
        checkBox1.Text = "Enabled";
    }
    else {
        checkBox1.Text = "Disabled";
    }
}
public enum SystemState
{
    /// <summary>
    /// System is under the control of a remote logging application.  
    /// </summary>
    RemoteMode,

    /// <summary>
    /// System is not under the control of a remote logging application.
    /// </summary>
    LocalMode
}

public interface IView
{
    void SetState(SystemState state);
}


//method on the UI to modify UI
    private void SetState(SystemState state)
    {
        switch (state)
        {
            case SystemState.LocalMode:

                //for now, just unlock the ui
                break;
            case SystemState.RemoteMode:

                //for now, just lock the ui
                break;
            default:
                throw new Exception("Unknown State requested:" + state);
        }
    }


    //now when you change state, you can take advantage of intellisense and compile time checking:
    public void Connect()
    {
            SetState(SystemState.RemoteMode);
    }