Archive for category C#
How to manipulate a Windows Forms control from another thread and get return value via Invoke() method
I was working on a Windows Forms project and I had this requirement to basically do some manipulation on a TreeListView on the parent form (MainForm in this case) for which we already had a developed a method and also to get the return value back from it. To make life even easier, we needed to do this from a BackGroundWorker thread in this case. Now, the problem with Windows Forms controls is that they can’t be manipulated apart from the main thread, so I had to look for a work around of some sort. So here’s a little modified snippet of the code for your understanding:-
ra= (ReportData?)m.treeListView.Invoke(new Func<ReportData?>(
() => m.CreateDataforReport(“Rpt type”, false)));
//m is the main form’s reference!
So what I’ve done is that I passed the main form’s reference to the child form in the constructor and made the TreeListView and the CreateDataforReport public. Notice that I called CreateDataforReport method inside the Func<ReportData?> and not directly. This is what enables you to get the appropriate return value, be it string, integer or any complex type like I have it here.
The type ‘System.IO.Packaging.Package’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘WindowsBase’
While I was working with the Open Office XML SDK 2.0, after I added the reference to the DocumentFormat.OpenXml assembly and tried to build the application I ran into this error message.
The type ‘System.IO.Packaging.Package’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘WindowsBase, Version=22.214.171.124, Culture=neutral, PublicKeyToken=31bf3856ad364e35′
Turns out, all you need to do to fix it is to just add another reference to WindowsBase assembly that is found in the .NET Tab.
Today I started woking on a windows application at home and I was very annoyed by the error I was getting whenever I tried to debug/build the application that said: “Could not execute CVTRES.EXE“
Tried lots of things like giving full rights to “everyone” on the debug folder and some other silly things, untill I found out that there was something in ZoneAlarm that was causing the problem. I had to uninstall and re-install ZoneAlarm. Turning it off was not sufficient. I never understood what was causing the problem at that time and to be honest I’m not too concerned with that either. May be there was some setting that needed a reset or something. But anyway like they say: “All is well that ends well”!
Hope some of you might find this useful someday.
It’s simple, yet not very obvious. All you have to do is to change the normal behavior of the commas you use in the Where function to act as an OR. You’d do it like this:-
ObjES.Query.es.DefaultConjunction = esConjunction.Or;
ObjES.Query.Where(<<Go ahead with using the commas that usually defaults to an AND conjunction>>);
oh and I almost forgot to mention my colleague Bilal mentioned this to me. So special thanks to him as well.
Here’s a nice little piece of code you can use to change file attributes of a particular file, its particularly useful when you’re trying to delete a read only file.
string savedCardFilePath = “My absolute file path”; if (File.Exists(savedCardFilePath))
* Remove readonly attribute off of the file if it exists before execution
if(File.GetAttributes(savedCardFilePath) == FileAttributes.ReadOnly)
UPDATE May 14, 2012:
There have been some problems reported and better solutions given on this article so I updated the code to:
FileAttributes attributes = File.GetAttributes(file);
if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
File.SetAttributes(file, attributes ^ FileAttributes.ReadOnly);
Thanks for the feedback and sorry about the mistake
You can pass multiple columns in like this:-
There is a common issue that you can’t manipulate session objects in http handlers, the files with ASHX extension Well normally you can read the contents in the session via context.Session but you can add/remove/modify that in there. So how do you do this:
The answer is simple: Just implement the IRequiresSessionState interface. It doesnt have any method to implement so you dont have to worry about any details, this just notifies the runtime that Session can be manipulated in this context. So your class declaration would look something like this:-
public class SetLabels : IHttpHandler, IRequiresSessionState
If you have a full file path like D:\My Documents\myimg.jpg and you want to get just the file name myimg.jpg, you can get it with the help of the Path class in this fashion:-
string filename = Path.GetFileName(“D:\My Documents\myimg.jpg“);
In VB .NET
Dim fileName as String = Path.GetFIleName(“D:\My Documents\myimg.jpg”)
Actually these are not exactly ‘true’ random numbers. These are actually pseudo-random numbers, which means that they give you an illusion that they’re being generated randomly.
It’s very simple: There’s a class called System.Random. and the rest is as simple as this:-
Random r = new Random(0);
int num = r.Next(0,999999);
in VB .NET
dim r as Random = new Random(0)
dim num as Int = r.Next(0,999999)