Jump to content
Xtreme .Net Talk

snarfblam

Leaders
  • Posts

    2156
  • Joined

  • Last visited

  • Days Won

    2

Everything posted by snarfblam

  1. Have you tried inheriting the panel class and overriding the OnPaint and/or OnInvalidated methods?
  2. What sort of graphics are these? Double buffering will probably help, especially if you are animating something. Just render your graphics to a Bitmap and when you are done draw it to the panel (the easiest way to do this is by setting the bitmap as the panel's background image and invalidating the panel with the bitmap's bounds when you want to refresh the image).
  3. To elaborate on number 4, a normal Windows application will have a main form. When this main form is closed the application will terminate. Application.Exit() is not a preferred way to end an application (just as using an End command is not the preferred way to end a VB6 application). I would say the closest VB.NET equivalent to Unload Form1 would be Me.Close() inside Form1's code (you could also use Form1.Close() if you are using magic instances).
  4. I believe you've come to the wrong place. This should probably be posted at Xtreme Visual Basic Talk
  5. You could make them owner drawn.
  6. What problem are you having? You should be able to paste it in as-is, regardless of which version of VS you are using.
  7. You could use the Delegate.CreateDelegate method.
  8. If there is no space in the string, the code will certainly fail. I think you are cramming too much logic into one line, which makes it hard to consider all of the possibilities. If you break it up... Dim FirstSpace As Integer Dim FirstWord As String Dim RestOfText As String FirstSpace = Input.IndexOf(" ") FirstWord = Input.Substring(0, FirstSpace) RestOfText = Input.Substring(FirstSpace, Input.Length - FirstSpace) Dim Result As String = FirstWord.ToUpper() & RestOfText ...potential errors become more obvious. FirstSpace could have a value of -1 if there are no spaces, and that will cause an error when passed to the Substring function. Now we can easily add code to deal with the possibility of no spaces. Dim FirstSpace As Integer Dim FirstWord As String Dim RestOfText As String FirstSpace = Input.IndexOf(" ") If FirstSpace = -1 Then ' Special case! 'If there are no spaces then the whole string is the first word FirstWord = Input RestOfText = "" Else FirstWord = Input.Substring(0, FirstSpace) RestOfText = Input.Substring(FirstSpace, Input.Length - FirstSpace) End If Dim Result As String = FirstWord.ToUpper() & RestOfText I'm not trying to hand you the answer (this forum typically prefers to guide you in the right direction), but make the point that it is important for your code to clearly demonstrate logic.
  9. Nine would probably suffice, but would it hurt to just leave it at 64 chars like the rest? Is memory scarce? How is this data being stored (binary/text file, database, etc.)?
  10. Try calling TreeView.BeginUpdate() before you begin modifying the TreeView and calling TreeView.EndUpdate() when you are done.
  11. At the point where the delegate is created, you must have a matching delegate type. In order to create the delegate, the CLR must know the signature of the function, and this means that it needs a delegate type. DotNet supports late binding with delegates by using the DynamicInvoke method of System.Delegate. This makes things easier. I whipped up this demo console app which is as close, I think, as possible to what you want. If you run the program you will see the expected output. [Color=Blue]Module [/Color]Module1 [Color=Green] ' Place to store a list of functions [/Color] [Color=Blue]Dim [/Color]delegates [Color=Blue]As New[/Color] List([Color=Blue]Of [/Color][Delegate]) [Color=Blue]Sub [/Color]Main() [Color=Green] ' In order to create these delegates, DotNet needs to know the signature. ' Otherwise DotNet can't invoke the method. That means we need a delegate type. ' This is why you must declare a variable with a delegate type (or cast an "AddressOf" ' to a delegate type before using it). [/Color] [Color=Blue]Dim [/Color]a [Color=Blue]As [/Color]DelA = [Color=Blue]AddressOf [/Color]Test1 [Color=Blue]Dim [/Color]b [Color=Blue]As [/Color]DelB = [Color=Blue]AddressOf [/Color]Test2 [Color=Blue]Dim [/Color]c [Color=Blue]As [/Color]DelC = [Color=Blue]AddressOf [/Color]Test3 [Color=Green] ' We can then add the delegates to a delegate array. [/Color] delegates.AddRange([Color=Blue]New [/Color][Delegate]() {a, b, c}) [Color=Green]' You can do it without declaring a variable beforehand like so.[/Color] delegates.Add([Color=Blue]CType[/Color]([Color=Blue]AddressOf[/Color] Test1, DelA)) [Color=Green]' And invoke them using late binding.[/Color] Console.WriteLine(MakeString(delegates(0).DynamicInvoke())) Console.WriteLine(MakeString(delegates(1).DynamicInvoke(5))) Console.WriteLine(MakeString(delegates(2).DynamicInvoke("Five"))) [Color=Green]' Wait for ENTER[/Color] Console.ReadLine() [Color=Blue] End Sub[/Color] [Color=Blue]Function [/Color]MakeString([Color=Blue]ByVal [/Color]obj [Color=Blue]As Object[/Color]) [Color=Blue]As String[/Color] [Color=Blue]If [/Color]obj [Color=Blue]Is Nothing Then[/Color] [Color=Blue]Return [/Color]"(NULL)" [Color=Blue]End If[/Color] [Color=Blue]Return [/Color]obj.ToString() [Color=Blue]End Function[/Color] [Color=Green]'Test delegates[/Color] [Color=Blue]Delegate Sub[/Color] DelA() [Color=Blue]Delegate Function [/Color]DelB([Color=Blue]ByVal [/Color]x [Color=Blue]As Integer[/Color]) [Color=Blue]As Integer[/Color] [Color=Blue]Delegate Function[/Color] DelC([Color=Blue]ByVal [/Color]x [Color=Blue]As String[/Color]) [Color=Blue]As Integer [/Color] [Color=Green]'Test Functions[/Color] [Color=Blue]Sub [/Color]Test1() [Color=Blue]End Sub[/Color] [Color=Blue]Function [/Color]Test2([Color=Blue]ByVal [/Color]x [Color=Blue]As Integer[/Color]) [Color=Blue]As Integer Return [/Color]x + 1 [Color=Blue]End Function[/Color] [Color=Blue]Function [/Color]Test3([Color=Blue]ByVal [/Color]x[Color=Blue] As String[/Color])[Color=Blue] As Integer[/Color] [Color=Blue]Return [/Color]x.Length [Color=Blue] End Function End Module [/Color]
  12. I'm not particularly experienced with multi-threading, but my understanding is that one should not interact directly with the UI from secondary threads. Caching values before invoking the thread or obtaining the values via the Invoke method would probably be the way to go.
  13. The compiler error asks an important question. Are you sure you didn't forget the parentheses in the WinForms app? Without parentheses, a function name evaluates to a method group. In case you aren't aware, or for the benefit of other programmers, a method group is a C# "type" comprised of all overloads of a method. Parentheses are required to invoke a function, even if the method has no arguments, and even if there is only one overload, in order to resolve an overload. It is a lexical matter. In other words, this error typically means you forgot your parentheses. If you forget your parentheses, you identify a method group instead of invoking a method. The compiler politely asks if you wouldn't rather invoke a method.
  14. The ideal method would probably be to use a background worker for a multi-threaded approach. At the very least, throw an Application.DoEvents() in your loop. And, yes, throwing Thread.Sleep in a loop to time it is generally frowned upon.
  15. Did you try putting quotes around the %1? File names with spaces in them need to be surrounded with quotes or the command line will think that the spaces are separating different file names.
  16. Go to the registry or "Folder Options" and see what the actual command is for the "Edit" action. There should be a %1 or "%1" in there. When the user selects "Edit" from the context menu the name of the file that is selected will be substituted for the %1. I'm guessing you'll be better off going with the quotes.
  17. [Color=Green]' Option 1 - Only works if there is only one Form2, not very MDI friendly. [/Color] [Color=Blue] Private Sub [/Color]NewDocumentToolStripMenuItem_Click([Color=Blue]ByVal [/Color]sender [Color=Blue]As [/Color]System.Object, [Color=Blue]ByVal [/Color]e [Color=Blue]As [/Color]System.EventArgs) [Color=Blue]Handles [/Color]NewDocumentToolStripMenuItem.Click Form2.Text = "New Document " + ([Color=Blue]Me[/color].MdiChildren.Length + 1).ToString() Form2.MdiParent = [Color=Blue]Me[/Color] Form2.WindowState = FormWindowState.Maximized Form2.Show() Form2.Update() [Color=Blue]End Sub[/Color] [Color=Blue]Private Sub [/Color]ShowTextInTheSelectedChildFormToolStripMenuItem_Click([Color=Blue]ByVal [/Color]sender [Color=Blue]As [/Color]System.Object, [Color=Blue]ByVal [/Color]e [Color=Blue]As [/Color]System.EventArgs) [Color=Blue]Handles [/Color]ShowTextInTheSelectedChildFormToolStripMenuItem.Click MessageBox.Show(Form2.TextBox1.Text) [Color=Blue] End Sub[/Color] [Color=Green]' Option 2 - Probably better[/Color] [Color=Blue]Dim [/Color]Documents [Color=Blue]As New [/Color]List([Color=Blue]Of [/Color]Form2) [Color=Green]' Depending on your approach, you might not need this.[/Color] [Color=Blue] Private Sub [/Color]NewDocumentToolStripMenuItem_Click([Color=Blue]ByVal [/Color]sender [Color=Blue]As [/Color]System.Object, [Color=Blue]ByVal [/Color]e [Color=Blue]As [/Color]System.EventArgs) [Color=Blue]Handles [/Color]NewDocumentToolStripMenuItem.Click [Color=Blue]Dim [/Color]Document [Color=Blue]As [/Color]Form2 = [Color=Blue]New [/Color]Form2 Document.Text = "New Document " + ([Color=Blue]Me[/color].MdiChildren.Length + 1).ToString() Document.MdiParent = [Color=Blue]Me[/Color] Document.WindowState = FormWindowState.Maximized Document.Show() Form2.Update() Documents.Add(Document) [Color=Blue] End Sub[/Color] [Color=Blue]Private Sub [/Color]ShowTextInTheSelectedChildFormToolStripMenuItem_Click([Color=Blue]ByVal [/Color]sender [Color=Blue]As [/Color]System.Object, [Color=Blue]ByVal [/Color]e [Color=Blue]As [/Color]System.EventArgs) [Color=Blue]Handles [/Color]ShowTextInTheSelectedChildFormToolStripMenuItem.Click [Color=Green] ' I don't know how you plan on picking wich document to perform the action ' This would perform the action on all documents [/Color] [Color=Blue]For Each [/Color]Document [Color=Blue]As [/Color]Form2 [Color=Blue]In [/Color]Documents MessageBox.Show(Document.TextBox1.Text) [Color=Blue]Next[/Color] [Color=Green]' This would perform the action on the currently active document [/Color] [Color=Blue]Dim [/Color]SingleDocument [Color=Blue]As [/Color]Form2 = [Color=Blue]TryCast[/Color](Me.ActiveMdiChild, Form2) [Color=Blue]If [/Color](Form2 [Color=Blue]IsNot Nothing[/Color]) [Color=Blue]Then[/Color] MessageBox.Show(SingleDocument.TextBox1.Text) [Color=Blue]End If End Sub[/Color]
  18. Re: What about 64 bit processors? I checked and the CLR does support a "natural integer" (native) type, with opcodes for built-in support, but with my C# Express 2008 set to compile for "Any CPU," the result is Int32--explicitly 32 bits. I haven't tried compiling for 64-bit, but I'm guessing that you will see the same result: 32 bits. I think Microsoft would prefer that an int is always the same thing, no matter which processor you use, because a lot of code depends on an int being 32-bits. It would be nice if C# or VB supported a native integer as well, though. I'd have no problem declaring my variables a nints.
  19. Re: What about 64 bit processors? It's an important aspect of managed code, but (and correct me if I'm wrong) doesn't a C# int or VB Integer turn into a System.Int32 when compiled?
  20. You are getting the error because you are opening the CurrentUser hive instead of ClassesRoot when you are saving the setting. If a key does not exist then a null reference is returned instead of an exception being thrown. Also, not only should you make this an option, but don't be surprised if an exception is thrown when the code is run on a limited user account.
  21. The easiest way to do this is to go to the designer of DocumentForm, select your DHTML control, and set its "Modifiers" property to "Friend" or "Public." You also need to keep a reference to your document form, probably using a class-level variable (unless you are using a version of VB that supports default instances and it is enabled).
  22. If you want to find the angle of the intersection then you need to study up on some trigenometry. I'll be more than glad to help if you get stuck, but you really need to find a trig website, which will explain how to get the angle of a line based on its slope. The angle of the intersection will be the difference of the angle of the lines, and the reflected angle will simply be the compliment to the angle of intersection.
  23. Here is a really simple feedback agent I wrote in C# (I've inlined it and translated it to VB for you). [Color=Green]'The variables used to initialize post are already declared or parameters [/Color] [Color=Green]'Strings should be formatted for a URI[/Color] [Color=Blue]Dim [/Color]post [Color=Blue]As String [/Color]= _ "&type=" & Uri.EscapeUriString(feedbackType) & "&followup=" & Uri.EscapeUriString(requestReply) & "&email=" & Uri.EscapeUriString(email) & "&version=" & Uri.EscapeUriString(appVersion) & "&message=" & Uri.EscapeUriString(message) [Color=Blue]Dim [/Color]postBuffer [Color=Blue]As Byte[/Color]() = [Color=Green]'Then we get the text in a binary format. I believe that PHP prefers ASCII.[/Color] System.Text.Encoding.ASCII.GetBytes(post); [Color=Green]'Create and initialize the request[/Color] [Color=Green]'Note that the URL is not hardcoded.[/Color] [Color=Blue]Dim [/Color]request [Color=Blue]As [/Color]HttpWebRequest = _ [Color=Blue]DirectCast[/Color](WebRequest.Create(feedbackUrl), HttpWebRequest) request.UserAgent = "My Program Agent" request.Method = "POST" [Color=Green]'Write POST data to request[/Color] request.ContentType = "application/x-www-form-urlencoded" request.ContentLength = postBuffer.Length [Color=Blue]Dim [/Color]requestStream [Color=Blue]As [/Color]Stream = request.GetRequestStream() requestStream.Write(postBuffer, 0, postBuffer.Length) requestStream.Close() [Color=Green]'Execute request and read response.[/Color] [Color=Blue]Dim [/Color]response [Color=Blue]As [/Color]HttpWebResponse = _ [Color=Blue]DirectCast[/Color](request.GetResponse(), HttpWebResponse) ReadResponse(response) postBuffer = [Color=Blue]Nothing[/Color] [/Code]
  24. Re: What about 64 bit processors? I believe that MSIL supports a native-sized integer, but no languages that I know of support this; it is, by definition, inconsistent. Like I said before, integers smaller than native size should perform equally with arithmatic operations and faster when copying arrays. The JIT compiler manages the packing and padding for you. The only real difference I would expect to see in terms of integer size versus performance is that 64-bit integers won't be slow anymore. I don't quite get your meaning when you talk about giving the compiler flexibility. Flexibility in our code? Or flexibility programmed into the compiler by Microsoft?
  25. Re: Integer for single variables, correct size for data blocks Here's my two cents, though it amounts to about the same thing as MrPaul's. There are some different considerations in terms of performance. Copying arrays of bytes will be faster than copying arrays of integers since they are one fourth the size, but most arithmatic operations are performed as integers and you will see very similar execution speed. Even if you add two short integers or two bytes, the processor still uses 32-bit operations and the extra bits are simply truncated after the overflow check. When you are working with a structure, depending on how members are packed, smaller data types might even perform worse than a full Int32. If four bytes are packed into a single 32-bit word, I would imagine it would take a little longer to retrieve or store a value into one of these members. In terms of memory, the real question is whether you need to conserve the bytes. Unless you are creating a very large number of objects or large arrays, you would probably never know the difference. If the integer is a member of a structure, the amount of memory you save would depend on how the data is laid out. Structures are padded to fill out 32-bit boundaries. And if the integer is a member of a class it is likely to be padded. Classes aren't intended to be binary creatures like structures, and I would guess that the jit-compiler would use 32 bits for any member 32-bits or smaller. As far a local variables go, they reside on the stack, so I really wouldn't worry about saving memory. Use what's simplest. If I had to give a one sentence answer, I would say don't bother using smaller integers unless you are in need of a memory optimization.
×
×
  • Create New...