Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 07-20-2004, 01:16 PM   #1 (permalink)
Goong
Registered User
 
Join Date: Jul 2004
Posts: 2
Goong is on a distinguished road
working with Array

Hello,

I have a form with a button and a text box. What I am trying to do is when I click the button, the text box shows the first item of my array, then each time I click the button, the textbox updates with the next item of the array until the end and then start again.

My code so far is


Code:
Private Sub btnnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnext.Click

        Dim x As New ArrayList
        Dim i As Integer
        Dim s As Object

        ' Add items to array list.
        x.Add("A")
        x.Add("B")
        x.Add("C")
        x.Add("D")
        x.Add("E")
        x.Add("F")
        x.Add("G")
        x.Add("H")
        x.Add("I")
        x.Add("J")

        
        For i = 0 To x.Count - 1
            txtArray1.Text = (x(i))
        Next
Any help appreciated
Thanks
Goong
Goong is offline   Reply With Quote
Old 07-20-2004, 03:42 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
you probably want to create your array outside of the click event.

also outside the button click event, you probably want to create an integer variable which represents the current value.

after you have those in place, you will increrment the integer variable inside the click event.

you may want to put some extra logic inside the click even which makes sure the integer value does not exceed the total amount of elements in the array.

this is just logic, .. sorry i don't code vb that well.

good luck.
__________________
Mike
sde is offline   Reply With Quote
Old 07-20-2004, 03:52 PM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
ok, i needed a break from work. this is done in c#, but maybe you can follow the logic and re-write it.

this is just a windows application with 1 text box and 1 button.

keep in mind that arrays in c# are 0 based. if i remember correctly, VB arrays start on 1.
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Test
{
	public class Form1 : System.Windows.Forms.Form
	{
		// declare your arraylist and current value here
		private ArrayList x;
		private int currentValue = 0;

		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.TextBox textBox1;
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			InitializeComponent();

			// setup your arraylist
			x = new ArrayList();
			x.Add("A");
			x.Add("B");
			x.Add("C");
			x.Add("D");
			x.Add("E");
			x.Add("F");
			x.Add("G");
			x.Add("H");
			x.Add("I");
			x.Add("J");
			x.Add("K");
			x.Add("L");
			x.Add("M");
			x.Add("N");
			x.Add("O");
			x.Add("P");
			x.Add("Q");
			x.Add("R");
			x.Add("S");
			x.Add("T");
			x.Add("U");
			x.Add("V");
			x.Add("W");
			x.Add("X");
			x.Add("Y");
			x.Add("Z");

			// print the current value to the text box
			this.textBox1.Text = x[currentValue].ToString();
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(32, 32);
			this.button1.Name = "button1";
			this.button1.TabIndex = 0;
			this.button1.Text = "button1";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(120, 32);
			this.textBox1.Name = "textBox1";
			this.textBox1.TabIndex = 1;
			this.textBox1.Text = "";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 266);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.button1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			// increment the current value by 1
			currentValue++;

			// if the current value is higher than the amount of
			// items in your array, set it to 0
			if(currentValue >= x.Count)
			{
				currentValue = 0;
			}

			// print the current value to the text box
			this.textBox1.Text = x[currentValue].ToString();
		}
	}
}
hope that helps.
__________________
Mike
sde is offline   Reply With Quote
Old 07-22-2004, 12:14 PM   #4 (permalink)
Goong
Registered User
 
Join Date: Jul 2004
Posts: 2
Goong is on a distinguished road
Thanks sde
Code works great. My understanding of handling array data is limited
Goong is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
breaking down an array from a form metazai PHP 12 07-09-2004 06:18 AM
array max num Apodysophilia Java 9 04-10-2003 06:36 AM
Return an array with register_globals = off bdl PHP 7 03-10-2003 05:37 PM
creating an array of an object on the freestore in c++ ?? sde Standard C, C++ 7 08-04-2002 01:39 PM
adding to an array in php sde PHP 1 06-12-2002 11:04 AM


All times are GMT -8. The time now is 07:32 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting