none
Wie konvertiere ich eine Array in String oder Char? RRS feed

  • Frage

  • Hallo,

    So sieht zur Zeit mein Quellcode aus:

    private const int TextBoxCount = 17;

    private TextBox[] TextBoxArray;

    private void einfügenToolStripMenuItem_Click(object sender, EventArgs e)
     {
          string tempr = Clipboard.GetText(TextDataFormat.Text);  
     
          TextBoxArray = new TextBox[] {textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7,
                                       textBox8, textBox9, textBox10, textBox11, textBox12, textBox13, textBox14, textBox15, textBox16};
                for (int i = 0; i < TextBoxCount; i++)
                    TextBoxArray[i].Text = tempr[i];
      }

    Ich bekomme die folgenden Fehler:

    Error 1 Cannot implicitly convert type 'char' to 'string'

    Ich hab mit ToString() oder ToChar() versucht zu konvertieren aber ohne Erfolg.

    Was genau muss ich den Code umbauen damit er funzt?




    • Bearbeitet TaMBeY Dienstag, 3. Juli 2012 10:45
    Dienstag, 3. Juli 2012 09:23

Antworten

  • Hallo,

    Du solltest die aktuelle Länge des Arrays verwenden, denn TextBoxCount kann falsch sein,
    und in tempr nicht genug Inhalt:

          for (int i = 0; i < TextBoxArray.Length && i < tempr.Length; i++)
              TextBoxArray[i].Text = tempr[i].ToString();

    Array.Length liefert die aktuelle Länge, eine zusätzliche Konstante brauchst Du dafür nicht.
    Mit char.ToString() erzeugst Du aus dem einzelnen Zeichen (Char) einen String.

    Gruß Elmar


    Danke Elmar. War sehr Hilfreich.
    • Als Antwort markiert TaMBeY Sonntag, 8. Juli 2012 12:37
    Dienstag, 3. Juli 2012 11:55

Alle Antworten

  • Entferne in der Schleife das [i] hinter tempr.

    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Dienstag, 3. Juli 2012 09:30
  • Entferne in der Schleife das [i] hinter tempr.

    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Jetzt bekomme ich die Fehlermeldung:

    Error 1 Syntax error, value expected

    Dienstag, 3. Juli 2012 10:09
  • Zeig doch mal, wie es jetzt aussieht.

    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Dienstag, 3. Juli 2012 11:02
  • Zeig doch mal, wie es jetzt aussieht.

    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

     

    private const int TextBoxCount = 17;

    private TextBox[] TextBoxArray;

    private void einfügenToolStripMenuItem_Click(object sender, EventArgs e)
     {
          string tempr = Clipboard.GetText(TextDataFormat.Text);  
     
          TextBoxArray = new TextBox[] {textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7,
                                       textBox8, textBox9, textBox10, textBox11, textBox12, textBox13, textBox14, textBox15, textBox16};
                for (int i = 0; i < TextBoxCount; i++)
                    TextBoxArray[i].Text = tempr[];
      }
    Dienstag, 3. Juli 2012 11:16
  • Ich hatte ja geschrieben : entferne [i] hinter tempr. Nicht nur i !

    TextBoxArray[i].Text = tempr;


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Dienstag, 3. Juli 2012 11:21
  • Ich hatte ja geschrieben : entferne [i] hinter tempr. Nicht nur i !

    TextBoxArray[i].Text = tempr;


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Hmm ich glaube das du mich Falsch verstanden hast.

    ich habe eine Variable tempr die 17 Zeichen enthält.

    z.B. tempr = C12345A  67C891 2 

    und habe 17 Textfelder. Ich möchte zu Textbox1 den erste Wert von tempr zuweisen nicht den ganzen tempr.

    z.B.

    Textfeld1=C

    Textfeld2=1

    usw...

    Ich habe ja eine Lösung schon der so aussiehtstring

    tempr=Clipboard.GetText(TextDataFormat.Text);
    textbox1.Text += tempr[0]
    textbox2.Text + = tempr[1]

    usw...


    • Bearbeitet TaMBeY Dienstag, 3. Juli 2012 11:34
    Dienstag, 3. Juli 2012 11:33
  • Hallo,

    Du solltest die aktuelle Länge des Arrays verwenden, denn TextBoxCount kann falsch sein,
    und in tempr nicht genug Inhalt:

          for (int i = 0; i < TextBoxArray.Length && i < tempr.Length; i++)
              TextBoxArray[i].Text = tempr[i].ToString();

    Array.Length liefert die aktuelle Länge, eine zusätzliche Konstante brauchst Du dafür nicht.
    Mit char.ToString() erzeugst Du aus dem einzelnen Zeichen (Char) einen String.

    Gruß Elmar

    Dienstag, 3. Juli 2012 11:47
    Beantworter
  • Ahhh,

    dann kannst Du einfach die SubString() Methode der String Klasse benutzen:

    //
    TextBoxArray[i].Text = tempr.Substring(i, 1);
    //


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Dienstag, 3. Juli 2012 11:48
  • Hallo,

    Du solltest die aktuelle Länge des Arrays verwenden, denn TextBoxCount kann falsch sein,
    und in tempr nicht genug Inhalt:

          for (int i = 0; i < TextBoxArray.Length && i < tempr.Length; i++)
              TextBoxArray[i].Text = tempr[i].ToString();

    Array.Length liefert die aktuelle Länge, eine zusätzliche Konstante brauchst Du dafür nicht.
    Mit char.ToString() erzeugst Du aus dem einzelnen Zeichen (Char) einen String.

    Gruß Elmar


    Danke Elmar. War sehr Hilfreich.
    • Als Antwort markiert TaMBeY Sonntag, 8. Juli 2012 12:37
    Dienstag, 3. Juli 2012 11:55
  • Ahhh,

    dann kannst Du einfach die SubString() Methode der String Klasse benutzen:

    //
    TextBoxArray[i].Text = tempr.Substring(i, 1);
    //


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/




    Danke für deine Hilfe Heslacher.
    Dienstag, 3. Juli 2012 11:56