Well i'll try to be clear as possible, i'm making like an account organizer (helper/tool) w.e you wanna call it, and for example i have 10 accounts.

I want to delete account #5 (They are array), what i need it to do is move account #6, 7, 8, 9 & 10 down and number 5 would be the last account (Last account will be blank).

I have this Listbox to change in between the accounts, so i can change from 1 to 10 fast, account 11 is just blank just if i want to add another account.

My codes:

Accounts

Option Explicit On

Public Class Accounts
    Private mVersion As String
    Private mUsername As String
    Private mPassword As String
    Private mCharacter As String
    Private mServer As String
    Private mLogin As String
    Private mReturnLogin As Boolean
    Private mLoginStart As Boolean
    Private mRelogDisco As Boolean
    Private mDisMap As Boolean
    Private mSpecialAcc As Boolean

    Public Sub New(ByVal Version As String, ByVal Login As String, ByVal Username As String, ByVal Password As String, ByVal Character As String, ByVal Server As String, ByVal ReturnLogin As Boolean, ByVal LoginStart As Boolean, ByVal RelogDisco As Boolean, ByVal DisMap As Boolean, ByVal SecialAcc As Boolean)
        mVersion = Version
        mUsername = Username
        mPassword = Password
        mCharacter = Character
        mServer = Server
        mLogin = Login
        mReturnLogin = ReturnLogin
        mLoginStart = LoginStart
        mRelogDisco = RelogDisco
        mDisMap = DisMap
        mSpecialAcc = SpecialAcc
    End Sub

    Property Version() As String
        Get
            Return mVersion
        End Get
        Set(ByVal value As String)
            mVersion = value
        End Set
    End Property

    Property Username() As String
        Get
            Return mUsername
        End Get
        Set(ByVal value As String)
            mUsername = value
        End Set
    End Property

    Property Password() As String
        Get
            Return mPassword
        End Get
        Set(ByVal value As String)
            mPassword = value
        End Set
    End Property

    Property Character() As String
        Get
            Return mCharacter
        End Get
        Set(ByVal value As String)
            mCharacter = value
        End Set
    End Property

    Property Server() As String
        Get
            Return mServer
        End Get
        Set(ByVal value As String)
            mServer = value
        End Set
    End Property

    Property Login() As String
        Get
            Return mLogin
        End Get
        Set(ByVal value As String)
            mLogin = value
        End Set
    End Property

    Property ReturnLogin() As Boolean
        Get
            Return mReturnLogin
        End Get
        Set(ByVal value As Boolean)
            mReturnLogin = value
        End Set
    End Property

    Property LoginStart() As Boolean
        Get
            Return mLoginStart
        End Get
        Set(ByVal value As Boolean)
            mLoginStart = value
        End Set
    End Property

    Property RelogDisco() As Boolean
        Get
            Return mRelogDisco
        End Get
        Set(ByVal value As Boolean)
            mRelogDisco = value
        End Set
    End Property

    Property DisMap() As Boolean
        Get
            Return mDisMap
        End Get
        Set(ByVal value As Boolean)
            mDisMap = value
        End Set
    End Property

    Property SpecialAcc() As Boolean
        Get
            Return mSpecialAcc
        End Get
        Set(ByVal value As Boolean)
            mSpecialAcc = value
        End Set
    End Property
End Class

New Account:

    Dim CurrentAcc(-1) As Accounts

    Private Sub NewAcc(ByVal wIndex As Integer)
        ReDim Preserve CurrentAcc(CurrentAcc.GetUpperBound(0) + 1)

        If Me.AccNumber.Text = "" Then
            Me.CurrentAcc(0) = New Accounts("Silkroad International", "Random", "", "", "", "", False, False, False, False, False)

            Me.AccNumber.Items.Add("1")
            Me.AccNumber.SelectedIndex = (0)
        Else
            Me.CurrentAcc(Me.AccNumber.Text + wIndex) = New Accounts("Silkroad International", "Random", "", "", "", "", False, False, False, False, False)
        End If
    End Sub

Add/Edit Account:

