snarfblam
Leaders-
Posts
2156 -
Joined
-
Last visited
-
Days Won
2
Content Type
Profiles
Forums
Blogs
Events
Articles
Resources
Downloads
Gallery
Everything posted by snarfblam
-
If you are stuck with version 1.0 then I would say that you are using the right approach.
-
The observer pattern and the "event pattern" aren't very different. The observer pattern is very typical of Java. It is based on interface types, which is the only reasonable way that Java could implement an event driven model. The event pattern is based on a simpler type that is somewhat similar to an interface, the delegate. While both the interface and delegate point to an object and allow you to access methods of that object, different delegates of the same type can point to different methods of the same object (interfaces can not). While the two patterns have their differences, the fundamental aspects of the two are the same. The use of delegates only allows us to treat different events more independently. (We don't need to declare two entire interfaces for each type of event, and we can use the same delegate type for more than one event of a class. In other words, much less code.) The fact remains, though, that the two patterns rely on the same principal. Indeed, the "event pattern" is really nothing more than a different form of the observer pattern.
-
This is not the case at all! It can be done, and without unsafe code. The attached tutorial is written in C#, but it is done in a language-neutral way (it is only half as fast as using pointers, but it works in VB). Lemme know if you need help with the translation. [Edit2] Guess Mr Paul is a little faster than me.[/Edit2] BitmapTutorial.zip
-
You forgot the most important part. What, exactly, did you have to work around?
-
Then I can't think of a reason why this shouldn't work. Have you tried reloading the images into the ImageList? Is the ImageList populated at run-time or design-time? Maybe you ought to post the code that populates the ImageList and the code that takes the image from the ImageList and shows it in the PictureBox.
-
Try setting the ImageList's ColorDepth property. By default it is 8 bits per pixel, which is just, well, silly, because it makes gradients, detailed color, and alpha transparency look all kinds of bad. You may or may not have to remove the images from the ImageList and then reload them (after you change the ColorDepth) in order for them to appear correctly.
-
Unfortunately, creating labels also requires much more resources than the graphics object, and it is not designed to do what you are using it for, which means that you may run into unexpected bugs and behavior might change in future versions. As far as taking borders into account, well, that's why we have the ClientRectangle property. We can compare ClientRectangle to Bounds and find the size difference, i.e. the border size. Public Class AutosizeTextBox Inherits TextBox Dim _AutoSize As Boolean = True <System.ComponentModel.DefaultValue(True)> _ Public Shadows Property AutoSize() As Boolean Get Return _AutoSize End Get Set(ByVal value As Boolean) _AutoSize = value If _AutoSize Then SizeControl() End Set End Property Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs) MyBase.OnTextChanged(e) If _AutoSize Then SizeControl() End Sub Sub SizeControl() 'Use a graphics object to measure the text Dim g As Graphics = Me.CreateGraphics() 'Measure the text Dim s As SizeF = g.MeasureString(Me.Text, Me.Font, 1000000) 'Adjust for the border s.Width += Me.Width - Me.ClientRectangle.Width s.Height += Me.Height - Me.ClientRectangle.Height 'Extra space for the caret s.Width += 3 'Set the size Me.Size = s.ToSize 'Hack: Because the box isnt resized until _after_ 'new text is typed, the text is scrolled to the left, 'out of view. This code brings it back into view. Dim selection As Integer = SelectionStart SelectionStart = 0 SelectionStart = selection 'Dispose our IDisposables g.Dispose() End Sub End Class
-
The AutoSize property is hidden because it is not implemented (hence, it does not work). There are many properties that are hidden because they aren't implemented or become irrelevant in a derived class. Also, instead of creating a label to measure the size of a string, why not create a graphics object and use the MeasureString function to measure the string?
-
Firstly, what is Mutant? It sounds like it would describe something that has changed (mutate). As far as I know that is not a synonym for mutex. Mutex is an abbreviation for "mutually exclusive." Secondly, when you say handles, do you mean handles to Windows resources or references to .Net objects? Over what period of time are you observing this behavior? Are you doing anything that might interfere with garbage collection? Are you forcing any garbage collections? Are you using third party components? Who makes the components? Are you using any objects that are disposable that you aren't disposing? What I really recommend you do is go over you code and be very familiar with where you allocate what. Consider refactoring. Simplify and unify code. It will make it easier to identify where resources are allocated and which resources might not be de-allocated.
-
You could use the Exists method. Depending on exactly how you make use of it it can be pretty flexible. It accepts a delegate that determines if an object meets a condition, or a "predicate" (defined by you), and returns a boolean. The example below shows one way to use it. Things could be made simpler with reflection, though. This is a class that implements methods to enable us to use the List<T>.Exists method: // Define the class we want to compare. We will have two properties // and two predicate methods that compare based on a property. class SomeClass { // Define a property for the example string backingfield; public string SomeProperty{ get{ return backingfield; } set{ backingfield = value;} } // define another example property string anotherbackingfield; public string AnotherProperty{ get{ return anotherbackingfield; } set{ anotherbackingfield = value; } } // This is a predicate for SomeProperty public bool Predicate_4_SomeProperty(SomeObject comparison) { return comparison.SomeProperty == this.SomeProperty; } // This is a predicate for AnotherProperty public bool Predicate_4_AnotherProperty(SomeObject comparison) { return comparison.AnotherProperty == this.AnotherProperty; } } Note the predicate methods. They each define a condition that can be used for the Find, Exists, FindAll, Remove, etc. methods of a List<T> object. Predicate<T> is a generic delegate type that we can instantiate with our two predicate methods. Below we actually use these predicates. // Example code that uses our predicates List<SomeObject> someObjects = new List<SomeObject>(); // Add items with unique "SomeProperty" values. void AddToMyListIfSomePropertyIsUnique(SomeObject newItem) { if(someObjects.Exists( new Predicate<SomeObject>(newItem.Predicate_4_SomeProperty))) { someObjects.Add(newItem); } } // Add items with unique "AnotherProperty" values. void AddToMyListIfAnotherPropertyIsUnique(SomeObject newItem) { if(someObjects.Exists( new Predicate<SomeObject>(newItem.Predicate_4_AnotherProperty))) { someObjects.Add(newItem); } } Hope that helps. Again, reflection could come in handy here. You could write a class that can create a predicate for any property of any object using reflection.
-
My VS 2005 C# Express compiles the code and skips all errors...
snarfblam replied to EFileTahi-A's topic in General
It is probably not compiling at all. I'm not sure if it will be the same for VS (I'm running Express), but under Tools->Options->Projects and Solutions->Build and run, there are different options for what to do when there are compiler errors. -
Where I have next to no experience with winsock, I really wouldn't know a stupid question from a good one. Looking back at my post, I hope I didn't come off as offensive, and sorry I can't help more.
-
I specifically chose MDX over XNA because it is more mature and has more support. Currently my only issue is whether I should implement tile sheets by extracting each tile as a texture or by using fractional texture coordinates with a single texture. Unfortunately, both methods seem to have drawbacks (the former uses more resources and takes longer to initialize, the latter requires that I lock my vertices and modify them in order to change tile data). But thank you for the link. Looks like it will be a great resource when I get myself started with XNA.
-
You are looking to get a translation? It's not that difficult. Private Sub StartServer() Dim server As New TcpListener(IPAddress.Any, 1234) server.Start() Dim socket As Socket Dim chatSession As ChatSession While True ' blocks until a connection is established socket = server.AcceptSocket() ' when this fires the socket will be connected and the connection ' will actually be setup on a port other than 1234. this allows ' us to keep servicing future requests on port 1234. ' for long running processes, the key is to somehow pass the socket ' off to another thread. if you don't do this the server will ' not be able to establish new connections while the process is still ' running. chatSession = New ChatSession(socket) chatSessions.Add(chatSession) End While End Sub There is a sticky thread to help with C#->VB translation.
-
You don't need to create the structures. They are created simply by being declared, unlike classes. They aren't initialized, though, unless you call a constructor or an initialization method... Dim X As SomeStruct 'Not initialized Dim X As New SomeStruct(5, "Doodie") 'Initialized Dim Y As SomeStruct Y.Initialize("Ketchup") 'Initialized. You should probably create a constructor (Sub New). As far as sizing arrays, you MUST create an array. If you don't, well, then, believe it or not, it doesn't exists. So, what if you don't know the size? Then, most likely, you shouldn't be using arrays. Try using an ArrayList or a HashTable or a generic List(of T) class, or a Dictionary(Of Integer, String).
-
Fixed a big in Reflector.cs and then converted the class to VB. Note that I did very little testing on the VB version and it is very possible that a bug or two or ten were introduced in the translation.Reflector.zip
-
I simply used the focus method of the control I wanted to give focus to.
-
If you explain how and why these functions will be called it is much easier to give a satisfactory answer. As for not being able to get reflection to work, I don't know where you might have run into problems and you did not explain. Reflection might be the answer you are looking for, I merely suggested you look into other options as well. As for the example you requested, well, since I am such a nice guy, here is a bunch of code. Like I said, this isn't a good beginner project. I'm assuming that you are familiar with inheritance and interfaces (especially since you are writing a plug-in app), and that you know how to use the System.Activator class and the PropertyGrid control. Since we will be creating command-based plug-in, we would want to write a command-based-plug-in-interface as a base for command-based plug-ins. The commands will be invoked by a string (which specifies the name of the command) and if there are any arguments, they can be passed using a special class for arguments for that command. You will see what I mean by that in a moment. Here is our interface: Interface ICommandInterface Function GetCommands() As CommandInfo() 'Gets an array of CommandInfo objects Function SendCommand(command As String, args As Object) As Object End Interface We need to declare a class that can describe a command: Public Structure CommandInfo Public Name As String Public ArgType As System.Type Public Sub New(Name As String, ArgType As System.Type) Me.Name = Name Me.ArgType = ArgType End Sub End Structure Note the ArgType. This specifies the class that is used to pass arguments to a command. This is very similar to the way arguments are passed to event handlers: A click event receives an EventArgs object, and a MouseDown event receives a MouseEventArgs. A label edit in a list view receives a LabelEditEventArgs. Get it? You will see how it works soon. Now, pretend we are making an graphics editing application which supports graphic filter plug-ins. We can actually use our ICommandInterface as is because it is flexible enough that we don't need to add any methods--everything can be done through the SendCommand method. This is how a graphics filter plugin will work: the GetCommands function will return a list of commands that represent filters that the plug-in supports, and you call SendCommand to use a filter. Simple, no? Here is our plug-in code: Public Class BitmapFilterPlugIn Implements ICommandInterface Public Function GetCommands() As CommandInfo() Implements ICommandInterface.GetCommands 'We don't have any filters yet, so return an empty array Return New CommandInfo(0) {} End Function Public Function SendCommand(ByVal command As String, ByVal args As Object) As Object Implements ICommandInterface.SendCommand 'We don't have any filters yet, so do nothing and return nothing. Return Nothing End Function End Class [/vB] It doesn't look like much yet, but we will add filters. First, we need to look at those argument objects I was talking about. Since all filters will work on Bitmap objects, we will need to pass bitmaps to our filters. Let's define a base class for passing arguments to our filters. [code=visualbasic] Public Class FilterOptions Public TargetImage As Bitmap End Class Now, let's create a filter. We can do something simple, like Brightness. Generally you would pass a floating-point number to a brightness filter that represents the percentage of brightness the filter is to apply. Here is a class that we can pass to the Brightness filter: Public Class BrightnessOptions Inherits FilterOptions 'Create a property for a filter option Dim _Brightness As Single Public Property Brightness() As Single Get Return _Brightness End Get Set(ByVal value As Single) _Brightness = value End Set End Property End Class Now, let's incorporate that filter into our filter plug-in. Public Class BitmapFilterPlugIn Implements ICommandInterface Public Function GetCommands() As CommandInfo() Implements ICommandInterface.GetCommands 'Return an array that lists our commands and the arguments they require Return New CommandInfo() { _ New CommandInfo("Brightness", GetType(BrightnessOptions)) _ } End Function Public Function SendCommand(ByVal command As String, ByVal args As Object) As Object Implements ICommandInterface.SendCommand 'Perform the appropriate action based on the command string Select Case command.ToUpper() Case "BRIGHTNESS" Brighten(CType(args, BrightnessOptions).TargetImage, _ CType(args, BrightnessOptions).Brightness) End Select ' Most likely, none of our filters will need to return anything Return Nothing End Function Public Sub Brighten(ByVal image As Bitmap, ByVal brightness As Single) 'This function applies the filter End Sub End Class Now everything should come together. To use the plug-in, first you would call GetCommands(), which would return an array with one CommandInfo. That CommandInfo would tell you that there is a command named Brightness which requires a BrightnessOptions object. We will list Brightness in our plug-in menu. When the user selects the Brightness filter, we will use the System.Activator class to create a BrightnessOptions object. We will assign the image the user is editing to the BrightnessOptions object's TargetImage field, and then we will put the BrightnessOptions object in a property grid where the user can edit the Brightness property (or whatever options there are for other filters). You will most likely want to show the property grid on a dialog form. Finally, we call the SendCommand function, passing the string "Brightness" and our BrightnessOptions object. The plug-in calls the Brighten() function, which actually applies the filter, and that's it.
-
Triggering the enter button to do an action
snarfblam replied to mhrappstead's topic in Windows Forms
What have you tried? Are you handling the KeyDown event of the textbox? This seems to do the trick for me with both a multi-line and single-line textbox: Private Sub textbox2_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles textbox2.KeyDown If (e.KeyCode = Keys.Enter) Then MessageBox.Show(textbox2.Text) End If End Sub -
I can't reproduce the problem. I tried putting both in a group box, one or the other in the group box, both directly on the form, both in different group boxes, and it worked every time.
-
How are you loading the XML into the TextBox? And are you sure that the document you are loading is formatted (what is the source of the document)? You might want to consider using the XML classes that come with .Net to load and parse the XML so that you can output it formatted however you like.
-
I can convert my reflector class that MrPaul linked to to VB, if you would like. Using reflection can actually be a bit tricky. I recommend that you investigate other possible solutions, however, even if you ultimately resort to reflection. For one thing, reflection is much slower than using an interface. For another, certain things such as method overloading and differing accessor accessibility can throw off reflection code that might normally work. One suggestion would be that your plug-in interface implement some sort of command interface. For example, something along the lines of: Public Interface ICommandInterface Public Function GetCommands() As IEnumerable 'Or more specific or array type Public Function InvokeCommand(command As String, args As Object()) End Interface Public Interface IPlugIn Inherits ICommandInterface 'Declare your standard plugin interface members here End Interface The plug-in class would have to declare a method (GetCommands) that returns, say, a list of CommandInfo objects, each of which defines a command name and expected arguments, and it would have to declare a function (InvokeCommand) which executes a command. (This would be similar to the way commands are sent to the mciSendStringA() method of winmm.dll.) It might not be the best solution, and it would be difficult for beginners to implement, but it is really just an example of how it could be done without reflection.
-
Check it out (from http://msdn2.microsoft.com/en-us/library/008h021s(VS.80).aspx): Indicator Name: Error List Description: A shortcut connected to the corresponding task item in the Error List. I still don't get exactly what it means or how to get rid of it, but Microsoft seems to think that that doc explains it. Oh, and your sig was on my brother's booze calender.
-
Very nice picture. If you are using IE the attached image might show up as a link instead of the image itself (thats what it does in my IE), but IE is for losers. Cool people use Firefox. By the way, very nice picture.
-
F10, like alt, accesses a windows menu. Even if your window doesn't have a menu it probably has a system menu (the one that shows up when you click the icon)