|
Are you sure p is not null ?
/ravi
|
|
|
|
|
|
Hi,
I want to make some app startup from the commandline no matter in which folder I am. So I was thinking of putting my executable in the c:\Windows folder.
now if I start the exe from "C:\program files" I want to know this path instead of the Application.StartupPath (which returns "C:\Windows")
Can anybody tell me how to get this path?
Thanks.
Ted
- Life would be so much easier if I had the source code!
- If C# had true garbage collection, most applications would delete themselves upon execution
|
|
|
|
|
found it, this does the trick:
Directory.GetCurrentDirectory();
- Life would be so much easier if I had the source code!
- If C# had true garbage collection, most applications would delete themselves upon execution
|
|
|
|
|
If I have understood your requirement correctly you could create an environment variable to give you accees to your application form anywhere.
On the Advanced tab of System Properties click on the Environment Variables button. Add the path to your app to the PATH variable and you will be able to start it from anywhere.
me, me, me
"The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!"
Larry Niven
|
|
|
|
|
You can pick up the location of many different "special" folders. This will get you the Program Files folder:
string strProgFilesDir = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
If, however, you really meant that you wanted the name of the directory where the assembly was located, then you should use:
using System.IO;
.
.
.
string strMyHomeDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
As an aside, rather than adding a program to the Windows folder, you should create a folder elsewhere (I have one called Tools) and add that folder to your environment Path. Perhaps it's just me but I don't like cluttering up system file locations.
|
|
|
|
|
I was spend for a long time looking for a decision of a problem, about the TextBox control that is included with Windows CE has a very limited opportunities for events related to mouse. I discovered that the team that developed the platform as it is a Compact Edition some controls are "clipped" from "unnecessary" events and an elaborate on them using unmanaged code by hooks and messages from the shell remitted. After a long search Internet and Googl-ing discovered fragments code and I have them so that they become finished code which help us for future implementations. You are welcome!
using System;
using System.Drawing;
using System.Runtime.InteropServices;
internal sealed class Win32
{
public delegate int WndProc(IntPtr hwnd, uint msg, uint
wParam, int lParam);
[DllImport("coredll.dll")]
public extern static int DefWindowProc(IntPtr hwnd,
uint msg, uint wParam, int lParam);
[DllImport("coredll.dll")]
public extern static IntPtr SetWindowLong(IntPtr hwnd, int nIndex,
IntPtr dwNewLong);
[DllImport("coredll.dll")]
public extern static int CallWindowProc(IntPtr lpPrevWndFunc,
IntPtr hwnd, uint msg, uint wParam, int lParam);
public const int GWL_WNDPROC = -4;
public const uint WM_PAINT = 0x000F;
public const uint WM_ERASEBKGND = 0x0014;
public const uint WM_KEYDOWN = 0x0100;
public const uint WM_KEYUP = 0x0101;
public const uint WM_MOUSEMOVE = 0x0200;
public const uint WM_LBUTTONDOWN = 0x0201;
public const uint WM_LBUTTONUP = 0x0202;
public const uint WM_LBUTTONDBLCLK = 0x0203;
public const uint WM_NOTIFY = 0x4E;
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
internal class WndProcHooker
{
public delegate int WndProcCallback(IntPtr hwnd, uint msg,
uint wParam, int lParam, ref bool handled);
private static Dictionary<IntPtr, HookedProcInformation> hwndDict = new Dictionary<IntPtr, HookedProcInformation>();
private static Dictionary<Control, HookedProcInformation> ctlDict = new Dictionary<Control, HookedProcInformation>();
public static void HookWndProc(Control ctl,
WndProcCallback callback, uint msg)
{
HookedProcInformation hpi = null;
if (ctlDict.ContainsKey(ctl))
{
hpi = ctlDict[ctl];
}
else if (hwndDict.ContainsKey(ctl.Handle))
{
hpi = hwndDict[ctl.Handle];
}
if (hpi == null)
{
hpi = new HookedProcInformation(ctl,
new Win32.WndProc(WndProcHooker.WindowProc));
ctl.HandleCreated += new EventHandler(ctl_HandleCreated);
ctl.HandleDestroyed += new EventHandler(ctl_HandleDestroyed);
ctl.Disposed += new EventHandler(ctl_Disposed);
if (ctl.Handle != IntPtr.Zero)
{
hpi.SetHook();
}
}
if (ctl.Handle == IntPtr.Zero)
{
ctlDict[ctl] = hpi;
}
else
{
hwndDict[ctl.Handle] = hpi;
}
hpi.messageMap[msg] = callback;
}
static void ctl_Disposed(object sender, EventArgs e)
{
Control ctl = sender as Control;
if (ctlDict.ContainsKey(ctl))
{
ctlDict.Remove(ctl);
}
else
{
System.Diagnostics.Debug.Assert(false);
}
}
static void ctl_HandleDestroyed(object sender, EventArgs e)
{
Control ctl = sender as Control;
if (hwndDict.ContainsKey(ctl.Handle))
{
HookedProcInformation hpi = hwndDict[ctl.Handle];
UnhookWndProc(ctl, false);
}
else
{
System.Diagnostics.Debug.Assert(false);
}
}
static void ctl_HandleCreated(object sender, EventArgs e)
{
Control ctl = sender as Control;
if (ctlDict.ContainsKey(ctl))
{
HookedProcInformation hpi = ctlDict[ctl];
hwndDict[ctl.Handle] = hpi;
ctlDict.Remove(ctl);
hpi.SetHook();
}
else
{
System.Diagnostics.Debug.Assert(false);
}
}
private static int WindowProc(IntPtr hwnd, uint msg,
uint wParam, int lParam)
{
if (hwndDict.ContainsKey(hwnd))
{
HookedProcInformation hpi = hwndDict[hwnd];
if (hpi.messageMap.ContainsKey(msg))
{
WndProcCallback callback = hpi.messageMap[msg];
bool handled = false;
int retval = callback(hwnd, msg, wParam, lParam, ref handled);
if (handled)
return retval;
}
return hpi.CallOldWindowProc(hwnd, msg, wParam, lParam);
}
System.Diagnostics.Debug.Assert(false,
"WindowProc called for hwnd we don't know about");
return Win32.DefWindowProc(hwnd, msg, wParam, lParam);
}
public static void UnhookWndProc(Control ctl, uint msg)
{
HookedProcInformation hpi = null;
if (ctlDict.ContainsKey(ctl))
{
hpi = ctlDict[ctl];
}
else if (hwndDict.ContainsKey(ctl.Handle))
{
hpi = hwndDict[ctl.Handle];
}
if (hpi == null)
{
throw new ArgumentException("No hook exists for this control");
}
if (hpi.messageMap.ContainsKey(msg))
{
hpi.messageMap.Remove(msg);
}
else
{
throw new ArgumentException(String.Format(
"No hook exists for message ({0}) on this control",
msg));
}
}
public static void UnhookWndProc(Control ctl, bool disposing)
{
HookedProcInformation hpi = null;
if (ctlDict.ContainsKey(ctl))
{
hpi = ctlDict[ctl];
}
else if (hwndDict.ContainsKey(ctl.Handle))
{
hpi = hwndDict[ctl.Handle];
}
if (hpi == null)
{
throw new ArgumentException("No hook exists for this control");
}
if (ctlDict.ContainsKey(ctl) && disposing)
{
ctlDict.Remove(ctl);
}
if (hwndDict.ContainsKey(ctl.Handle))
{
hpi.Unhook();
hwndDict.Remove(ctl.Handle);
if (!disposing)
{
ctlDict[ctl] = hpi;
}
}
}
class HookedProcInformation
{
public Dictionary<uint, WndProcCallback> messageMap;
private IntPtr oldWndProc;
private Win32.WndProc newWndProc;
private Control control;
public HookedProcInformation(Control ctl, Win32.WndProc wndproc)
{
control = ctl;
newWndProc = wndproc;
messageMap = new Dictionary<uint, WndProcCallback>();
}
public void SetHook()
{
IntPtr hwnd = control.Handle;
if (hwnd == IntPtr.Zero)
{
throw new InvalidOperationException(
"Handle for control has not been created");
}
oldWndProc = Win32.SetWindowLong(hwnd, Win32.GWL_WNDPROC,
Marshal.GetFunctionPointerForDelegate(newWndProc));
}
public void Unhook()
{
IntPtr hwnd = control.Handle;
if (hwnd == IntPtr.Zero)
{
throw new InvalidOperationException(
"Handle for control has not been created");
}
Win32.SetWindowLong(hwnd, Win32.GWL_WNDPROC, oldWndProc);
}
public int CallOldWindowProc(IntPtr hwnd,
uint msg, uint wParam, int lParam)
{
return Win32.CallWindowProc(oldWndProc, hwnd, msg,
wParam, lParam);
}
}
}
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace karlovo
{
public partial class TextBoxCustom : TextBox
{
new public event EventHandler DoubleClick;
public TextBoxCustom()
: base()
{
InitializeComponent();
WndProcHooker.HookWndProc(this, new WndProcHooker.WndProcCallback(WindowMessageHandler), Win32.WM_LBUTTONDBLCLK);
}
private int WindowMessageHandler(IntPtr hwnd, uint msg, uint wParam,int lParam, ref bool handled)
{
OnMouseDoubleClick(EventArgs.Empty);
handled = true;
return 0;
}
protected void OnMouseDoubleClick(EventArgs e)
{
if (DoubleClick != null)
{
DoubleClick(this, e);
}
}
}
}
So,all u need to do is to put this code in your custom control and implements the event. Thanks!!!
|
|
|
|
|
What a Great Help!Thank you very much!
|
|
|
|
|
Hi all,
I have a UserControl that manages a lot of editable information (like text-boxes). Now I need to make a version of this control that has a different layout (from horizontal to vertical). I can expose the control, like this:
public TextBox MyText
{
get { return myText; }
set { myText = value; }
}
i can now manually change the location and size, but this does not give me a visual reposition of them at design time.
Is there a better way of doing this ?
Thanks John
|
|
|
|
|
Hi John
How about a property in the control that changes the layout based on the value i.e. myControl.Orientation = Orientation.Vertical where the Orientation.Vertical is an enum held within the control.
Regards
The FoZ
|
|
|
|
|
In my project , we are used Mysql for database ... our application developed in c# winform application . Now we are creating deployment package using Visual studio 2008 ... user can install all prerequisites in a single click ... so i want add mysql also in a package ... how can do this ... please help me ... give some steps for how can achieve this ....
Thankyou
|
|
|
|
|
If Visual Studio's installer builder allows you to execute commands in the installation, you can let the MySQL installer run in silent mode.
I use Inno Setup for installers, so I'm clueless myself. But it's an idea.
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. - Calvin (from Calvin and Hobbes)(The Indispensable Calvin and Hobbes, p105-3)
|
|
|
|
|
I would like to write a program to be able to move a maximized window over to another monitor.
Some posts specify that you should first un-maximize the window, then move and then re-maximize the window.
I did and it works fine, except... it's not really nice to see.
You actually see the resizing of the window and it's slow too.
Moving a non-maximized window is much faster than moving a maximized one.
Any ideas in how to move the window in a nice fashion?
Thus without visual un-maximizing the window?
Thanks for any idea.
|
|
|
|
|
mvermand wrote: Any ideas in how to move the window in a nice fashion?
Thus without visual un-maximizing the window?
Hide it, restore it to a normal windowstate, move it, maximize it and show it again
I are Troll
|
|
|
|
|
Great Idea, but do you know how to hide a window based on its handle?
|
|
|
|
|
|
Hi
I've created a number of user controls to use on forms. What i need to do now is get a enum of variables from the form onto the user controls.
I am trying to write the code in the user control that just looks for a Enum with a fixed name on the form.
Any ideas? The problem i want to be able to reuse these controls on any form aslong as the Enum is there.
Chris
|
|
|
|
|
You can access a user control's parent's properties using the Parent property of the UserControl. If you need to access something not part of the Form class you'll need to cast the property to the Form type, but be warned, this can hinder reusing the control.
|
|
|
|
|
Hi Tony
Thanks for the help. The Parents propertiy it not something i have come accross before. Looking at a few points of the web and msdn.
I've manged to get the name of the control that the user control is on but not access the properties.
Chris
|
|
|
|
|
Hi Chris
Have you thought about passing the information from the form to the user control via a property. As Tony mentioned, trying to access in the way you are proposing will cause difficulties in reusability. using a property will make it easier to pass information back to the form (if required).
Regards
The FoZ
|
|
|
|
|
Hi TheFoz
Yes I no this would be the easiest way but would require some code to be added the form everytime you used the control.
What i was trying to do was have have enum on the main form. When the user control is droped on the form for it o read the enum and add it to a property of the user control. This way i could have multiple copies of the user control on the form and one of the properties of the user control would allow me to select from the forms enum.
Chris
|
|
|
|
|
I see!
Using this code in one of my custom controls I changed the colour of the form to alice blue when the user hovered their mouse on it
((Form)this.Parent).BackColor = Color.AliceBlue;
Using that you should be able to access the enum on the form
regards
The FoZ
|
|
|
|
|
Hi
Thanks for that. That could be very usefull but it only gives you access to standard windows form properties.
Chris
|
|
|
|
|
i need a datagridview inside datagridview in windows application.
the grid should be something lik this in the below link.
Grid Sample
Any help will be appricated.
thanks
|
|
|
|
|
sagarrana wrote: the grid should be something lik this in the below link.
That's a drill-down grid, could be quite a challenge to build. Doesn't Telerik's[^] grid provide something like this?
I are Troll
|
|
|
|
|