C# last next and previous button

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

30 Replies - 21430 Views - Last Post: 11 December 2010 - 02:51 PM Rate Topic: -----

#1 Guest_Tyrone*


Reputation:

C# last next and previous button

Posted 06 December 2010 - 02:33 AM

I have 9 arrays and i want to make next,previous and last button in the program so it can show the data that is in the array can someone tell me how i need to do these buttons.
Is This A Good Question/Topic? 0

Replies To: C# last next and previous button

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: C# last next and previous button

Posted 06 December 2010 - 02:49 AM

Hello there,

We can't just give you the code when you have shown no effort yourself :). However, I will say this. For the next and previous buttons, you could store a variable that holds the current index you are at in the array. When the next button is pressed, increment the index by one. When the previous button is pressed, decrement it by one. You can find the last element in an array using this syntax:


nameOfArray[nameOfArray.Length - 1];



This post has been edited by CodingSup3rnatur@l-360: 06 December 2010 - 02:50 AM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_Tyrone*


Reputation:

Re: C# last next and previous button

Posted 06 December 2010 - 03:06 AM

for the last button i tried this code but it gave me an error IndexOutOfRagneExeption Was Unhandled
the code i tried is this

private void btnLast_Click(object sender, EventArgs e)
{
    int i = 0;

    foreach (string myCurrentName in myNames)
    {
        if (myCurrentName == null)
        {
           i = i - 1;
           myNames[i] = txtName.Text;
           mySurnames[i] = txtSurname.Text;
           myAddresses[i] = txtAddress.Text;
           myTels[i] = txtTel.Text;
           myTowns[i] = cbxTown.Text;
           myIDs[i] = txtId.Text;
           myBankAccs[i] = txtAcc.Text;
           myNIs[i] = txtNi.Text;
           myComments[i] = txtComments.Text;
           break;
        }



MOD EDIT: Moved code into post. Please do not attach code to your post; put IN CODE TAGS as shown below within the post itself.

:code:

Attached File(s)

  • Attached File  code.txt (757bytes)
    Number of downloads: 505

This post has been edited by JackOfAllTrades: 06 December 2010 - 08:25 AM

Was This Post Helpful? 0

#4 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: C# last next and previous button

Posted 06 December 2010 - 03:17 AM

That's because, with this line i = i - 1; you set 'i' equal to -1. There are no elements in an array at index -1.

Anyway, that's the event handler method to find the last element in the array. Why are you filling the array in that method? Surely that method only needs to find the last element in the array, like I did with the code in my first post?

This post has been edited by CodingSup3rnatur@l-360: 06 December 2010 - 03:36 AM

Was This Post Helpful? 0
  • +
  • -

#5 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: C# last next and previous button

Posted 06 December 2010 - 03:18 AM

You define int i = 0 and in the first loop of foreach you substract -1 of 0 = -1.
myNames[i] is myNames[-1] and that is a IndexOutOfBounds.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_Tyrone*


Reputation:

Re: C# last next and previous button

Posted 06 December 2010 - 03:27 AM

does int i need to be assigned a value?
sorry but I'm only a beginner in c#
Was This Post Helpful? 0

#7 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: C# last next and previous button

Posted 06 December 2010 - 04:21 AM

It depends on what you are trying to do... Could you talk me through exactly what you want this program to do? From the time when the user clicks on the .exe icon, to when the user closes the application, what is meant to happen? It's difficult to advise you without a little more information. However, I will say that I am not sure that having 9 different arrays is the best way to go...

Thanks.

This post has been edited by CodingSup3rnatur@l-360: 06 December 2010 - 04:22 AM

Was This Post Helpful? 0
  • +
  • -

#8 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: C# last next and previous button

Posted 06 December 2010 - 04:30 AM

Use the for Statement to loop thourgh an array.

for (int i = 0; i < myArray.Lentgh; i++) {
   ... = myArray[i];
}


This post has been edited by mavarazo: 06 December 2010 - 04:31 AM

Was This Post Helpful? 0
  • +
  • -

#9 Guest_Tyrone*


Reputation:

Re: C# last next and previous button

Posted 06 December 2010 - 04:31 AM

this program is a payroll system of a company and it needs to calculate the salary of the workers and be able to keep information about the employee and navigate threw the records. I hope this helps.Thanks
Was This Post Helpful? 0

#10 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: C# last next and previous button

Posted 06 December 2010 - 04:49 AM

Okay...It sounds to me like a database is what you need... However, Why don't you create an Employee class to hold each employee's details. You can then store each Employee object in an array. Something like this:

Define an Employee class:

public class Employee{

public string FirstName{
get;
private set;
}

public string LastName{
get;
private set;
}

public string Address{
get;
private set;
}

public string PhoneNumber{
get;
private set;
}

public string Town{
get;
private set;
}

public string ID{
get;
private set;
}

public string AccountNumber{
get;
private set;
}

public string NISNumber{
get;
private set;
}

public string Comments{
get;
private set;
}


public Employee(string firstName, string lastName, string address, string phoneNumber, string town, string ID, string accountNumber, string nisNumber, string comments){

this.FirstName = firstName;
this.LastName = lastName;
this.Address = address;
this.PhoneNumber = phoneNumber;
this.Town = town;
this.ID = ID;
this.AccountNumber = accountNumber;
this.NISNumber = nisNumber;
this.Comments = comments;

}
}

You now have an Employee object that can be used to hold all individual employee detail.

I would use a generic list instead of an array to hold the details, as generic lists dynamically resize. Therefore, in you main class, you could do something like this:


List<Employee> employees = new List<Employee>();




You can then fill that list with al the Employees in the company. Although, if it is a big company, a database is recommended. However, if there are going to be only a few employees that are part of the program, a list will do fine (unless the details need to be stored after the app has closed!).

Then, you need to fill the list with employees. Here is an example of how to add one employee to the list:


employee.Add(new Employee("Joe","Bloggs","Somewhere","01567 283843","SomeTown","345","02938848828566","9284777593",""));




And there we are, one employee called Joe Bloggs added to the list, with all his details nicely encapsulated in the Employee object. I would also create an abstract Person class and have Employee inherit from that, but don't worry about that for the time being.

You now have a single list rather than 9 different arrays. You can now find the next, previous, last etc items in that single list!

You have to understand though that using lists and arrays alone will not store the details. Once the application is closed, all details will be lost. To actually store the details indefinately after they have been entered, you need to use serialization, or preferably a database back end (or even a flat file, at a push).

I know there may seem a lot there. Any questions then just ask :).

This post has been edited by CodingSup3rnatur@l-360: 06 December 2010 - 11:35 AM

Was This Post Helpful? 0
  • +
  • -

#11 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: C# last next and previous button

Posted 06 December 2010 - 05:28 AM

For such a simple assignment, I think his course instructor wants them to use Arrays and not some of the cooler collection types in .NET. :P We've all been there!
Was This Post Helpful? 0
  • +
  • -

#12 Guest_Tyrone*


Reputation:

Re: C# last next and previous button

Posted 06 December 2010 - 09:35 AM

I need to use arrays to do this. I tried with the for loop but its still not showing the correct record
        private void btnLast_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < myNames.Length; i++)
            {
                
                txtName.Text = myNames[i];
                txtSurname.Text = mySurnames[i];
                txtAddress.Text = myAddresses[i];
                txtTel.Text = myTels[i];
                cbxTown.Text = myTowns[i];
                txtId.Text = myIDs[i];
                txtAcc.Text = myBankAccs[i];
                txtNi.Text = myNIs[i];
                txtComments.Text = myComments[i];
                lblMessage.Text = "";
                break;
            }

