Jump to content
Xtreme .Net Talk

rbulph

Avatar/Signature
  • Posts

    398
  • Joined

  • Last visited

Everything posted by rbulph

  1. Shouldn't interface objects have the basic methods of an object, like GetType, Equals, etc.? For instance, in the following code I have to had quite a bit extra to tell if the two objects are of the same class. Maybe the answer is that I should be using inheritance instead of an interface here, but still, logically it seems surprising that you can't use all the basic methods of an object on an object referenced like this. Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim p As INameObject p = New class1 Debug.Print(TypeName(p)) Dim c As INameObject c = New class2 Debug.Print(c.FnGetType Is p.FnGetType) ' Debug.Print(c.gettype Is p.gettype) 'Can't say this. End Sub End Class Public Interface INameObject Property Name() As String Function FnGetType() As Type End Interface Public Class class1 Implements INameObject Private pName As String Public Property Name() As String Implements INameObject.Name Get Return pName End Get Set(ByVal value As String) pName = value End Set End Property Public Function FnGetType() As System.Type Implements INameObject.FnGetType Return Me.GetType End Function End Class Public Class class2 Implements INameObject Private pName As String Public Property Name() As String Implements INameObject.Name Get Return pName End Get Set(ByVal value As String) pName = value End Set End Property Public Function FnGetType() As System.Type Implements INameObject.FnGetType Return Me.GetType End Function End Class
  2. Am I right in thinking that the e as System.EventArgs provided in most windows events never contains any useful information? Classes that derive from it, like System.Windows.Forms.MouseEventArgs, of course do provide useful information, but this seems to provide nothing.
  3. The Operator "=" is not defined for type "Object" and type "Object" The following code doesn't work: Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim a As New Object Dim b As New Object a = b Dim c As New Object Select Case a Case b Text = "a is b" Case c Text = "a is c" End Select End Sub End Class because "The Operator "=" is not defined for type "Object" and type "Object"". This seems like a missed opportunity to provide a useful capability in the language. There's no way around this is there, apart from doing "If a is b then ... elseif a is c then..."?
  4. I've just seen the above error message for the first time. Why can't they? Is there some logical reason for this?
  5. This is easy once you know how. Define your property with the attribute as follows: <Editor(GetType(CustomEditor)> Public Property MyStringProp as String... Then define your editor class: Public Class CustomEditor Inherits System.Drawing.Design.UITypeEditor Public Overrides Function GetEditStyle(ByVal context As System.ComponentModel.ITypeDescriptorContext) As System.Drawing.Design.UITypeEditorEditStyle Return Drawing.Design.UITypeEditorEditStyle.DropDown End Function Public Overrides Function EditValue(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal provider As System.IServiceProvider, ByVal value As Object) As Object Dim ui As New System.Windows.Forms.CheckedListBox ui.Items.Add("ABC") ui.Items.Add("DEF") ui.Items.Add("GHI") ui.CheckOnClick = True ui.BorderStyle = Windows.Forms.BorderStyle.None Dim I As MyClass = context.Instance If I.MyStringProp <> "" Then Dim f() As String = I.MyStringProp.Split(",") For Each s As String In f If ui.Items.IndexOf(s) <> -1 Then ui.SetItemChecked(ui.Items.IndexOf(s), True) Next End If Dim fr As System.Windows.Forms.Design.IWindowsFormsEditorService = provider.GetService(GetType(System.Windows.Forms.Design.IWindowsFormsEditorService)) fr.DropDownControl(ui) Dim s2 As String For Each s3 As String In ui.CheckedItems s2 = s2 & s3 & "," Next If Right$(s2, 1) = "," Then s2 = Left$(s2, Len(s2) - 1) ui.Dispose() Return MyBase.EditValue(context, provider, s2) End Function End Class Obviously this code can be tidied up a little. Query whether I'm getting hold of the SelectedObject in the right way in using context.Instance. value always seems to be Nothing so I can't use that.
  6. Thanks. I see. I'm actually just doing this for an MDI form, so all the other forms which I'm concerned about are child forms of that. If I simply set the MDI form's cursor while the processing is carried out, that cursor is shown while it is over any part of the form or a child form. So it works OK without much bother. But it's good to understand what UseWaitCursor does.
  7. Thanks, I'll look into that. I wonder why the poster appears to have been banned? I hope there's nothing malicious in the code.
  8. So you would be agreeing with me that UseWaitCursor is useless then?
  9. What is UseWaitCursor all about? The help file says only "When this property is set to true, the UseWaitCursor property of all open forms in the application will be set to true." That doesn't actually tell you what the effect of setting the property will be, simply that when you set it for one form, it will be set the same for all other forms. Who on earth writes this stuff? The property grid says "When this property is set to true, the Cursor property of the control and its child controls is set to WaitCursor." This is untrue. Try the following code in a form with a button: Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.UseWaitCursor = True System.Windows.Forms.Application.DoEvents() 'doesn't help. Dim h As Long = Now.Second Dim i As Long = Now.Millisecond Dim n As Long Dim m As Long For n = 1 To 100000 For m = 1 To 2000 Next Next Text = (Now.Second - h) * 1000 + Now.Millisecond - i Me.UseWaitCursor = False End Sub End Class The cursor does not change while the loops are being cycled through. So is the UseWaitCursor property useless?
  10. This article says to use the StringConverter to provide for a drop down list for a property in the property grid. Two questions: 1. Why does it need to be the StringConverter? The TypeConverter class seems to do the job equally well, and 2. Is it possible to make the drop down list multi-select?
  11. Yes, except I can't do that here because the CategoryAttribute class is part of System.ComponentModel and is not something that I created.
  12. Thanks. What I'm trying to achieve is a CategoryAttribute that can only have certain string values for the category. Doing that allows me to select from a choice of categories (through the Intellisense) when I create a new property, rather than having to type in the text of the category each time (and risk getting it slightly different, and so inadvertently creating a new category). The difficulty is that there isn't anywhere to set properties of the CategoryAttribute other than through the constructor since you have to create it as part of the declaration of the property. There is no "room" to follow either of your suggestions, marble-eater. As I say, I have managed to do this to my satisfaction, and I've used code as follows: <CustomCategory(CatTypes.WeatherDetails)> Public Property Get Rainfall as decimal 'Get, Set etc. End Property <AttributeUsage(AttributeTargets.Property, AllowMultiple:=False)> Public Class CustomCategory Inherits CategoryAttribute Public Sub New(ByVal c As CatType) MyBase.New(CatDesc(c)) End Sub End Class Public Module Module1 Public Enum CatTypes WeatherDetails TaxDetails CityDetails End Enum Public Function CatDesc(ByVal ct As CatTypes) As String Select Case ct Case CatTypes.WeatherDetails Return "Weather Info" Case CatTypes.TaxDetails Return "Tax" Case CatTypes.CityDetails Return "Cities" End Select End Function End Module P.S. how do you get code to come out in colours as in marble-eater's post? I'm using the code tags, and you can see the result isn't as good.
  13. The IDE tells me that, in the constructor of an inheriting class, calls to MyBase.New must be the first line. This leads to me writing code like: Inherits System.ComponentModel.CategoryAttribute Public Sub New(ByVal x as long) MyBase.New(IIf(x=1, "One", "Two")) End Sub With more complicated conditions you can imagine that the IIfs are going to get ridiculous. Why will it not just let me write: Inherits System.ComponentModel.CategoryAttribute Public Sub New(ByVal x as long) Dim s as string If x= 1 then s="One" Else s="Two" End If MyBase.New(s) End Sub And is there a solution to the problem? Edit: OK, I see that I can call a function in the parameter of MyBase.New, and as long as this function isn't in the inheriting class, it will work OK. So I can manage. Still, any comments welcome.
  14. Thanks, helpful replies. This is a lot better than VB6 where an unhandled exception would just cause the application to close.
  15. Thanks. I note that in the IDE, when you throw an exception, execution stops, the code is shown outlined in yellow and a box is shown with a blue banner and description of the error. But in the release exe, the user is shown a different dialogue box with the option to continue or quit. Furthermore, when you throw an exception in an override of the ConvertFrom method of the ExpandableObjectConverter (which is what I'm working with for the propertygrid) you get different behaviour. In the IDE you get the same result as described above, but you then get a message in the application stating "Property value is not valid" with the parameter to the exception you have thrown in the dropdown of this message. You get this message in the exe as well. I'm not quite sure what the mechanism is for it to work differently in this method, but it works well, so I can't really complain. However it would be quite good if you could get the release exe style behaviour when debugging so that you can easily see what the user will see.
  16. What does the "Throws" statement do? The VB help file is typically unhelpful saying: "The Throw statement throws an exception that you can handle with structured exception-handling code (Try...Catch...Finally) or unstructured exception-handling code (On Error GoTo). You can use the Throw statement to trap errors within your code because Visual Basic moves up the call stack until it finds the appropriate exception-handling code." OK, so I know where the "Throws" statement is used, but there is no explanation as to what throwing an exception is or does. What are the practical consequences of throwing an exception? What does it mean?
  17. Three questions about building projects which I haven't been able to find the answers to: 1. What's the difference between Build and Rebuild? 2. What does "Clean" do? 3. What's the reason for having a "bin" and an "obj" file within a project folder? They seem rather duplicative. Thanks.
  18. Thanks. If I have been working on some sample code in a small project, as I often do if there's a particular problem which I want to figure out, and then want to put parts of that code into my main project, it seems that adding files temporarily as a link is a good way to do this. I can then copy and paste the code from there. I suppose I could add the whole project to the solution, but this is easier to navigate. Anyway I agree, adding links is not a very good thing to do long term.
  19. OK, thanks. I understand. But how do you add an item as a link? I go Project/Add Item and I'm not given any option to add the item as a link, so I can't see this. I'm using Visual Studio 2005.
  20. If you have an item, a module or a class say, which exists in one project, and you add it to another project, how can you then remove it from the second project? If you right click on it you can exclude it from the project, which results in it still being shown, but faded out, or you can delete it, which means it will be lost from the original project. I just want to go back to the situation where it wasn't part of the second project at all. How do I do that?
  21. Oh yes you can: Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click Dim v As String With Me.prgOne If Not .SelectedGridItem Is Nothing Then 'There's a parent GridItem at the top of the hierarchy, which is not shown. Find that. Dim f As GridItem = .SelectedGridItem Do Until f.GridItemType = GridItemType.Root f = f.Parent Loop For Each h As GridItem In f.GridItems GridItemToString(v, h) If h.GridItemType = GridItemType.Category Then For Each h2 As GridItem In h.GridItems GridItemToString(v, h2) Next v = v & vbCrLf End If Next Clipboard.SetText(v) Else 'only ever seems to be the case if the SelectedObject is Nothing, so not a problem. End If End With End Sub Private Sub GridItemToString(ByRef v As String, ByVal gi As GridItem) With gi v = v & .Label & vbTab If Not .Value Is Nothing Then v = v & .Value.ToString v = v & vbCrLf End With End Sub
  22. OK, that's a helpful tip, thanks. The search does now find this thread.
  23. Microsoft Visual Studio 2005 Version 8.0.50727.42 Visual Basic From the help library: "Show Derived Types Toggles display of derived types in the Objects pane. Available only for Visual C#." Perhaps they mean "Works only" rather than "Available" since for Visual Basic the menu option is there and not disabled but doesn't seem to work. It would be a shame if you can't do this because to be able to see examples of anything that inherits from a class could be useful in forming an understanding of that class.
×
×
  • Create New...