all groups > dotnet windows forms designtime > january 2005 >
You're in the

dotnet windows forms designtime

group:

Form Background Image in Designer Host


Form Background Image in Designer Host IMailInterface NO[at]SPAM gmail.com
1/5/2005 9:10:55 PM
dotnet windows forms designtime:
HI,
Iam creating Application implementing IDesigner host and facing
problem when embedding image in the form.After embedding the image in
the form when i compile i get an Error "resource not found".I tried
this on the MSDN provided Sample Designer host and it is also giving
the same error.Any clues solution this problem

Re: Form Background Image in Designer Host Simon Lawrence
1/9/2005 12:57:31 AM
On 06/01/2005 5:10 AM, in article
83b31e2a.0501052110.529cb1aa@posting.google.com, "MailInterface"
[quoted text, click to view]

The MSDN sample doesn't write out the resources except in its XML format, if
you change the code as follows (apologies if it gets mangled by line
breaks):

SampleResourceService.cs

using System;
using System.Resources;
using System.ComponentModel.Design;
using System.IO;
using System.Collections;

namespace SampleDesignerHost
{
/// Resource service using .resx files
public class SampleResourceService : IResourceService, IDisposable
{
private Hashtable writers;
private Hashtable readers;
private IDesignerHost host;
private MemoryStream ms;
// Track whether Dispose has been called.
private Boolean disposed = false;

public SampleResourceService(IDesignerHost host)
{
this.host = host;
writers = new Hashtable();
readers = new Hashtable();
}

~SampleResourceService()
{
Dispose(false);
}

private void SerializationComplete()
{
foreach (IResourceWriter writer in writers.Values)
{
if (writer != null)
{
writer.Close();
writer.Dispose();
}
}
writers.Clear();

foreach (IResourceReader reader in readers.Values)
{
if (reader != null)
{
reader.Close();
reader.Dispose();
}
}
readers.Clear();
}

public byte[] Resources
{
get
{
SerializationComplete();
return ms.GetBuffer();
}
}

#region Implementation of IResourceService

public System.Resources.IResourceReader
GetResourceReader(System.Globalization.CultureInfo info)
{
ResXResourceReader reader = (ResXResourceReader) readers[info];
if (reader == null)
{
if (ms == null)
{
ms = new MemoryStream();
}
reader = new ResXResourceReader(ms);
readers.Add(info, reader);
}
return reader;
}

public System.Resources.IResourceWriter
GetResourceWriter(System.Globalization.CultureInfo info)
{
ResXResourceWriter writer = (ResXResourceWriter) writers[info];
if (writer == null)
{
if (ms == null)
{
ms = new MemoryStream();
}
writer = new ResXResourceWriter(ms);
writers.Add(info, writer);
}
return writer;
}

#endregion

#region IDisposable Members

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

private void Dispose(Boolean disposing)
{
// Check to see if Dispose has already been called.
if(!disposed)
{
SerializationComplete();
}
disposed = true;
}

#endregion
}
}

SampleDesignerLoader.cs - Save method

/// Save the current state of the loader. If the user loaded the
file
/// or saved once before, then he doesn't need to select a file
again.
/// Unless this is being called as a result of "Save As..." being
clicked,
/// in which case forceFilePrompt will be true.
internal void Save(bool forceFilePrompt)
{
try
{
if (dirty)
{
// Flush any changes to the buffer.
Flush();
}

// If the buffer has no name or this is a "Save As...",
// prompt the user for a file name. The user can save
// either the C#, VB, or XML (though only the XML can be
loaded).
//
int filterIndex = 3;
if ((fileName == null) || forceFilePrompt)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.DefaultExt = "xml";
dlg.Filter = "C# Files|*.cs|Visual Basic Files|*.vb|XML
Files|*.xml";

if (dlg.ShowDialog() == DialogResult.OK)
{
fileName = dlg.FileName;
filterIndex = dlg.FilterIndex;
}
}

if (fileName != null)
{
switch (filterIndex)
{
case 1:
{
// Generate C# code from our codeCompileUnit and
save it.
CodeGeneratorOptions o = new
CodeGeneratorOptions();
o.BlankLinesBetweenMembers = true;
o.BracingStyle = "C";
o.ElseOnClosing = false;
o.IndentString = " ";
StreamWriter sw = new StreamWriter(fileName);
CSharpCodeProvider cs = new
CSharpCodeProvider();

cs.CreateGenerator().GenerateCodeFromCompileUnit(codeCompileUnit, sw, o);
sw.Close();
} break;
case 2:
{
// Generate VB code from our codeCompileUnit and
save it.
CodeGeneratorOptions o = new
CodeGeneratorOptions();
o.BlankLinesBetweenMembers = true;
o.BracingStyle = "C";
o.ElseOnClosing = false;
o.IndentString = " ";
StreamWriter sw = new StreamWriter(fileName);
VBCodeProvider vb = new VBCodeProvider();

vb.CreateGenerator().GenerateCodeFromCompileUnit(codeCompileUnit, sw, o);
sw.Close();
} break;
case 3:
{
Re: Form Background Image in Designer Host MailInterface !
1/9/2005 3:40:13 AM
Hi Simon Lawrence,


Thank you very much for your detailed reply.But still
facing some problem.here is the replication steps.

Open the designer host and put a image as background image.go to the C#
code view,we can see the
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(form1));
entry in the Initialize component.Now go back to the designer view and
add one control into it.go back to the code view then you can notice
that
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(form1));
is missing and the entry
this.BackgroundImage =
((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));

has changed to
this.BackgroundImage =
((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage1")));

(Some times this happens after adding 2-3 controls).

At this time when i compile the project i will get error resource not
found.

Even the first time iam not able to run the application,if i have an
background on the form.

Have any idea about this problem?

Thanks

*** Sent via Developersdex http://www.developersdex.com ***
Re: Form Background Image in Designer Host Simon Lawrence
1/9/2005 3:27:04 PM
On 09/01/2005 11:40 AM, in article ea0j8$j9EHA.2804@TK2MSFTNGP15.phx.gbl,
[quoted text, click to view]

If I remember correctly you may need to subclass CodeDOMSerializer to handle
the ResourceManager member of the form as a special case. I'll have a look
through my own code and see what I can pull out and shove in to the sample
but it may take me a day or two to get round to it as my stuff is in VB.Net
rather than C sharp. I hadn't really looked at this sample in any detail
before and noticed that it takes some other shortcuts apart from the
resource stuff (e.g. not exposing a "Name" extender property for components
in the designer!) that have an impact on serialization to code. It would be
nice if copy/paste was implemented in it as well, as that's the bit I can't
get working in my own implementation.

Cheers,

Simon
Re: Form Background Image in Designer Host MailInterface !
1/10/2005 8:04:24 PM
Here is the link

http://download.microsoft.com/download/f/d/9/fd986a23-d3d6-44c3-8fa0-75e
21b0094bf/designerhost.exe

*** Sent via Developersdex http://www.developersdex.com ***
Re: Form Background Image in Designer Host Ulrich Sprick
1/10/2005 11:13:44 PM
Hi,

can someone point me to the SampleDesignerHost in MSDN? I simply can't find
it...
Thx!
ulrich

AddThis Social Bookmark Button