Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...

ljw1004 posted:

You can use lambdas as event handlers! This is how you remove them:

code:
Dim x As EventHandler =
    Sub()
        RemoveHandler c.event, x
        ' do other stuff
    End Sub

AddHandler c.event, x
or in C#, where you have to initialize it to NULL first:

code:
EventHandler x = null; x = delegate {
  c.event -= x;
  // do other stuff
};
c.event += x;

I thought that this was a very clever construction when I discovered it, and I still use it. But it has the major flaw that if the event never fires, the event handler doesn't get removed, and then you're leaking instances all over the place. Event handlers root objects. It's one of the few ways you can leak memory in C#. I have gotten to learn alllll about this the last couple weeks.

You have to hold a reference to that handler somewhere so you can try to remove it yourself later.

I think this isn't a problem if your object is subscribing to its own event, though. In that case, you can just implement Dispose to null out your events.

Unless there's a much simpler way to handle this problem which I would love to hear.

Adbot
ADBOT LOVES YOU

chglcu
May 17, 2007

I'm so bored with the USA.

Ryouga Inverse posted:

I thought that this was a very clever construction when I discovered it, and I still use it. But it has the major flaw that if the event never fires, the event handler doesn't get removed, and then you're leaking instances all over the place. Event handlers root objects. It's one of the few ways you can leak memory in C#. I have gotten to learn alllll about this the last couple weeks.

You have to hold a reference to that handler somewhere so you can try to remove it yourself later.
The problem here is actually when you register a handler from one object with an event in another object that is otherwise rooted. The object containing the event will then root the object containing the handler through the event handler. The objects will still be collected if they are the only things that reference each other.

Ryouga Inverse posted:

I think this isn't a problem if your object is subscribing to its own event, though. In that case, you can just implement Dispose to null out your events.
This is not necessary due to the above. The only thing rooting the handler in this case is the object that contains it, which the garbage collector can handle.

Ryouga Inverse posted:

Unless there's a much simpler way to handle this problem which I would love to hear.
One way of dealing with this is using some variety of the weak event pattern.

chglcu fucked around with this message at 10:26 on Jul 27, 2011

glompix
Jan 19, 2004

propane grill-pilled
Kind of a simple, dumb question, but is there anything out there that can "figure out" and set the correct tab order on a WinForms form?

I assume it can be done if you make some assumptions about layout and how control trees should look. I assume it can't be done for our insanely messy forms, but it'd still be cool to know if anyone has come across anything.

TJChap2840
Sep 24, 2009
EDIT: I am using C# for this project.

Not sure if this is the right place. Point me in the right direction if it is not.

I'm trying to recolor a series of images based on the color a user picks. My problem is, all of my images have a white background. Is there some way to remove this white background or make it transparent?

I have created the images in my code as a bitmap and then recolored the pixels using for loops. I have based my code off of some source code I have used for reference. My code is (obviously) very similar to the reference (minus the extraneous parts) but I am still getting a recolored square instead of just a recolored head, shirt, pants, etc.

I'd also like to be able to remove the unnecessary white portions of the images to make my GUI look better.

_aaron
Jul 24, 2007
The underscore is silent.

TJChap2840 posted:

EDIT: I am using C# for this project.

Not sure if this is the right place. Point me in the right direction if it is not.

I'm trying to recolor a series of images based on the color a user picks. My problem is, all of my images have a white background. Is there some way to remove this white background or make it transparent?

I have created the images in my code as a bitmap and then recolored the pixels using for loops. I have based my code off of some source code I have used for reference. My code is (obviously) very similar to the reference (minus the extraneous parts) but I am still getting a recolored square instead of just a recolored head, shirt, pants, etc.

I'd also like to be able to remove the unnecessary white portions of the images to make my GUI look better.
It'd probably be helpful to see the code, and you might be better off asking in the .Net Megathread (though I imagine there's quite a bit of cross-readership between that thread and this one).

_aaron fucked around with this message at 01:12 on Jul 28, 2011

