Click here to Skip to main content
15,916,398 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Have a really simple question.

I have a list on my main form which is publically shared. It's called textboxestime.

This dictionary holds values for the name of the textbox, and also how many ticks have been added since it was created.

I kind of know in English what I want it to do, but I can't word it correctly.

if main.selectedbox.name = main.textboxestime.name
result = cstr(main.textboxestime.value)

But It is saying name is not a member of dictionary.

I would have thought if I can see the name within the dictionary list, I could query it?

I create the dictionary as per below

Dim first As New RichTextBox
Using first
first.Name = "first"
textboxestime = New Dictionary(Of RichTextBox, Integer)
textboxestime.Add(first, 0)
End Using

Then on each tick:
C#
Dim textBoxes As List(Of RichTextBox)

        textBoxes = textboxestime.Keys.ToList()
        For Each TextBox In textBoxes
            ' Increase time for each TextBox
            textboxestime(TextBox) = textboxestime(TextBox) + 1
            ' Check if TB reached 30 secs
            If textboxestime(TextBox) = (timeralert) Then
                TextBox.BackColor = Color.Red
                TextBox.ForeColor = Color.White

            End If
        Next
        If internaltimer = 300 Then
            Close()
        End If
Posted
Comments
Sergey Alexandrovich Kryukov 26-Jan-16 21:47pm    
What is "dictionary list"? :-)
What is the purpose of timer?
What is the purpose of such a weird dictionary. What is the role of key (RichTextBox) and value (integer)?
If you do it as is, your dictionary help to find integer by a RichTextBox integer quickly (O(1)). Is that what you need?
—SA

1 solution

I see no sense in your code and in formulation of the problem at all. Not even close. I'm almost sure that you do something wrong.

So, let me answer formally. System.Collections.Generic.Dictionary<KEY, VALUE> helps to find VALUE by KEY instance. It gives you time complexity of O(1), for the following operations:
Dictionary(TKey, TValue).ContainsKey Method (TKey) (System.Collections.Generic)[^],
Dictionary(TKey, TValue).TryGetValue Method (TKey, TValue) (System.Collections.Generic)[^].

It should explain you how to search in dictionary. If you do something else, you don't need dictionary at all. And you are doing "something else". You never really use your dictionary for anything useful. (Please see my comments to the question.) You use Keys.ToList(), but you could have only the list of the instances of RichTextBox. Also, there is no need to have this member "publicly shared". The internal access modifiers would do it. It's better not to give more access than it's really required. And it seems highly suspicious to me that you use a timer. We can discuss it only if you explain the goal.

You search in your list anyway. Slowly, at O(N). Moreover, you do your operation for all list elements meeting certain condition. You can also do it using LINQ or this method: List(T).ForEach Method (Action(T)) (System.Collections.Generic)[^].

However, I don't think it makes any sense. You should better explain the ultimate goal of all this activity.

—SA
 
Share this answer
 
Comments
Mendaharin 26-Jan-16 22:17pm    
It wouldn't surprise me if I have done something the wrong way - so I'll accept that as an answer :)

The program I have written creates dynamic textboxes which are placed on a form.
After the textbox is created, the name is generated for the textbox (It's the current date/time) and that name is added to the dictionary as well as a starting integer of 0.

On each tick of the timer, each of the textbox entries in the dictionary has value + 1 added to it, incrementing the "seconds" the textbox has been present.

The next part of the program is I have a button which opens up a new form. This form allows me to close the selected textbox, but I also want it to be able to show the time value stored within the dictionary.

The reason that I've done it the way I have is each textbox would have a different value for time. Originally I thought of using an array to store the values, but was guided down this path instead.
Sergey Alexandrovich Kryukov 26-Jan-16 22:53pm    
Well, you again explain it in terms of implementation; but it would be more useful to explain what you want to achive. It's bad to use timer, a separate thread would do it with much less trouble. Why RTF and not plain text box? What is "name", what it's use?

By the way, you did not really accept the answer formally — there is a button for that... Of course, it's up to you.

—SA
Mendaharin 26-Jan-16 23:23pm    
The timer is in a separate thread.

I don't understand why using a timer to increase a value of an object and have time limits attached to separate textboxes is the wrong way to do it. How would one implement a timed function?

I'm using a richtextbox as I append each line as different colours depending on leading characters.

A name is generally something you attach to an object for identification. The textbox needs a name so it can be referenced easily enough. The same as you have a name(Sergey) and you have a value (Age/Height etc)

I'm simply trying to find out how I can find out what textbox.name's value
Textbox.name and value is stored in a dictionary called textboxestime.
I want to use a msgbox to show the value.
Sergey Alexandrovich Kryukov 26-Jan-16 23:30pm    
I did not mean that. I meant a separate thread instead of timer. Again explain what you want to achieve first.
With RTF, it's clear. No, text box does not need a name. The available property Name should not be used. It's a misconception to find anything by name or identify anything at all. You have the object reference, that's it. So, forget the name. Just forget it. Not only you did not find any use of a dictionary, but you did not have a use of a name.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900