2

This seemingly simple for whatever reason is not working. Specifically the Foreach Loop is giving me this error "Error 1 Cannot convert type 'char' to 'string'". I have done some research although it does not want to reveal its self. Hopefully you guys will know, thanks so much for the help.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace A_HtmlEditor
{
    public partial class Form1 : Form
    {
        AutoCompleteStringCollection data = new AutoCompleteStringCollection();

        public Form1()
        {
            InitializeComponent();
        }               

        // The error occurs in the foreach loop below    
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            webBrowser1.DocumentText = textBox1.Text;

            foreach(string s in textBox1.Text)
            {
                data.Add(s);
            } 
        }    
    }
}

As an aside I was wondering while I am here, do any of you happen to know whether or not it is possible to find out if there was a button click, such as the shutdown button? Or if that is not possible is there a way to know when a computer is about to be shutdown.

Once again I appreciate it all, Thanks.

reggaeguitar
  • 1,795
  • 1
  • 27
  • 48
Alex Johnson
  • 33
  • 1
  • 1
  • 3

6 Answers6

9

textBox1.Text is a string (not a collection of strings). So when you do this:

foreach (string s in textBox1.Text)
{
    data.Add(s);
}

it is trying to treat the string as a collection. This actually works, because a string is really an array of char. The problem is that you're trying to convert each char to a string when you declare string s.

If you really want to add each character to data, then you can convert each char to a string:

// This takes each character from textBox1.Text,
// converts it to a string, and adds it to data
foreach (char chr in textBox1.Text)
{
    data.Add(chr.ToString());
}

Or, if your textBox1 is a multi-line textbox and you're trying to add each line to data, you can split the text on the NewLine character to get a list of strings, and add them like so:

// This takes each line from a multi-line text box and adds it to data
foreach (string line in textBox1.Text.Split(new[] { '\n' }))
{
    data.Add(line);
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
2

What it looks like you are trying to do is iterate through the lines of a textbox. The Text property of a TextBox object is of a data type String.

If I am correct, in order to accomplish this, you could do something like the following:

var lines = textbox.Text.Split((new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var s in lines)
{
   data.Add(s);
}

Because the Text property is a single string object. You would need to split the string into a collection of smaller strings on some sort of delimiter such as a new line character. (eg. '\n')

James Shaw
  • 839
  • 1
  • 6
  • 16
1

A string contains characters not strings, change to the following

foreach(char s in textBox1.Text) //should be renamed to c as char
meda
  • 45,103
  • 14
  • 92
  • 122
0

First of all if you want to iterate characters in string you can use something

        foreach (var s in textBox1.Text)
        {
            data.Add(s.ToString());
        }

if you want to get the array of characters from string you can use

textBox1.Text.ToCharArray()

You can find the answers here for detecting power off How to detect Windows shutdown or logoff

Community
  • 1
  • 1
zash707
  • 245
  • 2
  • 11
0

Another solution for this would be:

string[] textBox1.text = webBrowser1.DocumentText
foreach (string s in textBox1.Text)
{
    data.Add(s);
}

By doing this they will be seen as separate str instead of char

-1

Or use this one if getting substring at particular position is concerned

for (int i = 0; i < s.Length; i++)
    {
        Console.WriteLine(s[i]); // or other works 
    }
Jeff Bootsholz
  • 2,971
  • 15
  • 70
  • 141