|
-
May 19th, 2013, 02:06 AM
#1
Thread Starter
Member
Not really sure how to do this...
This code will take the selected item and put the string of the item into an inputbox and then you can edit the item inside of the inputbox. However, if you delete everything in the edit inputbox then it makes the item Nothing and it is just a blank space in my listbox.
How would I make it so that if the user deletes everything in the inputbox then it will not change anything since i dont want blank spaces in the listbox.
Sorry if you don't understand the question.
Code:
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If ListBox1.SelectedItem <> Nothing Then
Dim output As String = ""
output = InputBox("Type a new message in the text box below.", "Edit Message", ListBox1.SelectedItem.ToString, -1, -1)
ListBox1.Items(ListBox1.SelectedIndex) = output
Else
MessageBox.Show("You haven't selected a message.", Nothing, Nothing, MessageBoxIcon.Error)
End If
End Sub
-
May 19th, 2013, 02:08 AM
#2
Fanatic Member
Re: Not really sure how to do this...
Hi
if output is "" then dont update the listbox
Yes!!!
Working from home is so much better than working in an office...
Nothing can beat the combined stress of getting your work done on time whilst
1. one toddler keeps pressing your AVR's power button
2. one baby keeps crying for milk
3. one child keeps running in and out of the house screaming and shouting
4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
5. working at 1 O'clock in the morning because nobody is awake at that time
6. being grossly underpaid for all your hard work

-
May 19th, 2013, 02:33 AM
#3
Thread Starter
Member
Re: Not really sure how to do this...
 Originally Posted by GBeats
Hi
if output is "" then dont update the listbox
This doesn't really help me...
-
May 19th, 2013, 02:48 AM
#4
Re: Not really sure how to do this...
 Originally Posted by Flavor Flav
This doesn't really help me...
Well it should. One of the intentions of VB is that the code reads very much like an English sentence. Here's an example of that in action, from your code:
Code:
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If ListBox1.SelectedItem <> Nothing Then
Dim output As String = ""
output = InputBox("Type a new message in the text box below.", "Edit Message", ListBox1.SelectedItem.ToString, -1, -1)
ListBox1.Items(ListBox1.SelectedIndex) = output
Else
MessageBox.Show("You haven't selected a message.", Nothing, Nothing, MessageBoxIcon.Error)
End If
End Sub
and from GBeats post:
if output is "" then dont update the listbox
See the similarity? You simply have to write an If statement. In your code you obviously can't write a statement to NOT do something so you simply reverse the logic:
if output is NOT "" then DO update the listbox
That's just simple logic and doesn't take any programming experience at all. All that remains is to implement it in code and you already know how to do that because it's no different to what you've already done elsewhere in your code:
Code:
If output <> "" Then
ListBox1.Items(ListBox1.SelectedIndex) = output
Next
One of the most important lessons for beginners to learn is that they need to work out the logic first, before they write the code. Logic is something we all use every day, with varying levels of formality, so you don't need any programming experience at all to do that part. You can then pick up a pen and paper and write down that logic as a set of steps. It's then much easier to write the code to implement the logic because you have a clear picture of what that logic is. At first, it's also recommended to add an extra step and convert the original logic to pseudo-code first. Before too long, you'll be able to do the pseudo-code step in your head and eventually you'll be able to do the logic step in your head in most cases too. Most beginners can't do it in their head though, and that's why so many people have so much trouble writing simple code to do simple things: because they try to go straight from idea to code without clearly defining the logic in between.
-
May 19th, 2013, 03:09 AM
#5
Thread Starter
Member
Re: Not really sure how to do this...
This is what I have and it says 'output' is not declared.
Code:
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If ListBox1.SelectedItem <> Nothing Then
Dim output As String = ""
output = InputBox("Type a new message in the text box below.", "Edit Message", ListBox1.SelectedItem.ToString, -1, -1)
ListBox1.Items(ListBox1.SelectedIndex) = output
Else
MessageBox.Show("You haven't selected a message.", Nothing, Nothing, MessageBoxIcon.Error)
End If
If output <> "" Then
ListBox1.Items(ListBox1.SelectedIndex) = output
End If
End Sub
I don't understand why I get that error because I declared it in the exact same sub...
-
May 19th, 2013, 03:54 AM
#6
Re: Not really sure how to do this...
That would be because it isn't. Again, you have not thought about the logic before writing the code. Why would you put that new code outside the original If...Else block? To start with, you were only displaying the InputBox if the user had selected an item and then updating the item no matter what the user entered. What part of that do you want to change? Just the bit that updates the item, right? So, what part of the original code does that? This part:
Code:
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If ListBox1.SelectedItem <> Nothing Then
Dim output As String = ""
output = InputBox("Type a new message in the text box below.", "Edit Message", ListBox1.SelectedItem.ToString, -1, -1)
ListBox1.Items(ListBox1.SelectedIndex) = output
Else
MessageBox.Show("You haven't selected a message.", Nothing, Nothing, MessageBoxIcon.Error)
End If
End Sub
That's the part of the code that modifies the item so that's the part that you have to change to modify the item only if 'output' is not an empty String. If you just add new code and don;t change that then you'll just be adding new behaviour without changing the existing behaviour.
-
May 19th, 2013, 04:00 AM
#7
Re: Not really sure how to do this...
 Originally Posted by Flavor Flav