TJChap2840
Sep 24, 2009

_aaron posted:

It'd probably be helpful to see the code, and you might be better off asking in the .Net Megathread (though I imagine there's quite a bit of cross-readership between that thread and this one).

I was more asking if there was a known simple way. My way is technically correct, but because of the white background in my images, the result isn't correct.

and, if I am not mistaken, you linked me to thread that we are in?

TJChap2840 fucked around with this message at 21:04 on Jul 27, 2011

weapey
Jun 11, 2003

stomp stomp stomp

_aaron posted:

.Net Megathread

Cool, never knew this thread existed.

_aaron
Jul 24, 2007
The underscore is silent.

TJChap2840 posted:

I was more asking if there was a known simple way. My way is technically correct, but because of the white background in my images, the result isn't correct.

and, if I am not mistaken, you linked me to thread that we are in?
Haha, gently caress. I had the general programming thread open in a different tab and lost track. I'm dumb.

Zhentar
Sep 28, 2003

Brilliant Master Genius

TJChap2840 posted:

I was more asking if there was a known simple way. My way is technically correct, but because of the white background in my images, the result isn't correct.

You look at each pixel, see if it's white, if is, set the alpha for that pixel to the max.

ljw1004
Jan 18, 2005

rum

TJChap2840 posted:

I'm trying to recolor a series of images based on the color a user picks. My problem is, all of my images have a white background. Is there some way to remove this white background or make it transparent?
I have created the images in my code as a bitmap and then recolored the pixels using for loops.

I am still getting a recolored square instead of just a recolored head, shirt, pants, etc.

(1) You should remove the white backgrounds BEFORE the image goes into your software. Load up Photoshop, use its tools to make the white things transparent, and save as a PNG with transparency. Then your software can load them and have Images which already have their alpha values (i.e. transparency values) correct for each pixel.


(2) As for your code, well, we don't have a clue how you're doing the recoloring or what exactly is your specification for how the recoloring behaves.

It's likely that you shouldn't be using a loop. Instead you should find a way that uses calls that can potentially just ask the graphics accelerator card to do the recoloring.

In .NET, it looks like you can maybe use ColorMatrix to recolor your image. See if that can express the kind of recoloring you want:

http://msdn.microsoft.com/en-us/library/7fkd49hh.aspx
http://msdn.microsoft.com/en-us/library/6tf7sa87.aspx

TJChap2840
Sep 24, 2009

ljw1004 posted:

(1) You should remove the white backgrounds BEFORE the image goes into your software. Load up Photoshop, use its tools to make the white things transparent, and save as a PNG with transparency. Then your software can load them and have Images which already have their alpha values (i.e. transparency values) correct for each pixel.


(2) As for your code, well, we don't have a clue how you're doing the recoloring or what exactly is your specification for how the recoloring behaves.

It's likely that you shouldn't be using a loop. Instead you should find a way that uses calls that can potentially just ask the graphics accelerator card to do the recoloring.

In .NET, it looks like you can maybe use ColorMatrix to recolor your image. See if that can express the kind of recoloring you want:

http://msdn.microsoft.com/en-us/library/7fkd49hh.aspx
http://msdn.microsoft.com/en-us/library/6tf7sa87.aspx

Thanks for the help. I think I have the issue solved. I'm going to look into into drawing the images using Graphics instead of trying to mess around with PictureBoxes tomorrow

As for code:
code:
for (int x = 0; x < hair.Width; x++)
{
                for (int y = 0; y < hair.Height; y++)
                {
                    if (hair.GetPixel(x, y) != System.Drawing.Color.FromArgb(255,255,255))
                    {
                        System.Drawing.Color pixel = hair.GetPixel(x, y);
                        hair.SetPixel(x, y, System.Drawing.Color.FromArgb((pixel.R + hairClrBox.BackColor.R) / 2, (pixel.G + hairClrBox.BackColor.G) / 2, (pixel.B + hairClrBox.BackColor.B) / 2));
                    }

                    if (hair.GetPixel(x, y) == System.Drawing.Color.FromArgb(255, 255, 255))
                    {
                        hair.SetPixel(x, y, System.Drawing.Color.Transparent);
                    }
                 
                }
}

