how to put my textbox to an array

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 4910 Views - Last Post: 11 December 2011 - 01:11 AM Rate Topic: -----

#1 ramin-saljoghi  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 21
  • Joined: 09-December 11

how to put my textbox to an array

Posted 09 December 2011 - 01:19 PM

hello guy's
i got a text box on my form with an buttom
i want to write a text in my text box and put it in an array

exp;

textbox1.text="alireza";

i want to be my array like this

a[0]="a"
a[1]="l"
a[2]="i"
a[3]="r"
a[4]="e"
a[5]="z"
a[6]="a"

but i know i never know what is in my textbox....i just want to have it on an array

can u plz help me???
im using visual stadio 2010 and i use c#

Is This A Good Question/Topic? 0
  • +

Replies To: how to put my textbox to an array

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: how to put my textbox to an array

Posted 09 December 2011 - 01:23 PM

List is dynamic so maybe you could use it: Create a new list, then loop through the textBox1.Text and add each character to the list and after the loop use List.ToArray() if you require the text to be in an array.

Or if the type of array is character instead of string, you could use textBox1.Text.ToCharArray();
Was This Post Helpful? 0
  • +
  • -

#3 ramin-saljoghi  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 21
  • Joined: 09-December 11

Re: how to put my textbox to an array

Posted 09 December 2011 - 01:33 PM

View Postjanne_panne, on 09 December 2011 - 01:23 PM, said:

List is dynamic so maybe you could use it: Create a new list, then loop through the textBox1.Text and add each character to the list and after the loop use List.ToArray() if you require the text to be in an array.

Or if the type of array is character instead of string, you could use textBox1.Text.ToCharArray();


thx for ur answer m8
im rly new in programing.
look
int x=textbox1.TextLength;
char [x]a;

now what code i must use to put each char of my textbox to my array

i know i write the code for creating array in c....
as i said im new
i dont know how ccreate an array in c# too can u lead me plz..
i know im a big noob
Was This Post Helpful? 0
  • +
  • -

#4 ramin-saljoghi  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 21
  • Joined: 09-December 11

Re: how to put my textbox to an array

Posted 09 December 2011 - 01:42 PM

private void Form1_Load(object sender, EventArgs e)
        {
            int x;
            x = textBox1.TextLength;
            char[] aarray = new char[x];

            aarray = textBox1; 

           ///not working!


        }

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,518
  • Joined: 23-August 08

Re: how to put my textbox to an array

Posted 09 December 2011 - 01:56 PM

When you post code...USE CODE TAGS!!!

:code:

textBox1 is a CONTROL. Listen to the advice you've been given and use textBox1.Text.ToCharArray();

.Text gets the text data in the control, and .ToCharArray() converts that text to an array of chars.
Was This Post Helpful? 0
  • +
  • -

#6 ramin-saljoghi  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 21
  • Joined: 09-December 11

Re: how to put my textbox to an array

Posted 09 December 2011 - 01:56 PM

View PostJackOfAllTrades, on 09 December 2011 - 01:55 PM, said:

When you post code...USE CODE TAGS!!!

:code:

ok sorry about that! im new forgive me!
Was This Post Helpful? 0
  • +
  • -

#7 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: how to put my textbox to an array

Posted 09 December 2011 - 01:57 PM

The correct code (that you wanted to implement) would be
char[] myArray = textBox1.Text.ToCharArray();



But I wouldn't suggest it (Because if you change it it needs to make a char array again

What I suggest is to make a list and make it a global variable (declare it outside of any event handler)
List<char> myList = new List<char>();



And then on the textBox1_TextChanged event clear the list and then add the full contents of the text box

This should be in the textBox1_TextChanged even
myList.Clear();
foreach (char c in textBox1.Text.ToCharArray())
    myList.Add(c);


This post has been edited by RexGrammer: 09 December 2011 - 01:58 PM

Was This Post Helpful? 0
  • +
  • -

#8 ramin-saljoghi  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 21
  • Joined: 09-December 11

Re: how to put my textbox to an array

Posted 09 December 2011 - 02:02 PM

View PostJackOfAllTrades, on 09 December 2011 - 01:56 PM, said:

When you post code...USE CODE TAGS!!!

:code:

textBox1 is a CONTROL. Listen to the advice you've been given and use textBox1.Text.ToCharArray();

.Text gets the text data in the control, and .ToCharArray() converts that text to an array of chars.


i used it but got some error
1-cannot convert type 'string' to 'char'
2-cannot convert type 'int[]' to 'int'
3-cannot convert type 'int[]' to 'int'
Was This Post Helpful? 0
  • +
  • -

#9 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: how to put my textbox to an array

Posted 09 December 2011 - 02:05 PM

you wrote

Quote

aaray = textBox1;



That's IMPOSSIBLE

You're assigning a control to an array ( :crazy: )

You should use
aaray = textBox1.Text.ToCharArray();



But look at my example it should be more apropriate