also i have a date time picker and i need to get the value of the day but i cant convert the value to int. can someone tell me how i need to get the value
Was This Post Helpful? 0

#13 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: C# last next and previous button

Posted 06 December 2010 - 10:42 AM

Hello again,

I'm sorry but you have completely lost me...

I suggested list because it looked like you were capturing input from the user at runtime from the code in the .txt file you posted. You can use arrays instead if that isn't the case, no problem :).

I honestly have know idea what you are trying to do in that btn event handler.

What record do you think that code will show?

Why are you using a loop to display one record?

Why are you breaking out of the loop after one loop using the break; keyword? Don't you think that kind of defeats the idea of a loop?

Why are you not using the button called btnLast to return the last record (i.e. like I showed you in my first post)?

Why are you using 9 different arrays? Surely you want an array of Employees each of which has a name, accountNumber etc, rather than having their details sprawled everywhere in 9 different, unrelated arrays? Unless the 9 different arrays is specific to the assignment...

Try creating the Employee class if it isn't because I really don't know where to go from here... Sorry. Once you have an Employee class, fill one, single array with Employee objects. Then you can access the different elements using the ideas I posted in my first post. I explained the basic idea of navigating through the array n my first post too! Once you have one single array with all the details in, it will be ridiculously easy to do the next, previous etc things you want.
Also, what exactly will you be using the DateTime picker for? That sort of suggests to me that you want to give the user the ability to change and/or add records?

This post has been edited by CodingSup3rnatur@l-360: 06 December 2010 - 11:13 AM

Was This Post Helpful? 0
  • +
  • -

#14 Guest_Tyrone*


Reputation:

Re: C# last next and previous button

Posted 06 December 2010 - 11:14 AM

I'm using a loop because I thought that i can find the first space that is null in the array then decrement it by 1 to show the last record added. I don't get how to use the code in your first post but it gave me an error Only assignment, call, increment, decrement, and new object expressions can be used as a statement
myNames[myNames.Length - 1];

I made a mistake with the break; keyword
I'm using 9 different arrays because my instructor said i need to use arrays to do it.

The date time picker will be so the user can select the dates the employee has worked then find the total week days, Saturdays and Sundays and multiply the payment per hour by the total number of hours worked in those days.
Was This Post Helpful? 0

#15 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: C# last next and previous button

Posted 06 December 2010 - 11:21 AM

Right okay,

You need to actually do something with the value returned using the line showed in my first post. That code will return the last element in an array. However, you need to do something with that value. Like store it in a variable, display it in a text box etc. Your getting the errror because you aren't doing anything with the element you are accessing. The value of the last element is just floating aimlessly.

Just because your instructor said use arrays, I don't think it means you should use as many arrays as is physically possible. Please, please, please use only one array. If only for my sanity.

Also, please create the Employee class I showed a few posts ago. Then post the class you create. I will then talk you through filling an array with Employees and accessing next, previous; last elements.

This post has been edited by CodingSup3rnatur@l-360: 06 December 2010 - 11:30 AM

Was This Post Helpful? 0
  • +
  • -

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