如何在Windows窗体应用程序中启用或重新启用文本框,或者为什么我的tbx.Enable = true;代码不起作用? (C#)

时间:2018-01-27 22:09:17

标签: c# winforms controls wpf-controls

我在以下代码中有一个多行文本框(tbxReminderText),即使在尝试将其包含在其他多个位置后,也无法响应我的tbxReminderText.Enabled = true;。在设计器属性列表中是否将Enabled属性设置为true或false没有区别。试图刷新控件,或者我能想到的任何其他尝试,都无法正常工作。以下是适用的代码:

private void EditEntry_Load(object sender, EventArgs e) {
  EntryType.Entries currentTypeEdited = new EntryType.Entries();

  if (mainForm.alarmCLB.SelectedIndex != -1) {
    currentTypeEdited = EntryType.Entries.Alarm;
  } else if (mainForm.reminderCLB.SelectedIndex != -1) {
    currentTypeEdited = EntryType.Entries.Reminder;
  } else {
    currentTypeEdited = EntryType.Entries.Timer;

    //lets change the controls accordingly
    //(no event handler to remove for the existing dtp
    this.Controls.Remove(dtpActiveAt);

    //change existing label
    lblRingAt.Text = "Duration:";

    //set up new control properties

    /* MUCH dynamic insertion of controls removed here for brevity;
     * -+=this is in part of the above if/else branch not used for the
     *    EntryType.Entries.Reminder, which is where my issue lies=+-  */

    this.Controls.Add(nudTmrHrs);
    this.Controls.Add(lblTmrHours);
    this.Controls.Add(nudTmrMin);
    this.Controls.Add(lblTmrMinutes);
    this.Controls.Add(nudTmrSec);
    this.Controls.Add(lblTmrSeconds);
  }

  switch (currentTypeEdited) {
    case EntryType.Entries.Alarm:
      //edit our alarm entry heah
      tbxReminderText.Text = "unavailable";
      tbxReminderText.Enabled = false;
      tbxName.Text =
        HeadsUp.activeAlarms[mainForm.alarmCLB.SelectedIndex].Name;
      dtpActiveAt.Value =
        HeadsUp.activeAlarms[mainForm.alarmCLB.SelectedIndex].ActiveAt;

      break;
    case EntryType.Entries.Timer:
      //edit timer heah
      tbxReminderText.Text = "unavailable";
      tbxReminderText.Enabled = false;
      tbxName.Text =
        HeadsUp.activeTimers[mainForm.timerCLB.SelectedIndex].Name;
      //okay the following isn't a DTP, it's a 3 number field; we're
      //going to have to handle this one differently, hopefully not with
      //a completely different form
      /*dtpActiveAt.Value =
        HeadsUp.activeTimers[mainForm.timerCLB.SelectedIndex].remaining*/

      break;
    case EntryType.Entries.Reminder:
      //reminder tiem
      tbxReminderText.Text =
        HeadsUp.activeReminders[mainForm.reminderCLB.SelectedIndex].Msg;
      tbxReminderText.Enabled = true;
      tbxName.Text =
        HeadsUp.activeReminders[mainForm.reminderCLB.SelectedIndex].Name;
      dtpActiveAt.Value =
        HeadsUp.activeReminders[
          mainForm.reminderCLB.SelectedIndex].ActiveAt;

      break;
  }
}

在内联块注释中注释,问题在于在mainForm.reminderCLB.SelectedIndex != -1(已验证它正在使用此分支)时激活的if / else if / else语句中选择的代码分支,以及在currentTypeEdited = EntryType.Entries.Reminder时激活开关/案例块。

我用谷歌搜索了一下,看上去有点相似,但我找不到类似的东西。我猜测我在某个地方犯了一个愚蠢的错误,而我在代码审查中却没有看到它...任何人都可以帮助指出我犯的错误,指向适用的指针资源(我已经查看了MSDN上的Controls。*等),或者让我知道我应该包含哪些其他信息以便更好地排除故障?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

父容器很可能将属性Enabled设置为false。如果未启用父容器,即使您尝试将其设置为true,也不会启用子项。

修改

尝试在观察窗口中检查tbxReminderText.Parent.Enabled属性值,并在将tbxReminderText.Enabled设置为true之前检查其值。