|
Hi Folks,
I am having difficulty sorting a custom class based collection on two fields and am wondering if anyone can assist me. I have a class that looks like so:
Class myClass
private ID as String
private Name as String
private TK as String
private Di as String
private M as String
End Class
Using this class i add to a Collection a number of items, e.g.
Private colClass As Collection
Private clsTheClass As myClass
Set colClass = New Collection
Set clsTheClass = New myClass
clsTheClass.Name = "A"
clsTheClass.TKs = "100"
clsTheClass.Di = "5"
clsTheClass.ID = "1"
clsTheClass.M = "9"
colClass.Add clsTheClass
What i end up with it is a collection that looks like so after adding all the items:
ID |Name |TK |Di |M
1 A 100 5 9
2 B 100 3 9
3 C 10 5 9
4 D 10 7 9
5 E 10 1 9
6 F 400 9 5
7 F 400 4 5
I need to sort the collection On TK, and then DI. That should make the collection look like this:
ID |Name |TK |Di |M
6 F 400 9 5
7 F 400 4 5
1 A 100 5 9
2 B 100 3 9
4 D 10 7 9
3 C 10 5 9
5 E 10 1 9
I am currently using this method to sort on TK, but i cannot get it to work how i need the output:
Public Sub SortCollection(ColVar As Collection)
Dim oCol As Collection
Dim i As Integer
Dim i2 As Integer
Dim iBefore As Integer
If Not (ColVar Is Nothing) Then
If ColVar.Count > 0 Then
Set oCol = New Collection
For i = 1 To ColVar.Count
If oCol.Count = 0 Then
oCol.Add ColVar(i)
Else
iBefore = 0
For i2 = oCol.Count To 1 Step -1
If CLng(ColVar(i).TK) < CLng(oCol(i2).TK) Then
iBefore = i2
Else
Exit For
End If
Next
If iBefore = 0 Then
oCol.Add ColVar(i)
Else
oCol.Add ColVar(i), , iBefore
End If
End If
Next
Dim MyNewCol As New Collection
Dim obj As Object
For Each obj In oCol
If MyNewCol.Count > 0 Then
MyNewCol.Add Item:=obj, before:=1
Else
MyNewCol.Add Item:=obj
End If
Next
Set ColVar = MyNewCol
Set oCol = Nothing
End If
End If
End Sub
Anyone got any pointers as to what i need to change in the sort routine?
Thanks In Advance
|
|
|
|
|
Hi Keith
Is it known how many elements are added to the collection or is that defined at runtime?
|
|
|
|
|
Whenever I sort, I find the largest, or the least first and set the value to "x". From there I work up, or down assigning to a temp object, collection or array, and replacing "x" as the largest each time.
For example,
x = 0
for each item in collection
if item > x then
x = item
end if
next
This gets the largest to begin.
tempCollection.add(x)
while tempcollection.count isnot collection.count
for each item in collection
set x if the item is less than the last in tempcollection and is not less than x
next
tempcollection.add(x)
loop
That is a level one sort, but you should be able to expand to code easily enough. Sorry that I lack any time, or I would have done it for you!
|
|
|
|
|
You could implement the IComparable interface in your class, and then define the CompareTo function. Put your comparison logic in this CompareTo function. Then all you would need to do is call the Sort method on your collection.
MyNewCol.Sort()
Roy.
|
|
|
|
|
I need to hide window when application starts. I use VB 2008 on Windows CE 5.0.
|
|
|
|
|
Take a look in Form properties WindowState
"It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan
That's what machines are for.
Got a problem?
Sleep on it.
|
|
|
|
|
There is nothing like this. No property ShowWindow or Visible or something like that.
|
|
|
|
|
I think he meant starting your form "minimized".
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Yes, that's the general idea. It's no sweat in C++, just set the last WinMain parameter.
"It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan
That's what machines are for.
Got a problem?
Sleep on it.
|
|
|
|
|
How can I set main window minimized?
|
|
|
|
|
I wasn't able to find those settings either.
Read this article and maybe you'll be able to work around the problem.
Also, setting the WindowState for a form that doesn't exist yet won't work, which has also been mentioned elsewhere.
"The problem with dotnet smartdevice forms is, that using FormState in the Forms constructor has no effect. As the contructor is called, you cannot alter the formstate, you have to use a schedule to set the formstate to Minimized. Or, better, do not use a form at all."
I'll post back if I find anything useful.
=== UPDATE (reposted) ===
Try the OpenNETCF SmartDeviceFramework Community edition (free)
"It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan
That's what machines are for.
Got a problem?
Sleep on it.
modified 24-Apr-13 10:40am.
|
|
|
|
|
How can I minimize a main form before start? I could be solution for me too.
|
|
|
|
|
Ignoring the project type does your IDE have the same layout/windows as Figure 3 here?
If you look at the bottom right-hand corner that's where you'll see it.
Sorry I can't find a better example on msdn site, they've taken dowm a lot of the WinCE VB stuff, but using VS2008 doesn't help either. I don't have VS2008 installed right now, but I'll try and sort something out so that I can see what/where the problem lies.
"It's true that hard work never killed anyone. But I figure, why take the chance." - Ronald Reagan
That's what machines are for.
Got a problem?
Sleep on it.
|
|
|
|
|
Hi all.
I have an interesting problem (Interesting to me, anyway ).
I have a form loading, and in the process, an RTB is being loaded with a really super long RTF string. In the process, I get the ContextSwitchDeadlock message box. Now, from what I can gather, it seems to be telling me that system requests have not been able to be carried out for at least 60 seconds while the string is being loaded, and the message is telling me that you can't expect to be able to do anything else while that is going on, AND there may be an evergrowing queue of system tasks that have been put off until the string finishes loading. If this is true, I imagine I am getting the message because this is not a case where DoEvents() would be of any help...As the RTB is being loaded with the string, all of that activity is not code that a DoEvents() line can be inserted into.
So I started looking around, and discovered talk about message pumps (message loops), but no great information on them in general. So my question is, is a message pump what I need, and if so, how do I implement such a thing?
Thanks for any guidance!
|
|
|
|
|
Your app already has a message pump. It always runs on the UI thread (startup thread) and it's the little bugger that triggers that message "Not responding" when your code blocks the UI thread and the message pump can't be processed because of it.
The message you're getting is because COM interop happens through the message pump. You app is using a COM-based component (whether you know it or not) and COM message are not being processed because the message pump is prevented from running by your extremely long running operating (the RTF load).
Adding a second message pump isn't going to accompish anything because it also will be blocked.
The good news is that message only appears when running the code under the debugger. Even better, you can disable it in your app.config. See this[^].
THe better solution would be to NOT load such a massive document into the RTB.
|
|
|
|
|
AAAhhh...OK. Got it. And I can breath a sigh of relief since I am sooooo close to doing a final build and MSI.
I suppose I could break the RTF string up into pages and let new pages load when the scrollbar brings those portions into view.
|
|
|
|
|
Is there any way to create a Global HotKey in vb.net that is shared across all the forms in a project, not just one form that is registered to?
|
|
|
|
|
Possible??? Yes.
A good idea?? Not so much, depending on what you want to happen if you have multiple forms running at the same time.
If you want each form to process the hotkey, you'll have to implement things a bit differently than if you wanted only one action to happen not matter which and how many forms are open.
So, which is it??
|
|
|
|
|
I was trying add current date to a textbox in the current control when a user press something like Alt+D. I used an example from this Article: Setting A Global Hot Key I modified the code to something like this:
Protected Overrides Sub WndProc(ByRef msg As System.Windows.Forms.Message)
Select Case msg.Msg
Case ForcHotKey.WM_HOTKEY
If CShort(msg.WParam) = m_HotkeyID Then
If TypeOf (Me.ActiveForm.ActiveControl) Is TextBox Then
Me.ActiveForm.ActiveControl.Text = Me.ActiveForm.ActiveControl.Text.Insert(Me.ActiveForm.ActiveControl.Text.Length, DateTime.Now.ToShortDateString)
Dim tb As TextBox
tb = Me.ActiveForm.ActiveControl
tb.Select(Me.ActiveForm.ActiveControl.Text.Length, 0)
End If
End If
Exit Select
Case Else
MyBase.WndProc(msg)
Exit Select
End Select
End Sub
All I have to do is to register the hotkey in the main MDI Form. No matter which form I'm at, while typing in the textbox whenever I press hotkey I get the current date.
|
|
|
|
|
Well, I guess that answers the question of exactly what you were trying to.
Glad you got it working.
|
|
|
|
|
I have a form that starts with nothing on it.
I am adding controls to the form dynamically at run time.
This is the class declaration for the form.
Public Class foo
Public Sub New()
InitializeComponent()
End Sub
End Class
So here is my question - how do I force the developer to always create a new instance of the form rather than allowing them to refer to it without the new keyword?
I need to do this as I pick up old instances when I refer back to the form.
At present the developer needs to code.
fooHelp.filterForm(New foo,...)
If the developer codes the following, issues are hit later when old instances are pulled back.
fooHelp.filterForm(foo,...)
How do I ensure that when the developer codes fooHelp.filterForm(foo,...) they get an error message?
I feel like I was hit by the silly stick over the weekend and have had not had any luck with google(probably because I don't know how to phrase my question due to the silly stick...).
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
|
Thanks Eddy - I thought I had not hit this issue before in C# which is what you explain.
I may just need to find some way of coding around this - I can just see someone forgetting to use the new keyword, as I did this morning, and causing chaos...
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
Just created a vb-project, thrown in a second blanc Form and called it from a button's click using the ugly "Form2.Show"-approach. Using ILSpy, it's visible what's being generated;
internal sealed class MyForms
{
public Form1 m_Form1;
public Form2 m_Form2;
public Form2 Form2
{
[DebuggerNonUserCode]
get
{
this.m_Form2 = MyProject.MyForms.Create__Instance__<Form2>(this.m_Form2);
return this.m_Form2;
}
[DebuggerNonUserCode]
set
{
bool flag = value == this.m_Form2;
if (!flag)
{
flag = (value != null);
if (flag)
{
throw new ArgumentException("Property can only be set to Nothing");
}
this.Dispose__Instance__<Form2>(ref this.m_Form2);
}
}
} It's sealed, and generated by the compiler. It's only "illegal" if you change the project type from "Windows Forms Application" to "Console Application". Alternatively, one could consider the factory-pattern for forms;
Public Class Form2
<Obsolete("Use the CreateNew method")>
Sub New()
Throw New ApplicationException("")
End Sub
Private Sub New(ByVal skip As Boolean)
InitializeComponent()
End Sub
Public Shared Function CreateNew() As Form2
Return New Form2(True)
End Function
End Class The generated code will try to use the parameterless version of the constructor, and throw an ApplicationException . Not a very good alternative.
I think this would be better solved by writing an FxCop-rule.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
The factory pattern looks like an alternative that should be straightforward to implement.
This is the first time I have hit something in VB which has worked against me.
Thanks for all of this - it's been something of an education coming up against this issue.
Thanks for all your help!
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|