mobby_6kl
Aug 9, 2009

by Fluffdaddy
What's the fastest way to get a bunch of data from Excel into a SQL Server table with C# and ADO.net (or something else)? Doing it row by row, even within a transaction and using prepared statements, is slow as hell. Like, several minutes for a few thousand rows, even if they contain just one or two int columns. The MS import/export tool from their toolkit does the same thing in seconds, but it would be much more convenient to do it directly from my app for everyone involved.

Also, the reverse would be great too - the Office interop method I'm using to export data into excel and format it appropriately is slooooow.

Dietrich
Sep 11, 2001

mobby_6kl posted:

What's the fastest way to get a bunch of data from Excel into a SQL Server table with C# and ADO.net (or something else)? Doing it row by row, even within a transaction and using prepared statements, is slow as hell. Like, several minutes for a few thousand rows, even if they contain just one or two int columns. The MS import/export tool from their toolkit does the same thing in seconds, but it would be much more convenient to do it directly from my app for everyone involved.

Also, the reverse would be great too - the Office interop method I'm using to export data into excel and format it appropriately is slooooow.

Use a DTS package, SSIS can do stuff like that on a timed or triggered basis.

Edit: I should add that when you do an import though management studio, it creates a one-off DTS package to perform the import. SSIS is quite good at reading data out of excel files. No need to reinvent the wheel.

Dietrich fucked around with this message at 15:37 on Jul 28, 2011

Nurbs
Aug 31, 2001

Three fries short of a happy meal...Whacko!

mobby_6kl posted:

What's the fastest way to get a bunch of data from Excel into a SQL Server table with C# and ADO.net (or something else)? Doing it row by row, even within a transaction and using prepared statements, is slow as hell. Like, several minutes for a few thousand rows, even if they contain just one or two int columns. The MS import/export tool from their toolkit does the same thing in seconds, but it would be much more convenient to do it directly from my app for everyone involved.

Also, the reverse would be great too - the Office interop method I'm using to export data into excel and format it appropriately is slooooow.

Look into the SqlBulkCopy class, it's designed exactly for this sort of thing.

wwb
Aug 17, 2004

The above advices are good and probably the right answer. That said, the troubleshooting part of my guesses that the issue is on the read side -- how are you pulling data from excel?

Zhentar
Sep 28, 2003

Brilliant Master Genius
From my testing in the past, the fastest way to get data into Excel is by putting tab delimited data onto the clipboard and pasting it in. Unfortunately, it's also a fragile method that can easily fail silently with large amounts of data.

PhonyMcRingRing
Jun 6, 2002
Be wary that SQL Management Studio can do a really lousy job of figuring out what data type to use for the sql columns. It only scans the first x amount of rows(instead of, I dunno, using the column cell type from the Excel file) and then picks out a data type. This annoys the poo poo out of me when I import data at work and it decides that zip codes should be floats, or that a column with mixed alphanumeric characters should be an int because the rows it scanned happened to all be convertible to int.

Edit: Also the one time I wanted it to throw an error, it didn't. I think this was on SQL 2000, but we had to import a bunch of contact info from an Excel 2003 file. The import wizard decided "Hey! Phone numbers! Those should be int values!" and then proceeded to null out any cell where the phone number(as an integer) would be greater than 2,147,483,647.

PhonyMcRingRing fucked around with this message at 03:44 on Jul 29, 2011

mobby_6kl
Aug 9, 2009

by Fluffdaddy
^^^
Yeah, I noticed that too, but thankfully you can specify the types yourself with a bit more effort.

wwb posted:

The above advices are good and probably the right answer. That said, the troubleshooting part of my guesses that the issue is on the read side -- how are you pulling data from excel?

