|
Did you step through your code and see if this method even gets called? You must first call this method (or a similar one) to load the data into the cache and create the callback.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Yes, I've stepped through the method. It gets called, and there is something on the cache when it gets called. Once that method is called the remove succeeds, as does the loading of the data. However when I call add it doesn't seem to add it to the cache, however upon the next request it sees that there is nothing on the cache, loads the data, and readds it with no problem, and what I don't understand is that I call add then with the same parameters as I do in the RemovedCallback method, only this time it does add it.
|
|
|
|
|
First, you shouldn't call Cache.Remove : the callback is invoked after the item - and remember there could be multiple objects in the cache with different keys - is removed, but you need to be mindful of the key. The following example should work:
private const string KEY1 = "File1.txt";
private const string KEY2 = "File2.txt";
private void CacheFile(string key)
{
if (key == null) throw new ArgumentNullException("key");
if (key.Length == 0) throw new ArgumentException("Invalid key.");
if (Cache[key] == null)
{
string path = Server.MapPath("/docs/" + key);
using (StreamReader reader = new StreamReader(path))
{
Cache.Add(
key,
reader.ReadToEnd(),
new CacheDependency(path),
Cache.NoAbsoluteExpiration,
Cache.NoSlidingExpiration,
CacheItemPriority.AboveNormal,
new CacheItemRemovedCallback(CacheItemRemoved));
}
}
}
private void CacheItemRemoved(string key, object value,
CacheItemRemovedReason reason)
{
if (key == KEY1) CacheFile(KEY1);
else if (key == KEY2) CacheFile(KEY2);
}
public string File1Content
{
get
{
CacheFile(KEY1);
return Cache[KEY1];
}
}
public string File2Content
{
get
{
CacheFile(KEY2);
return Cache[KEY2];
}
}
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
:-OI'm printing a windows form by looping thru all the controls on the form, but I realized the form needed more than one page to fully print. I know I need to loop thru the controls but not with a 'for' loop. I need to increment each control with a counter of sorts so if I set the hasmorepages, the code will know which control to start with on the next page, but I can't get anything to work. help?
Here's the function that works, but without printing more data on the next page:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
.......
.......
Graphics g = e.Graphics;
DrawAll(e.Graphics, e);
foreach(Label lbl in panel1.Controls)
{
g.DrawString(lbl.Text, lbl.Font, Brushes.Black, lbl.Bounds.Left*scalex, lbl.Bounds.Top, new StringFormat());
}
}
Nancy
|
|
|
|
|
Use a for loop instead with the current index as a field in your class initialized to 0. then use a for loop like so:
for ( ; i < Controls.Count; i++)
{
Label lbl = Controls[i] as Label;
} Here, i would be a field in your class declared something like:
private int i = 0; FYI, your foreach loop has a major flaw: if a control in the Controls collection is not a Label , an exception would be thrown. This may not be applicable in your application now (if panel1 contains only Label s), but if you ever add anything else your code will throw a cast exception.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
thanks....that worked fine. And yes, this form is all labels. But now I have another issue. The first 4 labels on the form were placed on the form during development, and the next bunch are done at runtime. The runtime ones print in order, but the first 4 don't. #2 prints, then #1, then #3, then #4 due to the order I placed them on the form. Is there a way to 'reorder' these so the loop thru the label controls function does what I want it to? I even removed them and recreated them in the order I wanted them, but still they came out wrong. help?
|
|
|
|
|
The designer automatically uses Controls.AddRange to add them. This is in reverse order, so you can re-order them that way, so that they are added as 4, 3, 2, 1. You can also use View->Tab Order and select them in the correct order. This should, IIRC, cause the designer to re-order them as well, though you should double check.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
thanks for the fast reply! I did try to reorder by the taborder but it didn't do the trick. But I'll try the addrange. Thanks again!
|
|
|
|
|
My quick and dirty solution (there was no time to develop something more efficient):
1. Find maximum area of whole print
2. split it into printable pages (tiles)
for every page you print:
1. set offset of graphics object into current page
e.Graphics.TranslateTransform(-PrintRectangle.X+e.MarginBounds.Left, -PrintRectangle.Y+e.MarginBounds.Top+this.titleHeight);
2. create "window" for graphics:
e.Graphics.SetClip(PrintRectangle);
3. print EVERYTHING - all the controls you want in any order you want. That's the trick. Clip makes the rest for you
|
|
|
|
|
I accidently screwed up in my OnStart for my service by initializing my main thread before my argument checks and now after uninstalling the service, it is in the state of "Disabling." I've tried rebooting but it won't uninstall fully. I can't reinstall and I can't recompile, so does anyone know what I should do?
Thanks.
----
Xian
|
|
|
|
|
If you can't recompile, that's entirely a different problem, something you need to check, fix, and then recompile.
To get rid of the registry entry for the service, open regedit.exe and drill-down to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services. Find the key with the name (not the display name) of your service and delete it and reboot.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I couldn't recompile because the .exe was in use by the system. I managed to get it out via safe mode.
Thanks.
----
Xian
|
|
|
|
|
I'm coding a MDI form application. What I really want the form (not the controls it contain) to look like is similar to Office 2003, which reflects the theme the system is using. Could someone give me some help?
Thanks!
|
|
|
|
|
Unless you draw the UI elements with the theme data yourself, it's either all or nothing for applying visual styles.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Also, if you want the Office 2003 look, there are dozens of third-party controls out there (maybe even some free ones, though you get what you pay for often times). Just google.
There's a couple here on CodeProject as well, although - IMO - I don't think they even come close to Office 2003.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I can call EnableVisualStyle to make contained controls to have visula styles. But for the form itself, e.g. border colors, how do I make that happen?
|
|
|
|
|
I have never done this, but from reading I think it is possible. You can create a manifest file using XML to tell your application to use the operating system's style.
The file is placed in the same directory as your project, and is called myProject.exe.manifest
I don't have a link for it at the moment, but I'm sure information will be readily available using google
Hope it helps
|
|
|
|
|
I'm not really sure. I've never tried a callback with a class method before. You'll just have to try it. If you would, please reply whether or not it works.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hi all,
I tried to implement auto -indentation feature in my editor.
using some ideas from codeguru and codeproject .
i did the following code.
The problem is whenver i press "enter" key
the tab is incremented automatically in the next line.
(straight logic .if u see the code u will unerstand)
but i want the following feature :
if() (Enter key)
{ (Enter key)
MessageBox(); (Enter key)
DrawString(..); (Enter Key)
while(i>0) (Enter key)
{ (Enter key)
bool b = false; (Enter key)
} (Enter key)
} (Enter key)
else (Enter key)
{ (Enter key)
MessageBox(); (Enter key)
}
each time when i press "Enter" key the tab position
is changed . How to identify and move the tab
key properly.
Following is the fundamental code which i did .
How to improve this logic further.
I am struck up.Please help me.
Source code :
private void richTextBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
string temp="";
int indent=0;
if(e.KeyCode == Keys.Enter)
{
// taking previous line
string temp = Convert.ToString(richTextBox1.Lines.GetValue(richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart - 1)));
// GetIndent() is used to count the tabs in the previous line
indent = GetIndent(temp,12);
System.Diagnostics.Trace.WriteLine(indent);
//adding one tab further in the next line
for(int i=0;i
|
|
|
|
|
Hi guys,
First off im after some general help, Im in the wounderful process of learning C#, I have created a screen that I want the user to enter a username and password, this data comes form a access(20000) but i want to make it future proof, i mean at the moment im using access this is fine but I may want to use sql server 2000 to be the backend is their a way i can make switiching over easy in the future.
|
|
|
|
|
You have several options. The way that the .NET Framework is heading is the provider pattern, which is a pretty good system. See a recent article in MSDN - Provider Model Design Pattern and Specification, Part 1[^] - for more information.
A conceptually similar way without all the overhead of the typed class factories is what we currently use in our application.
Basically, you design an abstract class that performs the authentication. Then you extend this class (either in a different project or the same project) with Access-specific functionality. Later, you could extend this base class with SQL-specific functionality. In either case, you read-in a type from your application's .config file as a string (like "MyNamespace.MyAccessAuthClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0123456789abcdef"). You could, for example, simply store this in the <appSettings> section of your .config file and associate that with a key, like "authenticator". You then get that type, create an instance of it, and cast it to your base class (or interface, whichever you prefer - use a base class if you want to have basic functionality implemented, or use an interface if you want no default implementation of anything):
string type =
ConfigurationSettings.AppSettings["authenticator"];
if (type == null)
type = "MyNamespace.MyAccessAuthClass, MyAssembly, " +
"Version=1.0.0.0, Culture=neutral, PublicKeyToken=0123456789abcdef"
AuthenticatorBase auth = null;
try
{
Type t = Type.GetType(type);
auth = (AuthenticatorBase)Activator.CreateInstance(t);
}
catch (Exception ex)
{
MessageBox.Show("Could not create the authentication package: " + ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(1);
}
Obviously, the Type string is just an example, and you could use a partial type (namespace + classname, assembly name).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
need stuff about network with c#
thanks
|
|
|
|
|
Maybe you should elaborate on what you are looking for exactly.
Networks is not a very narrow topic.
|
|
|
|
|
System.Net namespace.
Due to technical difficulties my previous signature, "I see dumb people" will be off until further notice. Too many people were thinking I was talking about them...
|
|
|
|
|
I've been writing event handlers for my app. I have an event for a mousedown on the form, the event type is System.Windows.Forms.MouseEventArgs.
Using this event type I can type
e.Button to determine if the left or right mouse button
if i'm writing an event for an picturebox click the type is System.EventArgs. The is no method to determine the mouse button for this type. Using this event, is there any way to determine the mouse button, I want to use an if statement to ensure that only left clicks will allow the event code to change the picture box image and right clicks will do nothing.
Thanks in advance
|
|
|
|
|