5 Replies - 5555 Views - Last Post: 13 August 2007 - 08:16 PM Rate Topic: -----

#1 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Need help with MultiLine TextBox

Posted 10 August 2007 - 11:13 AM

OK heres what I'm trying to do, and its now officially driving me batty. In this application there are like 9 different parameters a user can enter to search by, so I have the textbox's for that, and a custom button control I made next to each textbox where they can say, enter parameter 1, hit add, enter a new number hit add, enter parameter 4 hit add, and it all gets added to a multi-line textbox for the search parameters, that was easy enough.


Heres the problem Im having. In the label the search categories are listed like:

Airline
Flight
From
Arrive
....

And as they enter a parameter for one of the categories I want to append it next to the appropriate category, i.e:

Airline : AA,UA
From: LAX,GEG,MIA
....
and so on

How do I find the right category in a setup like this and add the parameter next to the right category?

The categories wont be added until a parameter exists for it. I have a function to search for the category, and one to add it if it doesn't exist, but I cant quite get my head around how to do the last part.

Anyone have any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with MultiLine TextBox

#2 tody4me  Icon User is offline

  • Banned
  • member icon

Reputation: 12
  • View blog
  • Posts: 1,398
  • Joined: 12-April 06

Re: Need help with MultiLine TextBox

Posted 10 August 2007 - 11:39 AM

Why are you using a multi-line text box control to store the parameters? Is there a reason that you are not using a List Box or a Data Grid control? Best way that i see to handle this is with an array list or a hashtable in .net. If you use the Hashtable you can create a parameter type and parameter text structure / class in vb.net to hold what the user is opting for. Don't use the code below verbatim, it's not very good and just something to explain the concept more than syntax. Been a long time since i used VB.Net.

public class OptionClass
  Hashtable options = new Hashtable()
  enum optionType
	Airline = 0 <numeric is optional, otherwise VS adds it for you automatically>
	Flight = 1
	Departure = 2
	Arrival = 3
  end enum
  public sub AddOption(byval typeOption as optionType, byval optionCode as string)
	if not options.keys.exist(typeOption.ToString() + optionCode) then
	  option.add(typeOption.ToString() + optionCode, this)
	end if
  end sub
end class


Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Need help with MultiLine TextBox

Posted 10 August 2007 - 11:45 AM

Well I decided on the MultiLine Textbox simply because this is just something for the user to see what they've entered, it serves no other purpose. I have the background set to the same color as the form, it doesn't accept tab, its read only, the user wont even know its a textbox. I thought about the ArrayList but I'm still trying to figure out (I know how to read line by line through the textbox)

I just figured it out!

I have this function which searches the textbox for a category

Public Function IsCategoryLoaded(ByVal txt As TextBox, ByVal checkFor As String) As Boolean
		Try
			For Each Line As String In txt.Lines
				If Line.ToLower.Contains(checkFor.ToLower) Then
					Return True
				Else
					Return False
				End If
			Next
		Catch ex As Exception
			MessageBox.Show(ex.Message, "Error Loading Categories", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Return False
		Finally

		End Try
End Function



SO all I need to do now is when it finds the category add the new parameter right there, if it don't find the category then add the category then the parameter.

I cant believe I came up with an idea while I was posting this lol. Now lets see if it works
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4337
  • View blog
  • Posts: 12,137
  • Joined: 18-April 07

Re: Need help with MultiLine TextBox

Posted 10 August 2007 - 11:49 AM

Sometimes just talking it out and stepping through it will give you the idea. I have done it many times. I would have gone the same route as tody mentioned with the setup of an enum and transfer it to an array. Good call.
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Need help with MultiLine TextBox

Posted 13 August 2007 - 08:07 PM

Well I solved my dilemma (actually took more than I initially thought). Ive tried to come up with a cleaner way to do this, any ideas. Keep in mind, this is only for displaying and keeping a running total of the search parameters the user selects. The actual values to be sent to the database are stored in an ArrayList, which I convert to a comma delimited file before sending them to the database.

''' <summary>
	''' Function to check if a search category already exists in the list
	''' </summary>
	''' <param name="txt">Textbox to check in</param>
	''' <param name="cat">Category to check for</param>
	''' <param name="item">Search item to add</param>
	''' <remarks></remarks>
	Public Function AddToList(ByVal txt As TextBox, ByVal cat As String, ByVal item As String, ByVal tb As TextBox) As Boolean
		Dim blnAdded As Boolean = False
		Dim tmp As String
		Dim Cnt As Integer = 0
		Try
			'check to see if theres any lines in the textbox
			If txt.Lines.Length > 0 Then  'There is lines in the textbox
				'Loop through all the lines in the textbox looking for the category being passed
				For Each Line As String In txt.Lines
					'Check to see if this search category exists already
					If Line.ToLower.Contains(cat.ToLower) Then
						'Check to see if this search item exists already
						If Not Line.ToLower.Contains(item.ToLower) Then  'Search item doesnt exist
							'Add it to the textbox
							'Create an array of the lines in the textbox
							Dim lines() As String = txt.Lines
							'Get the length of the line (will be needed later)
							Dim lineLength As Integer = Line.Length
							'Set that index of the lines array to the line with the new item inserted
							lines(Cnt) = lines(Cnt).Insert(lineLength, " " & item.ToUpper)
							'Add the line back to the textbox
							txt.Lines = lines
							'Trip the boolean to True
							blnAdded = True
							Return True
							Exit For
						Else   'Already exists so let the user know
							MessageBox.Show("Sorry, " & item.ToUpper & " is already in the list", "Error: Duplicate Entry", MessageBoxButtons.OK, MessageBoxIcon.Error)
							tb.SelectAll()
							tb.Focus()
							Return False
							Exit Function
						End If
					End If
					Cnt += 1
				Next
			Else  'No lines in the textbox
				'Add the search category & item to the textbox
				tmp = txt.Text
				tmp += cat.ToUpper & ": " & item
				txt.Text = tmp.ToUpper
				blnAdded = True
				Return True
			End If

			'If it makes it through the loop without anything happening it means
			 'that category doesnt exists so now we need to start a new line for
			 'this new category
			If Not blnAdded Then
				'Add a new line
				txt.Text += vbCrLf
				'Get the length of the line (minus 1, its zero base4d)
				Dim length = txt.Lines.Length - 1
				'Create an array of the lines in the textbox
				Dim lines() As String = txt.Lines
				'Add the new category
				lines(length) = lines(length).Insert(0, cat.ToUpper & ": " & item.ToUpper)
				'Add the new line to the textbox
				txt.Lines = lines
				Return True
			End If
		Catch ex As Exception
			MessageBox.Show(ex.Message, "Error adding search item to list", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Return False
		End Try		
	End Function



Ive tried to, like I said earlier, find a cleaner way to pull this off but this is the only thing Ive gotten to work so far.

This post has been edited by PsychoCoder: 13 August 2007 - 08:18 PM

Was This Post Helpful? 0
  • +
  • -

#6 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Re: Need help with MultiLine TextBox

Posted 13 August 2007 - 08:16 PM

wish i could help yea! But the good news is i actually understand what your trying to do and i understand basically what code does what in your code box. Yeah me!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1