Actually, the data is already in memory by the time I try to upload it, I don't recall right now (or have access to the code) but it's either in a list/array or a datatable. Actually reading with Jet db is pretty fast anyway. I'll try the stuff suggested here and see how it works.

As for exporting to excel, I suppose I could directly write a CSV, but then all the formatting would have to be done manually or with macros :(

weapey
Jun 11, 2003

stomp stomp stomp
If I recall, while working with Excel in Office macros, accessing the cells is like accessing a 2-dimensional array. It should be relatively trivial to have 2 nested for loops in your code to loop your in-memory dataset and dump it into an in-memory Excel spreadsheet, then just save it to disk after doing whatever formatting you wish to it.

genki
Nov 12, 2003
Open xml is the easiest way to work with excel spreadsheets programmatically, afaik.

I believe it only handles 2007 and up (xlsx format), but if that's an acceptable limitation, the ease of working with cells/formatting/etc is well worth it.

http://blogs.msdn.com/b/erikaehrli/archive/2008/06/10/announcing-the-open-xml-format-sdk-1-0.aspx

wwb
Aug 17, 2004

^^^yup. Another option if you need pre 2007 compatibility is spreadsheet XML, look up CarlosAgExcelXmlWriter for that.

Scaramouche
Mar 26, 2001

SPACE FACE! SPACE FACE!

This is an embarrassingly dumb question that you guys are going to laugh at me for but here goes:

Visual Studio 2005 working on an auto-converted old project.
I'm make a new EDI document translator (210) and I've got some great sample code that I've tweaked all the logic for perfectly (as opposed to writing the code from scratch like I did for 810/850/856 etc.). The problem? The original project is so old, that it doesn't include <formname>.designer.vb, which means I can't launch Design mode.

Is there a way to automate generation of the designer.vb file? I notice there's a giant swack of code in the 'Windows Form Designer generated code' region; do I just have to move all that into a 'fake' designer.vb that I make myself? Sorry for the basic nature of the question, but I've never really 'got' how the design side fits together (I normally write data transformation/non-interactive services).

Factor Mystic
Mar 20, 2006

Baby's First Post-Apocalyptic Fiction
Why not just connect to the Excel spreadsheet directly via a oledb connection? Then select the data out into a data table, and feed that to your sqlbulkcopy object.

IMJack
Apr 16, 2003

Royalty is a continuous ripping and tearing motion.


Fun Shoe
So I have a VC# project that I'm building on a Win 7 64-bit machine. This is the login screen I expect, and that I'm getting on this primary machine.



Note how the Server list box on top has two visible lines; the Domain and Project list are the same way.

When I install the published app on an XP machine, this is what I see:



The list boxes have collapsed to one visible line each. This makes choosing servers, domains, and projects a pain in the rear end. What could be causing this?

genki
Nov 12, 2003

IMJack posted:

So I have a VC# project that I'm building on a Win 7 64-bit machine. This is the login screen I expect, and that I'm getting on this primary machine.



Note how the Server list box on top has two visible lines; the Domain and Project list are the same way.

When I install the published app on an XP machine, this is what I see:



The list boxes have collapsed to one visible line each. This makes choosing servers, domains, and projects a pain in the rear end. What could be causing this?
I think the more important question is, how is it that your list boxes are two visible lines high? I find that a bizarre design choice.

Anyhow, I would highly suspect this has something to do with application styles. I believe there should be something in your program main before the form is launched where you can control whether windows styles should be used in your application. I'd try hardcoding your app to a certain (backwards compatible) style and see if that fixes the discrepancies between OSes (first is to ensure the form elements look the same on both OSes, next is to get them to look the way you want them to look).

biznatchio
Mar 31, 2001


Buglord

IMJack posted:

The list boxes have collapsed to one visible line each. This makes choosing servers, domains, and projects a pain in the rear end. What could be causing this?

Your Windows 7 system appears to be using large fonts, and your Windows XP system is not. Windows Forms, by default, will automatically attempt to change the location and size of controls when the system font size is different; but because Windows prefers crisp scaling instead of smooth scaling, so the metrics change in surprising ways as a result.

So what it appears to be happening is that Windows Forms is shrinking the listbox to account for the smaller system font size, but it's shrinking it enough that the IntegralHeight property of the ListBox is forcing it to collapse down to one line.

epswing
Nov 4, 2003

Soiled Meat
Can any gurus here comment on http://northhorizon.net/2011/the-right-way-to-do-inotifypropertychanged/ ?

Sprawl
Nov 21, 2005


I'm a huge retarded sperglord who can't spell, but Starfleet Dental would still take me and I love them for it!

IMJack posted:

So I have a VC# project that I'm building on a Win 7 64-bit machine. This is the login screen I expect, and that I'm getting on this primary machine.



Note how the Server list box on top has two visible lines; the Domain and Project list are the same way.

When I install the published app on an XP machine, this is what I see:



The list boxes have collapsed to one visible line each. This makes choosing servers, domains, and projects a pain in the rear end. What could be causing this?

Change the control to a combo box with dropdownstyle as dropdown .

weapey
Jun 11, 2003

stomp stomp stomp

epswing posted:

Can any gurus here comment on http://northhorizon.net/2011/the-right-way-to-do-inotifypropertychanged/ ?

That looks like a pretty elegant solution. I used a combination of the lambda and the MS recommendation, which isn't as painful to work with if you're also using ReSharper. The lambda issue is only god-awfully slow if you're doing a lot of notifications - updating a property through databinding after a user has tabbed off a field isn't going to (or usually shouldn't) trigger a load of notification events.

Sedro
Dec 31, 2008

epswing posted:

Can any gurus here comment on http://northhorizon.net/2011/the-right-way-to-do-inotifypropertychanged/ ?

He creates separate callback methods for each property change. This has pros and cons so I'd say it's personal preference. The alternative:
code:
protected override void OnPropertyChanged(string propertyName)
{
    switch (propertyName)
    {
        case "Foo":
        case "Bar":
            NotifyPropertyChanged("Foobar");
            break;
    }
}
I like having the equality comparison logic centralized and using IEquatable<T> instead of operator==.

I don't like how he handles the Foobar property. I would prefer this:
code:
public int Foobar
{
   get { return _foo + _bar; }
}
But now when Foo or Bar changes I'm back to firing INPC outside of the property:
code:
private void UpdateFoobar() { NotifyPropertyChanged("Foobar"); }
The ref keyword won't work when a property is not backed by a field.

Milotic
Mar 4, 2009

9CL apologist
Slippery Tilde
I appreciate it only runs in Debug, but creating StackTraces is horrible for performance, and is likely to bite you in the rear end with race conditions and whatnot when everything runs faster on Prod. More importantly, it uses reflection to subscribe to events. This could be fairly horrible if you're subscribing to lots of objects.

Additionally, it doesn't seem like there's a nice way of raising PropertyChanged events on calculated fields (Silverlight still does not support multi-value binding, or even a nice way of saying "Holy poo poo, the entire object has changed", so you're stuck with sometimes triggering multiple RaisePropertyChanged calls from a single setter).

The code whilst beautiful, doesn't live in a vacuum - you'd still be using the old form of INotifyPropertyChanged when dealing with ObservableCollection, auto-generated code (e.g. WCF web services and no one hand cranks that poo poo unless there is a bloody good reason to). Also it doesn't really work for views, given the point that you don't need to know the exact type of the view model, just that it has some properties.

To be quite blunt, it smacks of something done by someone who needs to learn when to let go of type safety a little. Don't do this sort of thing in a work project.

A MIRACLE
Sep 17, 2007

All right. It's Saturday night; I have no date, a two-liter bottle of Shasta and my all-Rush mix-tape... Let's rock.

What's the best way to redirect urls from domains in IIS7. So let's say my client buys https://www.bad-example.com adds it to my DNS, and wants it to go to https://www.my-domain.net/bad-example ? I can figure it our for paths after "/" but not redirects for the base domain. Right now all I can get is those URLs to point to https://www.my-domain.net. Do I have to set up a new website to do this?

It's been a long day, so maybe I'm just not thinking straight. But IIS6 seemed a lot better at this sort of thing...

\/\/\/Thanks\/\/\/

A MIRACLE fucked around with this message at 23:31 on Aug 2, 2011

Sprawl
Nov 21, 2005


I'm a huge retarded sperglord who can't spell, but Starfleet Dental would still take me and I love them for it!

A MIRACLE posted:

What's the best way to redirect urls from domains in IIS7. So let's say my client buys https://www.bad-example.com adds it to my DNS, and wants it to go to https://www.my-domain.net/bad-example ? I can figure it our for paths after "/" but not redirects for the base domain. Right now all I can get is those URLs to point to https://www.my-domain.net. Do I have to set up a new website to do this?

It's been a long day, so maybe I'm just not thinking straight. But IIS6 seemed a lot better at this sort of thing...

Yea you have to create a new website folder that binds the dns name or ip address/port.

Sprawl fucked around with this message at 23:00 on Aug 2, 2011

wwb
Aug 17, 2004

Or bind the same website to bad-domain.net and then use the url rewriting function to point it in the right direction.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



I'm trying to work on a Silverlight application and I don't really want it to open in my default browser (Opera) because if it does and I hit a breakpoint, the whole browser is locked until the debugger is finished. I tried using the option in the ASP solution to start an external program, but when I run it that way, no debugging symbols will load.

Has anybody else had this problem and figured it out?

Sedro
Dec 31, 2008
You can change the default browser that Visual Studio uses:

quote:

...right click on the web page that has your Silverlight control (such as default.aspx) and choose “Browse with…” from the context menu.

wwb
Aug 17, 2004

There is even a VS2010 extension for fast browser switching.

Also, my experience with other web apps is that if you just open the browser and browse to the url you get the same server-side debugging session. Not sure if that applies to SL as I don't play that poo poo.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Sedro posted:

You can change the default browser that Visual Studio uses:

Funny story - picking IE still opens the page in Opera, but in a private tab for some reason.

Doesn't matter, though. There are still no debug symbols loaded on any other browser.

Edit: Setting IE as the system default works but not FF. Ugh

Munkeymon fucked around with this message at 14:55 on Aug 5, 2011

TasteMyHouse
Dec 21, 2006
Prefacing this by saying that I have very little .NET experience.

My company uses an internal tool for automating certain kinds of hardware testing which uses C# for scripting. There's a device I'd like to talk to inside one of these scripts which is not supported by the tool natively, but I found a C library that lets me talk to it. What I've done is wrapped the C Library in a C++/CLR class, then compiled that into a DLL, used Assembly.LoadFile to load the DLL, assembly.GetType() to get my class, Activator.CreateInstance() to make an instance of my class, then Type.InvokeMethod() to call methods on the class. This works, but I feel like I'm making something that's going to show up in the Coding Horrors thread in a couple of years. Is there a Simpler / Better way to do this?

The tool I need to be able to run this in is running on .NET 2.0.50727.3623 and seems to be using NRefactory to parse the C# scripts (I am guessing anyway, because I found a dll for that in the application directory).

Stupid internal politics prevent me from having access to the source of the tool I'm trying to interact with, so the only solutions I can use are ones that can be run directly in the script.

TasteMyHouse fucked around with this message at 20:17 on Aug 8, 2011

Adbot
ADBOT LOVES YOU

Sedro
Dec 31, 2008
You should be able to reference the C++/CLR wrapper class without a bunch of reflection. There is a way to add a reference using CodeDom, and I would assume NRefactory as well. If the scripting tool doesn't expose this, you're SOL. If you were using .NET 4, dynamic dispatch would make things a bit easier.

  • Locked thread