Private Sub AddEdit_Btn_Click(sender As System.Object, e As System.EventArgs) Handles AddEdit_Btn.Click
        If Not Me.IDTextbox.Text = String.Empty And Not Me.PWTextbox.Text = String.Empty And Not Me.CharTextbox.Text = String.Empty Then

            CurrentAcc(Me.AccNumber.SelectedIndex).Version = Me.SVersion.Text
            CurrentAcc(Me.AccNumber.SelectedIndex).Username = Me.IDTextbox.Text
            CurrentAcc(Me.AccNumber.SelectedIndex).Password = Me.PWTextbox.Text
            CurrentAcc(Me.AccNumber.SelectedIndex).Character = Me.CharTextbox.Text
            CurrentAcc(Me.AccNumber.SelectedIndex).Server = Me.SName.Text
            CurrentAcc(Me.AccNumber.SelectedIndex).ReturnLogin = Me.LReturn.Checked
            CurrentAcc(Me.AccNumber.SelectedIndex).LoginStart = Me.LStart.Checked
            CurrentAcc(Me.AccNumber.SelectedIndex).ReturnLogin = Me.LReturn.Checked
            CurrentAcc(Me.AccNumber.SelectedIndex).RelogDisco = Me.RDisconnect.Checked
            CurrentAcc(Me.AccNumber.SelectedIndex).SpecialAcc = Me.SAccount.Checked

            If Me.AccNumber.Items.Count <= Me.AccNumber.Text Then
                NewAcc(0)

                Me.AccNumber.Items.Add(Me.AccNumber.Text + 1)
                Me.AccNumber.SelectedItem = (Me.AccNumber.Text + 1)

                DefaultProf()
            Else
                Me.AccNumber.SelectedItem = (Me.AccNumber.Text + 1)
            End If

        Else
            MessageBox.Show("Please don't leave any box in blank", "phBot Account Manager", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
    End Sub

I need help on the part of moving down the arrays.
I deleted account #5 so i want account#5 to be the last accounts and all accounts after 5 to move 1 place down.

Any help ? Thx.

Recommended Answers

All 7 Replies

You have alot of over head here I see.

Is using a database out of the question?

If it is, then what about using an ArrayList?

When using an array list, you can simply store each account in the ArrayList and remove one by:

Dim ar As New ArrayList
Dim index As Integer = 3
ar.RemoveAt(index) 'This will remove the fourth entry.'

This will remove the account at the fourth entry in the array.

You will need to cast to the account type when using arraylists too.

Example:

Dim refAccount as Accounts


refAccount = TryCast(ar(0),Accounts)

If refAccount IsNot Nothing Then
    'Cast was successfull. Continue working'
Else
    'Cast was unsuccessfull. Halt application or implement fault tolerance.'
End If

I've used removeat, but when I do it deletes that entry like you said, that entry stays blank, and what I want is to move that entry to the last one and then put it in blank

You could try something like:

Dim curItem as Integer = 3
ar.RemoveAt(3)
For i = CurItem to ar.Count - 2
    ar(i) = ar(i + 1)
Next

ar.RemoveAt(ar.Count - 1)

This will "push" everything up one, then delete the last one.

commented: Really important, i needed this, thank you +0

Dim ar As New ArrayList

Im not using it from a database, and for me to use removeAt i got this function.

Module RemoveUser
    <System.Runtime.CompilerServices.Extension()> _
    Function RemoveAt(Of T)(ByVal arr As T(), ByVal index As Integer) As T()
        Dim uBound = arr.GetUpperBound(0)
        Dim lBound = arr.GetLowerBound(0)
        Dim arrLen = uBound - lBound

        If index < lBound OrElse index > uBound Then
            Throw New ArgumentOutOfRangeException( _
            String.Format("Index must be from {0} to {1}.", lBound, uBound))

        Else
            'create an array 1 element less than the input array
            Dim outArr(arrLen - 1) As T
            'copy the first part of the input array
            Array.Copy(arr, 0, outArr, 0, index)
            'then copy the second part of the input array
            Array.Copy(arr, index + 1, outArr, index, uBound - index)

            Return outArr
        End If
    End Function
End Module

i used

        Dim curItem As Integer = 3
        Me.CurrentAcc.RemoveAt(Me.AccNumber.Text)
        For i = curItem To Me.CurrentAcc.Count - 2
            Me.CurrentAcc(i) = Me.CurrentAcc(i + 1)
        Next
        Me.CurrentAcc.RemoveAt(Me.CurrentAcc.Count - 1)
    End Sub

but didn't work..

Edit:

i used:

        Dim curItem As Integer = Me.AccNumber.Text

        Me.CurrentAcc.RemoveAt(Me.AccNumber.Text)

        For i = curItem - 1 To Me.CurrentAcc.Count - 2
            Me.CurrentAcc(i) = Me.CurrentAcc(i + 1)
        Next

        Me.CurrentAcc.RemoveAt(Me.CurrentAcc.Count - 1)

And it deleted it but, it justs makes it blank, at the end of my accounts theres already 1 blank accoutn that's always blank to keep adding more accounts, so when i delete account number 3 from the 5 i have i get account 5 and account 6 blank, any possible way to complete delete the last index.

Well, its kind of solved because i had #5 and #6 blank, but i forgot to update the DomainUpDown (Where i can change from accounts), but i used in a button:

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        MsgBox("How many accs: " & CurrentAcc.Count())
        MsgBox("Selected Acc: " & Me.AccNumber.SelectedIndex + 1)
    End Sub

When i load the app and i click it, it displays that i have 1 account and i have 1 item in the domainupdown, ok good, i add 3 accounts and +1 account which is the last account that is in blank to keep adding more accounts, i delete account 2 it says that i have 4 accounts, and 3 items in the domainupdown, i could leave it like this since is kind of working but the account 4 is blank and if i keep adding accounts and deleting it will keep adding more arrays not being used.

im using:

        Dim curItem As Integer = Me.AccNumber.SelectedIndex

        CurrentAcc.RemoveAt(Me.AccNumber.Text)

        For i = curItem To CurrentAcc.Count - 2
            CurrentAcc(i) = CurrentAcc(i + 1)
        Next

        CurrentAcc.RemoveAt(CurrentAcc.Count - 1)
        Me.AccNumber.Items.RemoveAt(Me.AccNumber.Items.Count - 1)

RemoveAt:

    <System.Runtime.CompilerServices.Extension()> _
    Function RemoveAt(Of T)(ByVal arr As T(), ByVal index As Integer) As T()
        Dim uBound = arr.GetUpperBound(0)
        Dim lBound = arr.GetLowerBound(0)
        Dim arrLen = uBound - lBound

        If index < lBound OrElse index > uBound Then
            Throw New ArgumentOutOfRangeException( _
            String.Format("Index must be from {0} to {1}.", lBound, uBound))

        Else
            'create an array 1 element less than the input array
            Dim outArr(arrLen - 1) As T
            'copy the first part of the input array
            Array.Copy(arr, 0, outArr, 0, index)
            'then copy the second part of the input array
            Array.Copy(arr, index + 1, outArr, index, uBound - index)

            Return outArr
        End If
    End Function

Almost done, just want to know if is posible to completely delete the array instead of just hidding it.

I have not tried it before, but you can make use of ReDim Preserve.

You can recreate the array every time it is needed.

commented: And what i needed to finish, thx :) +0

Thank you, i got it working now :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.