You need to learn about C# a little better, you shouldn't take bites bigger than you can chew

In this context I want to refer you to the Dream.In.Code Learning C# Series

Please don't accept this as an offense but more as a friendly advice...

This post has been edited by RexGrammer: 09 December 2011 - 02:07 PM

Was This Post Helpful? 1
  • +
  • -

#10 ramin-saljoghi  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 21
  • Joined: 09-December 11

Re: how to put my textbox to an array

Posted 09 December 2011 - 02:07 PM

View Postramin-saljoghi, on 09 December 2011 - 02:02 PM, said:

View PostJackOfAllTrades, on 09 December 2011 - 01:56 PM, said:

When you post code...USE CODE TAGS!!!

:code:

textBox1 is a CONTROL. Listen to the advice you've been given and use textBox1.Text.ToCharArray();

.Text gets the text data in the control, and .ToCharArray() converts that text to an array of chars.


i used it but got some error
1-cannot convert type 'string' to 'char'
2-cannot convert type 'int[]' to 'int'
3-cannot convert type 'int[]' to 'int'


oh sorry the erro list was
1- cannot convert type 'string' to 'char'

View PostRexGrammer, on 09 December 2011 - 02:05 PM, said:

you wrote

Quote

aaray = textBox1;



That's IMPOSSIBLE


You should use
aaray = textBox1.Text.ToCharArray();



But look at my example it should be more apropriate

no i used what u said
but got that converting error

                    int x;
	            x = textBox1.TextLength;
                    char[] aarray = new char[x];
	            aarray = textBox1.Text.ToCharArray ();




still got converting string to char error
Was This Post Helpful? 0
  • +
  • -

#11 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: how to put my textbox to an array

Posted 09 December 2011 - 02:10 PM

That's not what I said... I said this

RexGrammer said:

char[] myArray = textBox1.Text.ToCharArray();



You don't need to use the variable x as a length counter because you can get a 'out-of-the-box' array holding the textBox text converted to a char array using the .ToCharArray() method...

This post has been edited by RexGrammer: 09 December 2011 - 02:12 PM

Was This Post Helpful? 1
  • +
  • -

#12 ramin-saljoghi  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 21
  • Joined: 09-December 11

Re: how to put my textbox to an array

Posted 09 December 2011 - 02:17 PM

View PostRexGrammer, on 09 December 2011 - 02:10 PM, said:

That's not what I said... I said this

RexGrammer said:

char[] myArray = textBox1.Text.ToCharArray();



You don't need to use the variable x as a length counter because you can get a 'out-of-the-box' array holding the textBox text converted to a char array using the .ToCharArray() method...


Worked...
i dont know how to say thank you ..i used ur first code and worked now!

can i ask anothere question?
now i got a array
how i can show the array[2] in a trxtbox?
exp;
            texbox1.text="alireza"
            int x;
            x = textBox1.TextLength;
            char[] aarray = new char[x];
            aarray = textBox1.Text.ToCharArray();


now what to show aarray[2] in anothre txe box like textbox2
(3th block of my aarray i mean)
Was This Post Helpful? 0
  • +
  • -

#13 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: how to put my textbox to an array

Posted 09 December 2011 - 02:22 PM

What I meant you just need to have:
char[] aarray = textBox1.Text.ToCharArray();



And nothing more...

So this:
texbox1.text="alireza"
int x;
x = textBox1.TextLength;
char[] aarray = new char[x];
aarray = textBox1.Text.ToCharArray();



Becomes this:
char[] aarray = textBox1.Text.ToCharArray();



And again use the explanation I gave you in the bottom of the post (The better logic) (The part that is starting with

Quote

What I suggest is to...
)

And for your second question:
textBox2.Text = myArray[2].ToString();



A few notes:
1. The numbering of the array is zero based (it starts with 0 so that myArray[2] is actually the third element of the array
2. The way to thank me is to click the plus sign in the bottom right part of the post
Was This Post Helpful? 1
  • +
  • -

#14 ramin-saljoghi  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 21
  • Joined: 09-December 11

Re: how to put my textbox to an array

Posted 09 December 2011 - 02:31 PM

worked again
how i can thank you?u just helped me to get A in class!


i got anothere question but if u got time for it
its not about textv=box or array

u know i got 2 form's wich call from1 and form2
i got x=3 on frist form..and i want to change it when im in my second form(form2)
also i got listbox on form1 ( visible=false)...i got abutton on form2...u wan when i click on button in form2 the visibility of my listbox in form1 change to true

how to do it?
i did this in visual basic but i dont know how to do this in c#
Was This Post Helpful? 0
  • +
  • -

#15 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: how to put my textbox to an array

Posted 09 December 2011 - 02:34 PM

Could you by any chance post the code where you did in VB just so that we can see that you put effort in it?

You can help me by:

Quote

The way to thank me is to click the plus sign in the bottom right part of the post that helped you

Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3