|
Yeah, use a timer or if you have a problem with that use the Thread.Sleep(So many milliseconds); statement.
Salil Khedkar [^]
|
|
|
|
|
Hi all,
how can we add history of recently viewed items into our application's menu. I have developed an interface, i want to keep track of all the recently viewed file names in my File menu (atmost 5).
is it easy to implement it or should we have to do some rigourous work...?
any ideas or suggestions?
thanks in advance,
Suman
|
|
|
|
|
You'll have to define a container where you store the names respectively the paths of your recently viewed files. This could be a simple array or maybe some more complex container.
To your MainMenu add a MenuItem which is for example named "Recently Files". In the PopUp event of this MenuItem add a MenuItem for each file in your container. At last write an event handler for Click events of the several MenuItems and load the specific file.
www.troschuetz.de
|
|
|
|
|
|
Hi guys,
thanks for ur advices and suggestions and they did help me in coding my own program.
thanks,
Suman
|
|
|
|
|
Does anyone know how to change the font size of the column header in a ListView?
I'm wrinting an App in C#
Have a great coding day.
Regards,
Tony D. Abel
|
|
|
|
|
There is a nice article on customizing the ListView located here[^]. Basically you will see that he has subclassed the NativeWindow . There are a few important steps; in particular, you must assign the handle of the subclassed native window to that of the handle to the header control's handle. Also, you will need to override the WndProc and watch for WM_SETFONT (0x0030). This will allow you to specify your own Font instance and then assign the WParam of your message the handle of your newly created font (call ToHfont() on your Font object), then obviously you will want to call base.WndProc(ref m); so your changes are propagated. The following P/Invoke signature will be needed:
[DllImport("User32.dll",CharSet = CharSet.Auto,SetLastError=true)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
- Nick Parker My Blog | My Articles
|
|
|
|
|
Can someone point me to a 'good' site that outlines good naming conventions for objects and variables etc. for both c# and VB? Preferably a microsoft site. Doing a search for this brings up nothing substantial. Thanks!
SN
|
|
|
|
|
|
|
Hi
I have a form which is being displayed, and from my class I would like to be able to set control values.
In VB6 I would have done this by simply frmMain.txtName = "John", however, in C# doesn't seem quite so easy. What am I doing wrong?
Thanx
|
|
|
|
|
How can we tell you if you don't show any code?
|
|
|
|
|
Well, your question is rather nebulous, in particular because assuming you had a Form class instance called frmMain , you could simply access the control and then specifically the specific property of the control (this may be the part you were missing) like this:
frmMain.txtName.Text = "John";
Also, if you are referencing the control from within the class it is defined, you can change properties as such:
this.txtName.Text = "John";
txtName.Text = "John";
Hope this helps.
- Nick Parker My Blog | My Articles
|
|
|
|
|
Hi Nick
Thanks for taking the time to help, and sorry for the lack of detail. I have a form - frmMain - which is my main window. Within frmMain (ie the code behind the form), I have a function called Main(), within this function I load the form using the following code:
static void Main()
{
Application.Run(new frmMain());
}
This Main() function is set as my startup function therefore by default.
On this form, I have some textboxes, relating to a specific job. I am trying to keep all the processes that relate to this job in it's own class. So populating the fields on the form, with values from the database would be a method in the class.
But, my problem seems to be accessing the form controls, from the class.
Not sure if this makes the situation any clearer. Is it because the form has to be instantiated in the class? Or do the form controls need to be static in order for them to be visible to the class? Or am I barking completely up the wrong tree?!?
Thanks again.
M
|
|
|
|
|
isittheweekendyet wrote:
Not sure if this makes the situation any clearer.
Yes, this helps. First it's important to understand that the following are identical.
static void Main()
{
Application.Run(new frmMain());
}
static void Main()
{
frmMain frm = new frmMain();
frm.ShowDialog();
}
So where are you trying to access a controls property? This is a quick example:
public class test : System.Windows.Forms.Form
{
Button b;
Label l;
public test()
{
l = new Label();
this.Controls.Add(l);
b = new Button();
b.Location = new Point(20, 20);
b.Click += new EventHandler(bClick);
this.Controls.Add(b);
}
public static void Main()
{
Application.Run(new test());
}
private void bClick(object sender, EventArgs e)
{
l.Text = "Hello";
}
}
- Nick Parker My Blog | My Articles
|
|
|
|
|
I am accessing the controls as follows (continuing with your example):
public class test : System.Windows.Forms.Form
{
Button b;
Label l;
Client myClient = new Client();
public test()
{
l = new Label();
this.Controls.Add(l);
b = new Button();
b.Location = new Point(20, 20);
b.Click += new EventHandler(bClick);
this.Controls.Add(b);
}
public static void Main()
{
Application.Run(new test());
}
private void bLoadClientClick(object sender, EventArgs e)
{
myClient.LoadClient();
myClient.DisplayClient();
}
}
public class Client
{
string strName = "";
string strAddress = "";
string strTelephone = "";
public void DisplayClient()
{
txtName.Text = strName;
txtAddress.Text = strAddress;
txtTelephone.Text = strTelephone;
}
public bool LoadClient()
{
...
...
}
}
That type of thing.
Thanks
|
|
|
|
|
I have an application that accesses a password protected database. I know that is it not recommended placing the password within the code of the application that accesses the database. Especially with the ability to decompile .NET.
What are some recommended ways/best practices of handling the password storage on the user's PC but preventing the user from seeing the password?
|
|
|
|
|
This is a sore subject with many, however, one possible consideration would be to store an encrypted version of the password within a registry key.
- Nick Parker My Blog | My Articles
|
|
|
|
|
|
hi
does anybody know how to insert a picture in a listbox?
huda
|
|
|
|
|
hudhud wrote:
does anybody know how to insert a picture in a listbox?
Sure, there are several articles on the Internet that show how to do this, it is rather simple. Andrew Ma wrote an article covering this at the devhood site (a place I used to hang out a long long time ago). His article can be located here[^]. There is also another article here on CP that covers it:ListBox with Icons[^]
- Nick Parker My Blog | My Articles
|
|
|
|
|
Hello!
I have to txt-files, each at 37MB and 337000 lines.
I need to compare the lines in these two files and find differences.
The file content is products and their prices.
I intendt to find those products that have different price and do some computing and statistics on the price change.
This is is timeconsuming and I need some advice.
Is there a really smart way to compare the content of the two files?
Perhaps put the data into some sort of table to achive performance during datacomparison...
Any suggestion is appriciated.
/sverre
|
|
|
|
|
C# is probably not the best language to use. You could use file compare from the command prompt and the just redirect the output to another file. The following will do this for you.
fc file1.txt file2.txt > diff.txt
- Nick Parker My Blog | My Articles
|
|
|
|
|
I have a DLL that was written using C++. I am required to write a C# program that calls into the DLL and gets some information out of it. The DLL entry points that I have to call are declared as:
__declspec(dllexport) HostInfo* GetHostInfoFromDb (int iHostId);
__declspec(dllexport) void FreeHostInfoStruct (void* pHostInfo);
struct {
char* pHostName;
int iId;
char* pOwner;
...
...
} HostInfo, *P_HostInfo;
HostInfo is a structure containing a bunch of flat fields (i.e, all basic data types except for a couple of char*'s ). When GetHostInfoFromDb is called, it allocates a HostInfo structure internally, populates it with the information from a database, and retuns me the pointer to it.
The way I did this in C# is as follows:
[DllImport @"hostinfo.dll",EntryPoint="GetHostInfoFromDb")]
public static extern IntPtr GetHostInfoFromDb(int iHostId);
[DllImport @"hostinfo.dll",EntryPoint="FreeHostInfoStruct")]
public static extern void FreeHostInfoStruct(IntPtr pHostInfo);
[StructLayout(LayoutKind.Sequential,Size=116,CharSet=CharSet.Ansi )]
public struct ManagedHostInfo
{
string szHostName;
int iId;
string szOwner;
.....
.....
}
I could successfully get my DLL to return the information I was looking for by calling GetHostInfoFromDb. I marshal the returned IntPtr using Marshal.PtrToStructure. However, when I try to call FreeHostInfoStruct, the DLL doesn't seem to free up the memory it had allocated. After a few hundred calls, the memory kept growing and started impacting the system performance.
I verified this by first printing out the address of the HostInfo struct that the DLL allocated during a call to GetHostInfoFromDb. Next, I print out the address of pHostInfo during my call to FreeHostInfoStruct. The two addresses are different. What was initially allocated is not being freed up during FreeHostInfoStruct.
C# code C++ Code
---------------------------------------------------------------------------------------
1. IntPtr iPtr = GetHostInfoFromDb (0); ---> P_HostInfo pInfo = new HostInfo ();
// Populate pInfo fields
// using info from database
return pInfo; //Say, pInfo = 0x46F53A44
2. ManagedHostInfo hostInfo =
Marshal.PtrToStructure (iPtr);
// If you look at iPtr, it is the
// same as pInfo (=0x46F53A44)
3. FreeHostInfoStruct (iPtr); ---> void FreeHostInfoStruct (void* pHostInfo)
{
// if you check out pHostInfo here,
// it's different that what was
// originally passed by the caller.
// So in our example, this won't
// be 0x46F53A44, but something else!!
}
So what am I doing wring here? Please help!!
|
|
|
|
|
First - a few words about your unmanaged signatures.
You should almost never return void . For your "free" functions, this can be okay but sometimes you might gets errors back that would be good to know about.
Mostly, never return pointers. Return error codes (like HRESULT s) and declare your previous return values as [out] parameters. If you did that, your unmanaged to managed mapping would be easy and you wouldn't have to worry about marshaling the struct yourself:
__declspec(dllexport) HRESULT GetHostInfoFromDB(int iHostID, P_HostInfo hostInfo);
[DllImport(...)]
extern static int GetHostInfoFromDB(int iHostID, out HostInfo hostInfo); I would also warn that if you want to support both 32- and 64-bit architectures, your native int is equivalent to an IntPtr (both platform-dependent bit widths).
Moving on, though...
You need to pin your structure in memory using the GCHandle class (see the documentation in the .NET Framework SDK). The GC can move this around so that when you pass the address back to FreeHostInfoStruct . You can also use Marshal.DestroyStructure , which you typically should use when you use Marshal.PtrToStructure . In this case, however - since your freeing memory in unmanaged code - pinning the object should solve the problem.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|