don't understand why I get that error because I declared it in the exact same sub...
Yes, it's declared in the same method but a variable only exists within the block in which it is declared. You declared 'output' inside the If block so it only exists inside If block. If you want a variable to exist inside the method but outside the If block then you must declare the variable inside the method but outside the If block. The part of the code in which a variable exists is known as it's scope. Scope goes all the way down, i.e. any inner block can see a variable declared outside it, but does not extend up, i.e. a block cannot see a variable declared in an inner block.
-
May 19th, 2013, 04:17 AM
#8
Thread Starter
Member
Re: Not really sure how to do this...
UGHHHHHHH THIS ISN'T WORKING. I have no freaking idea what to do. Why can't you just give me the code? If I saw what you did I would remember it and it would help me. I am not experienced enough to know how to do this.
Code:
If ListBox1.SelectedItem <> Nothing Then
Dim output As String = ""
output = InputBox("Type a new message in the text box below.", "Edit Message", ListBox1.SelectedItem.ToString, -1, -1)
ListBox1.Items(ListBox1.SelectedIndex) = output
If output = "" Then
Else
MessageBox.Show("You haven't selected a message.", Nothing, Nothing, MessageBoxIcon.Error)
End If
End If
-
May 19th, 2013, 04:22 AM
#9
Thread Starter
Member
Re: Not really sure how to do this...
Solved it.
Code:
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If ListBox1.SelectedItem <> Nothing Then
Dim output As String = ""
output = InputBox("Type a new message in the text box below.", "Edit Message", ListBox1.SelectedItem.ToString, -1, -1)
If output <> Nothing Then
ListBox1.Items(ListBox1.SelectedIndex) = output
End If
Else
MessageBox.Show("You haven't selected a message.", Nothing, Nothing, MessageBoxIcon.Error)
End If
End Sub
Btw, why do people tell me never to use "Nothing" because something can never be "Nothing"??
-
May 19th, 2013, 05:51 AM
#10
Re: Not really sure how to do this...
 Originally Posted by Flavor Flav
Btw, why do people tell me never to use "Nothing" because something can never be "Nothing"??
I'm not sure who told you never to use Nothing but it was bad advice. Nothing should be used in VB when it's appropriate to use Nothing.
If you think about memory usage in its simplest form then a .NET variable is a binary number stored on the stack. Assigning Nothing to a variable will set that memory location to zero. Exactly what that means depends on the type of the variable. If it's a numeric type, e.g. Integer or Double, then it is the number zero. If it's a DateTime then it's 1/01/0001. If it's a Boolean then it's False. For other value types, i.e. structures, it is their default value, whatever that may be. For reference types, i.e. all classes and delegates, it is a null reference, i.e. no object. Reference types provide the most literal interpretation of Nothing. Consider yourself to be a Person object, i.e. an instance of the Person class. If that class has a Dog property and you have no dog then your Dog property is equal to Nothing. So, you should use Nothing with reference types when a variable or property refers to no object. It's less justified with value types but there are still instances where it's appropriate.
-
May 19th, 2013, 06:22 AM
#11
Re: Not really sure how to do this...
I should make special note of how Nothing works with Strings. String is a class so it behaves like other reference types. In order to Strings easier to work with though, the String class supports value equality as well as referential equality. With value types, you use the '=' and '<>' operators to test value equality. With reference types, you use the 'Is' and 'IsNot' operators to test referential equality. Unlike most types, the String class supports both and they behave slightly differently. Testing for referential equality determines whether two String variables refer to the same String object while testing for value equality determines whether two String variables refer to String objects that contain the same text, whether or not that's one String object or two. Nothing acts as no object when testing referential equality while it behaves as an empty String when testing value equality. Here's some code that compares and contrasts the two:
Code:
Private Sub StringEqualityTest()
Dim str1 As String 'str1 refers to no object
Dim str2 As String = "" 'str2 refers to an empty String
If TestValueEquality(str1, String.Empty) Then
MessageBox.Show("str1 contains the same value as String.Empty.")
Else
MessageBox.Show("str1 does not contain the same value as String.Empty.")
End If
If TestValueEquality(str1, Nothing) Then
MessageBox.Show("str1 contains the same value as Nothing.")
Else
MessageBox.Show("str1 does not contain the same value as Nothing.")
End If
If TestReferentialEquality(str1, String.Empty) Then
MessageBox.Show("str1 refers to the same object as String.Empty.")
Else
MessageBox.Show("str1 does not refer to the same object as String.Empty.")
End If
If TestReferentialEquality(str1, Nothing) Then
MessageBox.Show("str1 refers to the same object as Nothing.")
Else
MessageBox.Show("str1 does not refer to the same object as Nothing.")
End If
If TestValueEquality(str2, String.Empty) Then
MessageBox.Show("str2 contains the same value as String.Empty.")
Else
MessageBox.Show("str2 does not contain the same value as String.Empty.")
End If
If TestValueEquality(str2, Nothing) Then
MessageBox.Show("str2 contains the same value as Nothing.")
Else
MessageBox.Show("str2 does not contain the same value as Nothing.")
End If
If TestReferentialEquality(str2, String.Empty) Then
MessageBox.Show("str2 refers to the same object as String.Empty.")
Else
MessageBox.Show("str2 does not refer to the same object as String.Empty.")
End If
If TestReferentialEquality(str2, Nothing) Then
MessageBox.Show("str2 refers to the same object as Nothing.")
Else
MessageBox.Show("str2 does not refer to the same object as Nothing.")
End If
End Sub
Private Function TestValueEquality(s1 As String, s2 As String) As Boolean
'The '=' operator tests value equality.
Return s1 = s2
End Function
Private Function TestReferentialEquality(s1 As String, s2 As String) As Boolean
'The 'Is' operator tests referential equality.
Return s1 Is s2
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|