Swapping values in a combobox

Swapping values in a combobox that come from an array

Page 1 of 1

3 Replies - 1530 Views - Last Post: 07 November 2009 - 08:52 AM Rate Topic: -----

#1 trashr0x  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 27-October 09

Swapping values in a combobox

Post icon  Posted 06 November 2009 - 01:41 AM

OK so what I do is: I display some values in a combobox that come from an array.
By default the selected index of the combobox should be the first item.

If cboAtt.Items.Count = 0 Then
				For i = 0 To nums(att)
					cboAtt.Items.Add(cat(att)(i))
				Next
				cboAtt.SelectedIndex = 0
			ElseIf cboAtt.Items.Count > 0 Then
				cboAtt.Items.Clear()
				For i = 0 To num(att)
					cboAtt.Items.Add(cat(att)(i))
				Next
			Else
				Return
			End If


Now, when the users selects another value from the combobox, I want that value to be swapped with the initial value of the array (if order is 1,2,3 and user selects 3 then order becomes 3,1,2),
which I do with the following function:

Function swap(ByVal orig As String, ByVal swapB As String) As String
		Dim init, swapd, tmp As String
		init = orig
		swapd = swapB
		tmp = init
		init = swapd
		swapd = tmp
		Return init
	End Function


The function is used here:

Private Sub cboAtt_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboAtt.SelectedIndexChanged
		swap1 = swap(cat(att)(cboAtt.SelectedIndex), cat(att)(0))
		swap2 = swap(cat(att)(0), cat(att)(cboAtt.SelectedIndex))
		cat(att)(cboAtt.SelectedIndex) = swap1
		cat(att)(0) = swap2
		cboAtt.Items.Clear()
		For i = 0 To num(att)
			cboAtt.Items.Add(cat(att)(i))
		Next
			End Sub



This works perfectly, but now I want the combobox to display the new (initial) value that is swapped, except than when I do that the cboAtt_SelectedIndexChanged event is triggered which results to an infinite loop --> crash. Anyone knows how to avoid this?

Best regards

This post has been edited by trashr0x: 06 November 2009 - 01:43 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Swapping values in a combobox

#2 dzone41  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 216
  • Joined: 21-August 09

Re: Swapping values in a combobox

Posted 06 November 2009 - 05:47 AM

There are much simpler ways to do what your doing, but to answer your question, you can make a boolean value at the start of the Selected Index change code that sets to true when your code is running, after all code has been executed set it to false. When the selected index change event fires, it will check the value use an If..Then..Exit Sub if the value is true, and your code is still making changes in the comboBox. Just make that the absolute first and absolute last code that runs.
See if that works out.
Was This Post Helpful? 1
  • +
  • -

#3 trashr0x  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 27-October 09

Re: Swapping values in a combobox

Posted 06 November 2009 - 12:46 PM

Hey dzone41, that sounds good, I will experiment with it tonight.
Btw, you mentioned there are simpler ways, could you suggest one? It doesnt have to be written in code, I would just like to see the logic.
This is the best I could think of at the moment, but I am open to suggestions.
Was This Post Helpful? 0
  • +
  • -

#4 trashr0x  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 27-October 09

Re: Swapping values in a combobox

Posted 07 November 2009 - 08:52 AM

Private Sub cboAtt_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboAttBool.SelectedIndexChanged
		If bool = True Then
			swap1 = swap_bool(cat(att)(cboAtt.SelectedIndex), cat(att)(0))
			swap2 = swap_bool(cat(att)(0), cat(att)(cboAtt.SelectedIndex))
			cat(att)(cboAtt.SelectedIndex) = swap1
			cat(att)(0) = swap2
			cboAtt.Items.Clear()
			For i = 0 To num(att)
				cboAtt.Items.Add(cat(att)(i))
			Next
			bool = False
			cboAttl.SelectedIndex = 0
			Exit Sub
		Else
			bool = True
		End If
	End Sub


Worked fine ;) Cheers.

This post has been edited by trashr0x: 07 November 2009 - 08:54 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1