|
Shy Agam wrote: That would create a potential problem if the Image is used elsewhere around the application.
Only if you used the same instance of the image elsewhere. If you do, I would look to using a weak reference[^] on the image.
Shy Agam wrote: Should my UserControl dispose of it when it gets disposed?
In my opinion, yes.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Hi Pete,
I always have been, and still am, struggling with this question. Here is a simple example:
I create a UserControl that holds a PictureBox;
I create a modal Form holding two such UserControls and some Buttons
Initially the Form loads one image and shows it in both UC.
then:
scenario 1: I press a button and another image gets loaded in one of the UCs (the original image remains in use in the other UC)
scenario 2: I press another button and one UserControl should vanish (the image remains in use in the other UC)
scenario 3: I press yet another button, it closes the Form (the image is no longer referenced).
So who should be calling Image.Dispose()?
It can't be the UC, as it does not "own" the image.
It shouldn't be the Form, as it will be very vulnerable as soon as the use cases get a bit complex.
So we need some pattern here that automates/hides the problem.
FWIW: the problem vanishes if one never assigns the same image to different UCs, which implies each UC shows a different instance of Image (possibly showing the same thing though), but I want to avoid loading the same image twice if I can reliably do so.
And I don't see how a WeakReference will help me; I know how to use them to possibly keep an object alive, as in a user-created cache. However here the PictureBoxes want to be sure they have the image at hand that they are supposed to show, nothing is supposed to be weak here.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Wow, talk about going way beyond the original problem. Right, here are some thoughts based on work we've done with this in the past.
If you want to show the same image in multiple locations then you don't make the lifetime of the image the responsibility of the items that are showing it. In other words, I'd have an ImageManager class that contained the image. When the controls want the image, then I would return a clone of that image to the control instead - the control can then happily dispose of that image. The lifetime of the ImageManager is then independent of the user control and can be maintained as a WeakReference.
We have used this technique successfully in several projects and it's worked well for us.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Yeah, that is what I was afraid of, another Manager.
Thanks. I was still hoping a ready-made solution existed.
Acutally, you now said it can't be the UC's responsibility. That is a change of heart from before[^]. The UC, being a component, is open to reuse.
Pete O'Hanlon wrote: going way beyond the original problem
curiosity. the ambition to learn. generalization. advancement. you name it.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Luc Pattyn wrote: another Manager
You can never have enough managers. The UK is proof how well that works.
Luc Pattyn wrote: curiosity. the ambition to learn. generalization. advancement. you name it.
Hoo yeah. I'm almost tempted to write a blog post demonstrating this.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Pete O'Hanlon wrote: I'm almost tempted to write a blog post demonstrating this.
what else do you need?
I'll be reading it!
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Luc Pattyn wrote: what else do you need?
Time.
Luc Pattyn wrote: I'll be reading it!
Ah. A reader. Excellent.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I've posted an article demonstrating a simple weak referenced image library that demonstrates what I've been talking about. It's available here[^].
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Hi Pete,
thanks for the notification.
BTW: you may want to check the downloads, they seem pretty empty at the moment.
Regards,
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
What??? Try now - for some reason the article editor put in a borked entry (or two).
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
All is well now. Thanks.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Shy.
Have a read of the article here[^]. It might help.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Hey guys,
You've helped me a lot before, but once again, I need some hints about how to program something.
Suppose that my application generated 2 arrays of int values:
ModulesOnSurface = {14, 12, 46}
ModulesConnected = {24, 14, 10, 18, 6}
This is only an example. The lengths could be different and so could the values. The sum of the items in ModulesOnSurface is always equal to the sum of the items in ModulesConnected (72 in this case).
The items of ModulesConnected should automatically be assigned to ModulesOnSurface. An item in ModulesConnected can be split and multiple items can be put together. This means there is always a solution, but I need just the one with as few splits as possible.
A possibility in this case is (1 split: 24 to 12+12):
14 = 14
12 = 12 (from 24)
46 = 12 (from 24) + 10 + 18 + 6
This would also be possible but less optimal (2 splits: 24 to 22+2 and 18 to 10+8), so I would like to avoid it as much as possible:
14 = 6 + 8 (from 18)
12 = 10 + 2 (from 24)
46 = 22 (from 24) + 14 + 10 (from 18).
Afterwards, I need to know how everything is assigned. So in the first example I need to know:
24: 12 to surface B and 12 to surface C
14: 14 to surface A
10: 10 to surface C
18: 18 to surface C
6: 6 to surface C
This is a brain breaker. I know how to get the assigning done without the possible splits but now that I can split and need the optimal solution, I'm kind of stuck...
Could someone help me out?
Thanks in advance!
|
|
|
|
|
|
In my opinion, setting the Path of the FileSystemWatcher to an empty string is wrong.
The control is meant for watching a physical folder, so you shold provide it with a path.
If you want to stop watching the current path, you ought to set:
fw2.EnableRaisingEvents = false;
and then reenable it again the next time you provide a valid path.
Good Luck
BTW: Don't cross post! See my other answer in QA...
modified on Thursday, May 20, 2010 4:47 AM
|
|
|
|
|
|
Interesting, in the big whole, you agree with me, and you accept the fact that you cannot clear the path. So why do you vote 1 for my answer which is in fact correct? Don't shoot the messenger...
|
|
|
|
|
|
But that still doesn't warrant a vote of 1 for my reply which is correct.
It's not my fault that MS has done it that way.
As I mentioned: Don't shoot the messenger just because you don't like the message.
|
|
|
|
|
|
EthicsGradient wrote: I don't see any other option.
Don't mark it as any.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I am handling WM_PAINT message for my (winforms) TextBox. As OnPaint is not available for this control, I catch wm_paint messages. Strange this is after I hook paint event, if I add any unicode characters, textbox shows only question marks.
The control by default can handle unicode characters(without wm_paint). After I call wm_paint event, the text box is no longer accepting unicode characters. Any idea ?
|
|
|
|
|
Found the answer!
I am using non-unicode versions of API for pinvoke. When I changed to unicode versions (W-suffix) it solved all.
|
|
|
|
|
I'm writing a simple flow calculator using <a href="http://www.brighthub.com/engineering/civil/articles/52905.aspx">Manning's Equation</a>[<a href="http://www.brighthub.com/engineering/civil/articles/52905.aspx" target="_blank" title="New Window">^</a>] to determine gravity flow rates in sewer lines. Not for publication, mind you - I'm just tired of having to do it manually with a calculator 50 times a day.
I started a new Windows Form application, added a couple of radio buttons for the user (me) to select the shape of the channel - round, rectangular, or trapezoidal - and created some functions to change the display of the main form according to which shape the user selects. The first two take two parameters, the third, three. It all works perfectly so far, to wit:
<pre> public partial class FlowCalcs : Form
{
private int Shape; //Channel shape
private int Material; //Channel material
private double N; //Manning's coefficient
private double Area; //Area of flow
private double Perimeter; //Wetted perimeter
private double Radius; //Hydraulic radius
private double Slope; //Slope of channel
private double Flow; //Flow velocity, ft/sec
private double Gpm; //Flow volume, gallons per minute
public FlowCalcs()
{
InitializeComponent();
}
private void rbtnRect_CheckedChanged(object sender, EventArgs e)
{
if (rbtnRect.Checked == true)
{
Shape = 2;
lblDh.Text = "Height, h =";
lbldW1.Text = "Width, W =";
lblW2.Visible = false;
txtW2.Visible = false;
txtW2.Enabled = false;
}
}
private void rbtnTrap_CheckedChanged(object sender, EventArgs e)
{
if (rbtnTrap.Checked == true)
{
Shape = 3;
lblDh.Text = "Height, h =";
lbldW1.Text = "Width, W1 =";
lblW2.Text = "Width, W2 =";
lblW2.Visible = true;
lblW2.Enabled = true;
txtW2.Visible = true;
txtW2.Enabled = true;
}
}
private void rbtnRound_CheckedChanged(object sender, EventArgs e)
{
if (rbtnRound.Checked == true)
{
Shape = 1;
lblDh.Text = "Diameter, D =";
lbldW1.Text = "Depth, d =";
lblW2.Text = "";
lblW2.Visible = false;
txtW2.Visible = false;
txtW2.Enabled = false;
}
}</pre>
Then the fun begins. I added a few textboxes to enter the dimensions of the channel, and coded a couple of handlers for the _TextChanged event.
<pre>private void txtDh_TextChanged(object Sender, EventArgs e)
{
testDimensions(Sender,e);
}
private void txtdW1_TextChanged(object Sender, EventArgs e)
{
testDimensions(Sender, e);
}
private void txtW2_TextChanged(object Sender, EventArgs e)
{
testDimensions(Sender, e);
}</pre>
The testDimensions method just checks the length of the text in each textbox to see if a meaningful calculation can be performed yet - a missing entry blocks execution of the numerical calculations. If all the right boxes have numbers in them it converts them to double and does the math.
The goofy bit is that, while the operations on the radio button events work perfectly, typing values into the textboxes never triggers the TextChanged event. Even weirder (is that a word?), when I use the Watch window in the debugger, every variable contained in the main Form is reported as 'does not exist in the current context' or some such wording. Another weirdity (I know that's not a word) is that, when trying to step through the program, each key click advances correctly through the Program.cs file, then it shifts to the Designer file to initialize the Form object. When that's done, my form is displayed, then it moves the cursor back to the Program.cs file, instead of the Form's main page. I haven't seen that happen before, and it confuses me greatly.
Another clue to this mystery: When I enter a value into a textbox while debugging, it makes a 'bonk' sound; when I tab to the next textbox, it doesn't. I don't know what that means, and searching MSDN for 'bonk sound' hasn't been too revealing.
By the way, there are no syntax errors, compilation errors, or runtime errors reported. According to Visual Studio 2008, my program is perfect; it just doesn't do anything. I've been beating this deceased equine for four evenings so far, with no progress. Can someone provide me a clue?
[EDIT]
Now this is odd. I posted this using Opera, and I'm now viewing it with IE7. What got posted was not what I intended viewers to see. The sig line was included in the body of the post, and the code blocks were not encoded correctly. Interesting...
[/EDIT]
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
Hi Roger,
what strikes me is your TextChanged handlers use Sender, not sender. That is immaterial to the correct operation, but it tells me something has happened there.
Hint1 (experiment): change txtDh_TextChanged to txtDh_TextChanged123 (that is a one-line change!) and make sure it does not compile. If it does, it proves your txtDh is NOT wired to that handler.
Hint2: search your project for "txtDh_TextChanged_" (NOT whole word); if you find any, look for all of them. I expect there to be two, one of them containing a throw.
Hint3: maybe the textbox names are a bit odd too; I see Dh, dW1 and W2. C# is case-sensitive.
Hint4: maybe you have more than 3 textboxes, some of them at the same location, and when you think you type in one, you actually type in another one that is not wired up to a TextChanged handler.
Hint5: show us (the beginning lines of) testDimensions, and offer convincing proof it is not being called.
That is it for now. I'll be back tomorrow.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|