Announcement

Collapse
No announcement yet.

Textbox über Variable ansprechen

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Textbox über Variable ansprechen

    Hallo,

    ich möchte eine TextBox über eine Variable ansprechen.

    anstatt: String Inhalt = textBox1.Text;

    String TextBoxName = "textBox1";
    String Inhalt = TextBoxName.Text;

    So funktioniert es natürlich nicht, es geht nur ums Prinzip.
    Ich würde mich freuen wenn mir jemand helfen kann.

    Gruß

    Norbert

  • #2
    Hallo,

    es gibt wohl eine Möglichkeit, ein bestimmtes Control über den Namen zu finden. Allerdings habe ich das noch nie probiert und empfinde dies auch als ungünstig, weil man dann oft noch mit Typ-Prüfung und Typ-Konvertierung arbeiten muss.

    Was aber geht, ist z.B. Folgendes; dabei wähle ich aus einer ComboBox eine bestimmte TextBox aus, mit der weiter gearbeitet wird:

    TextBox tb; // als Deklaration
    switch(ComboBox1.SelectedIndex) {
    case 0 { tb = TextBox1; }
    case 1 { tb = TextBox2; } // usw. }
    string Inhalt = tb.Text;

    Hilft Dir dieser Gedanke weiter? Jürge

    Comment


    • #3
      Hallo Norbert,

      diese Aufgabe kann man lösen. Allerdings ist sie etwas umfangreicher. Zuerst musst Du durch die Controls Auflistung deiner Form gehen bis Du die gewünschte Textbox gefunden hast. Dann kannst Du die Property über Reflektion setzen. Bsp:
      <pre>
      //
      foreach (Control c in this.Controls)
      {
      if (c.Name = TextBoxName)
      {
      c.GetType().InvokeMember("Text", BindingFlags.SetProperty, null, c, new object[]{Inhalt});
      break;
      }
      }
      </pre>
      Wenn die Textbox in einem Container-Control liegt, dann musst Du die Controls natürlich rekursiv durchsuchen.
      Der große Vorteil besteht hier darin, dass Du wirklich alles auf der Basis von Strings verarbeiten kannst. Der Nachteil ist natürlich ein relativ großer Aufwand. Es lohnt sich aber auf jeden Fall sich mit Reflektion auseinander zu setzen - ziemlich cool!

      Viele Grüße
      Ola

      Comment


      • #4
        Hallo Norbert,

        einfach den nachfolgenden Code ins Programm übernehmen, z.B. in eine neue Datei namens FormHelper.cs, Namespace bitte anpassen. FindControlByName arbeitet rekursiv, d.h. das Control wird auch dann gefunden, wenn es sich innerhalb eines Unter-Containers befindet. Wenn Du sicherstellen möchtest, dass es sich bei dem Control um eine TextBox oder ein von TextBox abgeleitetes Steuerelement handelt, kannst Du mit FindTextBoxByName arbeiten. Hoffe, dass ich damit helfen konnte ;-)

        using System;
        using System.Windows.Forms;

        namespace MyApplication
        {
        public static class FormHelper
        {
        // ein beliebiges Control mit einem bestimmten Namen rekursiv finden
        public static Control FindControlByName(Control parentControl, string controlName) {
        if (parentControl == null) {
        throw new ArgumentNullException("parentControl");
        }
        if (controlName == null) {
        throw new ArgumentNullException("controlName");
        }
        foreach (Control currentControl in parentControl.Controls) {
        if (currentControl.Name.Equals(controlName, StringComparison.OrdinalIgnoreCase)) {
        return currentControl;
        }
        else {
        Control control = FindControlByName(currentControl, controlName);
        if (control != null) {
        return control;
        }
        }
        }
        return null;
        }

        // Eine TextBox mit einem bestimmten Namen finden. Existiert ein Steuerelement
        // mit dem in textBoxName übergebenen Namen, welches aber keine TextBox oder ein
        // von TextBox abgeleitetes Steuerelement ist, gibt die Methode null zurück.
        public static TextBox FindTextBoxByName(Control parentControl, string textBoxName) {
        Control control = FormHelper.FindControlByName(parentControl, textBoxName);
        if (control != null) {
        Type controlType = control.GetType();
        Type textBoxType = typeof(TextBox);
        if (controlType.Equals(textBoxType) || controlType.IsSubclassOf(textBoxType)) {
        return (TextBox)control;
        }
        }
        return null;
        }
        }
        }

        Beispiel für den Aufruf aus einem Formular:

        TextBox textBox = FormHelper.FindTextBoxByName(this, "txtVorname");
        if (textBox != null) {
        MessageBox.Show(textBox.Text);

        Comment


        • #5
          Nachtrag: Der Code oben kann nur mit .NET 2.0 kompiliert werden, falls Du noch mit .NET 1.1 arbeitest, hier die angepasste Version:

          using System;
          using System.Windows.Forms;

          namespace WindowsApplication1
          {
          public class FormHelper
          {
          // ein beliebiges Control mit einem bestimmten Namen rekursiv finden
          public static Control FindControlByName(Control parentControl, string controlName) {
          if (parentControl == null) {
          throw new ArgumentNullException("parentControl");
          }
          if (controlName == null) {
          throw new ArgumentNullException("controlName");
          }
          foreach (Control currentControl in parentControl.Controls) {
          if (currentControl.Name.Equals(controlName)) {
          return currentControl;
          }
          else {
          Control control = FindControlByName(currentControl, controlName);
          if (control != null) {
          return control;
          }
          }
          }
          return null;
          }

          // Eine TextBox mit einem bestimmten Namen finden. Existiert ein Steuerelement
          // mit dem in textBoxName übergebenen Namen, welches aber keine TextBox oder ein
          // von TextBox abgeleitetes Steuerelement ist, gibt die Methode null zurück.
          public static TextBox FindTextBoxByName(Control parentControl, string textBoxName) {
          Control control = FormHelper.FindControlByName(parentControl, textBoxName);
          if (control != null) {
          Type controlType = control.GetType();
          Type textBoxType = typeof(TextBox);
          if (controlType.Equals(textBoxType) || controlType.IsSubclassOf(textBoxType)) {
          return (TextBox)control;
          }
          }
          return null;
          }
          }

          Comment

          Working...
          X