
_SBradley_
Avatar/Signature-
Posts
29 -
Joined
-
Last visited
Content Type
Profiles
Forums
Blogs
Events
Articles
Resources
Downloads
Gallery
Everything posted by _SBradley_
-
Sorry, I thought that would be enough to describe the problem. Here's a "compilable snippet": using System; using System.Drawing; using System.Windows.Forms; class ListViewTest : Form { static void Main() { Application.Run(new ListViewTest()); } ListViewTest() { m_Grid.View = View.Details; m_Grid.Size = ClientSize; Controls.Add(m_Grid); PopulateGrid(); } private void PopulateGrid() { #if PAUSE_UPDATE m_Grid.BeginUpdate(); #endif m_Grid.Columns.Add("Col", 100, HorizontalAlignment.Left); foreach (string str in m_Data) { ListViewItem item = null; foreach (ListViewItem lvi in m_Grid.Items) { if (lvi.Text == str) { item = lvi; break; } } if (item == null) { item = m_Grid.Items.Add(str); } } #if PAUSE_UPDATE m_Grid.EndUpdate(); #endif } private string[] m_Data = {"one", "two", "one", "three"}; private readonly ListView m_Grid = new ListView(); } Compile this with the .NET Framework 1.x with or without PAUSE_UPDATE defined, and you get a list containing just "one, two, three". Compile it for the .NET Framework 2.0 without PAUSE_UPDATE defined, and you still get "one, two, three". But compile it for the .NET Framework 2.0 with PAUSE_UPDATE defined, and you get "one, two, one, three"! (Obviously, in the case of this snippet, a workaround would be to search the array for duplicates; but my actual app does more than this and doesn't use an array, so my only workaround right now is just to not use BeginUpdate/EndUpdate.)
-
I have some code that says something like: ListView lv = ...; lv.BeginUpdate(); foreach (piece of data) { if (an appropriate row already exists) item.SubItems.Add("blah"); else item = lv.Items.Add("blah"); } lv.EndUpdate();[/Code] This used to work as expected in .NET 1.0/1.1. However, since I installed 2.0 Beta 2, the 'if' part never runs; every iteration always inserts a new row, even if I've actually already inserted the specified row. Surely, BeginUpdate/EndUpdate should only affect what's displayed, not what actually gets inserted into the ListView? Is this a bug? Cheers. :)
-
Inheritance with a constructor that takes arguments problem
_SBradley_ replied to aewarnick's topic in Visual C# .NET
It's pretty much the same thing as [CS] class Inherit extends c1 { Inherit() { super("whatever"); } } [/CS] in Java. Only nicer. ;) -
Inheritance with a constructor that takes arguments problem
_SBradley_ replied to aewarnick's topic in Visual C# .NET
The "correct" way to do what you're after is this: [CS] class Inherit : c1 { Inherit() : base("whatever") { } } class c1 : UserControl { public c1() { } public c1(string argument) { // Initialize the class with the argument in this sub } } [/CS] -
Regular Expressions in VB My first ever attempt at VB. Hope it's OK! ;) Imports System.Text.RegularExpressions Imports System.Windows.Forms Public Module IDExtractor Sub Main Dim str As String str = "[3] DOE JOHN" Dim r As Regex r = New Regex("\[(?<id>([0-9]+))\]") Dim m As Match m = r.Match(str) If m.Success MessageBox.Show(m.Groups("id").Value, "ID") End If End Sub End Module
-
Regular Expressions I have to say, I would use a Regular Expression for this purpose. I've written a little example in C#, because I don't know VB; but converting back to VB should be easy enough for you. :) [CS] using System.Text.RegularExpressions; using System.Windows.Forms; class IDExtractor { static void Main() { string str = "[3] DOE JOHN"; Regex r = new Regex(@"\[(?<id>([0-9]+))\]"); Match m = r.Match(str); if (m.Success) MessageBox.Show(m.Groups["id"].Value, "ID"); } } [/CS]
-
[*]You misspelt "Length". [*]'i' is out of scope by the time you come to the second loop. I presume this was meant to be a nested loop. Since only the first statement is inside the first loop, you would need to enclose the two statements in a pair of braces. [*]C# is case sensitive: MessageBoxButtons has a member 'OK', but no member 'ok'. [/list=1] Also, your inner loop was counting from 1 to n - 1, so would have printed one too few asterisks. Here's an alternative, using a string constructor instead of a nested loop: [CS] using System; using System.Windows.Forms; class Histogram { static void Main() { int[] n = { 3, 4, 6, 8, 10, 9, 15 }; string output = "element\tsubscript\thistogram\r\n"; for (int i = 0; i < n.Length; i++) output += i + "\t" + n + "\t" + new string('*', n) + "\r\n"; MessageBox.Show(output, "histogram", MessageBoxButtons.OK, MessageBoxIcon.Information); } } [/CS] Hope you like that. :cool:
-
If I run the following code on a 16-colour bitmap, the changes to the palette are not included in the saved image. However, the image displayed in the form is as I expected (black & white). Am I doing something wrong, or is this a bug? [CS] using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; class Test { static void Main(string[] args) { Bitmap bmp = new Bitmap(args[0]); ColorPalette pal = bmp.Palette; for (int i = 0; i < pal.Entries.Length; ++i) pal.Entries = i == 0 ? Color.Black : Color.White; bmp.Palette = pal; bmp.Save("new.bmp"); new ImgForm(bmp).ShowDialog(); } } class ImgForm : Form { public ImgForm(Bitmap bmp) { m_Bitmap = bmp; } protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.DrawImage(m_Bitmap, ClientRectangle); } private Bitmap m_Bitmap; } [/CS]
-
TrayIcon is now called NotifyIcon. There is actually a subtleish difference between the ShowInTaskbar and Visible methods. If you set Visible to false, then, when you Alt+Tab between applications, your application will not be in the list. With the ShowInTaskbar method, your application will still be there (although it turns out to be a "blank window" icon -- this seems like a bug...?). I didn't have a problem with either method, though: you just have to make sure to reset the rest of the state (make it visible or whatever) before resizing the form.
-
Your method is called ror(), which I presume stands for "rotate right", but your code shifts left -- although the highbit part looks good for a right-shift method. It's a little confused... ;) I had a quick go, and came up with this pair: [CS] class Rotate { public static byte RotateLeft(byte val) { byte lowbit = (byte) ((val & 0x80) != 0 ? 0x1 : 0x0); val <<= 1; val |= lowbit; return val; } public static byte RotateRight(byte val) { byte highbit = (byte) ((val & 0x1) != 0 ? 0x80 : 0x0); val >>= 1; val |= highbit; return val; } } [/CS]
-
What sort of errors? GetType() should never cause any errors. And it cannot be overriden, because it is not virtual in Object.
-
'watcher' is an instance member of your StartUp class. However, you are trying to reference it from Main(), which is a static method. Since a static method does not have an implicit 'this' reference, you cannot reference instance members, only static members. You have three choices: 1. Declare and initialize 'watcher' inside your Main() method, instead of making it a member. 2. Make 'watcher' a static member of your StartUp class (just declare it "static FileWatch watcher"). 3. Create an instance of class StartUp and use that, as in "new StartUp().watcher.StartWàtch()".
-
Instead of using typeof(Second), just call obj.GetType(). :)
-
There's only one instance of the DLL. You'll have to solve this problem in code, either or the client side or on the server side. (I can't really say which would be best without further details.)
-
It's very easy with .NET. [*]Build your DLL using the /t:library command-line switch. [*]In your application code, add any using directives for namespaces defined in that DLL, if you fancy, and use the classes and methods in which you're interested. [*]Build your EXE using the /r:MyDll.dll command-line switch. [/list=1] Voilà! (I don't know what magic you must perform in order to do this via your IDEs.)
-
C# [*]Embed the icon file as a resource in the application executable, using the appropriate command-line switch: /res:Blah.ico [*]Set the form's icon, something like this: Icon = new Icon(GetType().Assembly.GetManifestResourceStream("Blah.ico")); [/list=1] Voilà!
-
I would always handle a form event by way of overriding the event method (and calling the base class's method). You are the form, so it's only right that your event method gets called by the framework directly. If you do this by way of attaching an event handler, it's as though you're an outsider who just happens to be interested in one of the form's events. (This would be like, for example, when you attach an event handler to a Button member's Click event. You are not the button itself; you are just interested in when something happens to the button.) If you look at the documentation for any form event method, you'll see the following text: The OnBlah method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.
-
Ah, yes. This is a good one. A more compact example of what you're experiencing is shown below: [CS] class Test : System.MarshalByRefObject { int c = 5; static void Main() { Test t = new Test(); t.c.ToString(); t.f(ref t.c); } void f(ref int i) { } } [/CS] The problem is that, when you call Int32.ToString(), the first parameter (the hidden 'this' parameter) is passed in the same way as a "ref int" parameter would be passed. (Compare this to the call to f(ref int).) The generated MSIL is the same for both Int32.ToString() parameter 0 and Test.f() parameter 1: they use 'ldflda'. What I'm saying is that, so far as the compiler's concerned, when you do the "t.c.ToString()", the first parameter is essentially a "ref int" parameter. Since your class is derived from MarshalByRefObject (Form is ultimately derived from this class), passing this int member by-ref is illegal. If you were to comment out the derivation from MarshalByRefObject in the example above, the code would compile just fine.
-
That looks pretty good. (You said to ignore errors in the code, so I won't mention the fact that you can't put access modifiers on destructors. ;)) As wyrd said previously, the advantage of this technique is that, say you had 'class Level1Boss : GenericMonster' and 'class Level2Boss : GenericMonster', which used two separate groups of images, you wouldn't have to keep all of Level1Boss' images around whilst level 2 was being played. :) Come to think of it... If GenericMonster were your base class, and the Image/Bitmap references were to be static members of the monster classes, then you'd need something along the lines of: protected abstract Image[] GetImages(); Otherwise, there is no (nice) way for GenericMonster to access the static members in the derived classes.
-
OK. Sounds like a plan. :) Yes, that gives you more control over when the image will be released, compared to our hypothetical static destructor, in that you can release the resource any time during the running of the program, rather than just when the class is unloaded (or the AppDomain goes away). Good luck! ;)
-
Well, OK, but that's a different concern from the one originally voiced. If you're concerned about the memory usage of your several static members while the program is running (and not just about releasing those resources when the class goes away), then the first solution that comes to mind is this: Reference the resource via a static WeakReference member. Implement a private GetImage() method or whatever. If the resource has been collected by the GC, then you'll have to recreate it. If it's still hanging around, then fine: the system obviously didn't require that memory just yet.
-
Yes, you can have a static constructor. No, you can't have a static destructor. Is this static image member a managed resource? Your static destructor, if you could have such a thing, would, I guess, be called just before the class is unloaded -- which, I guess, would be just before the AppDomain is unloaded. You could spend time thinking of a very clever solution to the problem you described above. However, if the answer to my "managed resource" question is yes, then you could just as easily say, "Who cares? The AppDomain is being unloaded anyway. Everything must go!" The image won't be around much longer, no matter what happens. Therefore, there is little point in you worrying about disposing of your image. If it is an unmanaged resource, then, yes, you have a problem, because you need to clean up that image resource before the class/AppDomain is unloaded. In this case, I think you'd either have to do something with the AppDomain.DomainUnload event or write some sort of wrapper class for your unmanaged resource, with a Dispose() and a destructor, so that the resource would be released when the finalizer is run (just once, since it'd be a static reference to a single wrapper class instance).
-
Two things: 1. You want to use the bitwise & operator, not the logical && operator. 2. Math.Pow() returns a double, so you'll have to cast the result to long to do the ANDing. Like this: public void abilita(long mint) { for (int i = 0; i < toolbarMain.Buttons.Count; i++) { toolbarMain.Buttons[i].Enabled = (mint & (long) Math.Pow(2, i)) == (long) Math.Pow(2, i); } } Having said that, I wouldn't use Pow() in this situation. It gets very long-winded, looking at that; and all those method calls can't be good. I'd have thought something like this would be a good solution: public void abilita(long mint) { for (int i = 0; i < toolbarMain.Buttons.Count; i++) toolbarMain.Buttons[i].Enabled = (mint & 1 << i) != 0; }
-
Probably no one was quite sure what you were asking. ;) Do you just mean that you want to write a function to make some GDI+ method calls? Maybe you want a generic function that you can call both from OnPaint() and from elsewhere? If so, then of course. You'll need some way for that method to get ahold of a Graphics object -- the easiest way is to pass it as a parameter. For example: private void Draw(Graphics g, int x1, int y1, int x2, int y2) { g.DrawLine(Pens.Red, x1, y1, x2, y2); } protected override void OnPaint(PaintEventArgs e) { Draw(e.Graphics, 50, 50, 100, 150); } If you're after something a bit more complicated, then you'll have to do a bit more explaining first. ;)
-
Which classes are automatically passed by ref and which are not?
_SBradley_ replied to aewarnick's topic in General
No. I would suggest that you only use ref when you really need the argument to be passed by reference. That is, you need that the called method should be able to alter the value of the parameter itself.