|
Prohibiting multiple instances is not the problem neither is restoring the app if it is just minimized on the task bar but when its in the system tray and hidden i'm really pulling my hair out as to how to restore it.
Thanks in advance for any thoughts
|
|
|
|
|
Set WindowState to FormWindowState.Normal or FormWindowState.Maximize and, optionally, call Show to make sure that the form is visible (if you simply set Visible to false or call Hide on the form).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
Sorry my fault for being unclear. yes that would be the case if i just wanted to double click the icon and restore it but what im looking for is restoring the previous instance which is in the system tray when another instance is started.
|
|
|
|
|
Yes, and what I said still applies. What you need to do, though, is get an instance of the Form that is your main application window. Yes, the HWND may be 0, but there's still a Form instance.
This is where .NET Remoting comes in handy. Read Accessing Objects in Other Application Domains Using .NET Remoting[^].
You could also use COM monikers, but there's a lot of overhead involved with interoping the necessary COM interfaces and functions. .NET Remoting is much easier to implement and provides you better access to your managed application.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
Dionne wrote:
its in the system tray and hidden i'm really pulling my hair out as to how to restore it.
If you have the Handle to the window, you can call ShowWindow to cause your window to become visible. The P/Invoke signature looks like this:
const UInt32 SW_HIDE = 0 ;
const UInt32 SW_SHOWNORMAL = 1 ;
const UInt32 SW_NORMAL = 1 ;
const UInt32 SW_SHOWMINIMIZED = 2 ;
const UInt32 SW_SHOWMAXIMIZED = 3 ;
const UInt32 SW_MAXIMIZE = 3 ;
const UInt32 SW_SHOWNOACTIVATE = 4 ;
const UInt32 SW_SHOW = 5 ;
const UInt32 SW_MINIMIZE = 6 ;
const UInt32 SW_SHOWMINNOACTIVE = 7 ;
const UInt32 SW_SHOWNA = 8 ;
const UInt32 SW_RESTORE = 9 ;
const UInt32 SW_SHOWDEFAULT = 10 ;
const UInt32 SW_FORCEMINIMIZE = 11 ;
const UInt32 SW_MAX = 11 ;
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
- Nick Parker My Blog | My Articles
|
|
|
|
|
Thanks Nick thats very close but the only problem left is that the Handle that ShowWindow is looking for is the MainWindowHandle and when an app is minimized to the system tray and hidden the MainWindowHandle is 0.
|
|
|
|
|
Dionne wrote:
but the only problem left is that the Handle that ShowWindow is looking for is the MainWindowHandle and when an app is minimized to the system tray and hidden the MainWindowHandle is 0.
That doesn't make any sense, the Handle or HWND for the application doesn't get set to 0 when the application is minimized and hidden. You can prove this simply by the following, you will see the Handle is still there:
using System;
using System.Timers;
using System.Drawing;
using System.Windows.Forms;
public class test : System.Windows.Forms.Form
{
Button b;
System.Timers.Timer t;
public test()
{
InitializeComponents();
}
public void InitializeComponents()
{
b = new Button();
b.Text = "Hide Me";
b.Click += new EventHandler(bClick);
t = new System.Timers.Timer();
t.Elapsed += new ElapsedEventHandler(elapsed);
t.Interval = 3000;
t.Enabled = true;
this.Controls.AddRange(new Control[] {b});
}
private void bClick(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
this.Visible = false;
this.ShowInTaskbar = false;
}
private void elapsed(object source, ElapsedEventArgs e)
{
MessageBox.Show(this.Handle.ToString());
}
[STAThread]
public static void Main()
{
Application.Run(new test());
}
}
- Nick Parker My Blog | My Articles
|
|
|
|
|
Yes that makes perfect sense
[STAThread]
static void Main()
{
string strSystemMutex = Process.GetCurrentProcess().ProcessName;
bool firstInstance;
Mutex mut = new Mutex(true, strSystemMutex,out firstInstance);
if (firstInstance)
{
try
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
catch {}
finally
{
mut.ReleaseMutex();
}
}
else
{
try
{
Process[] processes =
Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
foreach (Process proc in processes)
{
if (proc.Id != Process.GetCurrentProcess().Id)
{
WinMSG.ShowWindow(proc.Handle,3);
System.Environment.Exit(0);
}
}
}
catch{}
}
}
try this your test and run a multiple instance you'll see what i mean. proc.Handle is not the right parameter and proc.MainWindowHandle is 0
|
|
|
|
|
You might want to take a look at this article[^] by Chris Anderson.
From my experience the next thing you'll want is to delegate commandline options to the running instance, and that's something that can be achieved really easily using the ApplicationFramework provided by Chris.
A few little caveats, though:
When using .NET 1.1 you'll have to modify the code to include the required typeFilterLevel property for the channel used or else you'll get security exceptions.
You should add an InitializeLifetimeService() override returning null or else you won't be able to activate your previous instance after 2 minutes.
If you plan to use remoting in your application yourself you should remove the channel property bindTo = "127.0.0.1" (all three in UserApplicationContext.cs) or you'll get strange errors when handling remote events.
Regards,
mav
|
|
|
|
|
hi,
now i am devloping an report using crystal report in my application. i need to print in preprinted sheet. i need to set fixed width for details section and after the fixed size then print goes to next new page. any one help to me.
thanks in advance
Have A Nice Day!
Murali.M
|
|
|
|
|
Hi,
I have a WindowsForm, which has a groupBox and has multiple CheckedListBox Controls. I want to clear all selected check items dynamically, instead of doing it individually. I want to get all the controls from the groupBox first, which also has other controls, then get the CheckedListBox controls and clearing it, if selected.
I wrote this code, but it is not working, its not coming out of the loop. Any help, will be really appreciated.
foreach(Control ctrl in this.groupBox1.Controls)
{
if(ctrl is CheckedListBox)
{
CheckedListBox cb=(CheckedListBox)ctrl;
for(int i=0; i<=(cb.CheckedItems.Count-1); i++)
{
cb.ClearSelected();
}
}
}
abhi
|
|
|
|
|
Hmm, that is rather strange. Here is a quick work around.
private void ClearSelected()
{
CheckedListBox clb = null;
foreach(Control c in grp.Controls)
{
if(c is CheckedListBox)
{
clb = c as CheckedListBox;
if(clb != null)
{
foreach(int index in clb.CheckedIndices)
clb.SetItemChecked(index, false);
}
}
}
}
- Nick Parker My Blog | My Articles
|
|
|
|
|
Consider that you might have nested controls, that a CheckedListBox may not be a direct child of a GroupBox . Consider a generic recursive loop like so:
void ClearSelected(ControlCollection controls)
{
if (controls == null || controls.Count == 0) return;
foreach (Control control in controls)
{
if (control is CheckedListBox) ((CheckedListBox)control).ClearSelected();
ClearSelected(control.Controls);
}
} This will encounter any CheckedListBox at any depth in the parent/child hierarchy of controls.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
ISA? How old is this machine?!
Anyway, you're going to have to write your own devices drivers, in C++, to communicate with the hardware. C# is NOT good at this part! You can use C# as a higher level controller, providing a user interface to the functions of the board.
Like leppie said, you'll need the Windows Driver Development Kit. Start here[^]!
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
hi!
simply can't find a way to get the drive roots in c#. would be great if you could help.
thanks
thomas
|
|
|
|
|
.NET Framework 2.0 will add a new class, System.IO.DriveInfo , which can do this easily.
For now with .NET Framework 1.x, there are several ways. One is to use WMI with the System.Management namespace like so:
using System;
using System.Management;
class Test
{
static void Main(string[] args)
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("select * from Win32_LogicalDisk");
ManagementObjectCollection objects = searcher.Get();
if (objects != null)
{
foreach (ManagementObject obj in objects)
Console.WriteLine(obj.Properties["DeviceID"].Value);
}
}
} You could also P/Invoke several native APIs, like GetLogicalDrives :
using System;
using System.Collections;
using System.Runtime.InteropServices;
class Test
{
static void Main(string[] args)
{
uint drives = GetLogicalDrives();
byte[] buffer = BitConverter.GetBytes(drives);
BitArray bits = new BitArray(buffer);
for (int i=0, j=65; i<bits.Count; i++, j++)
if (bits[i])
Console.WriteLine((char)j + ":");
}
[DllImport("kernel32.dll", SetLastError=true)]
static extern uint GetLogicalDrives();
} There's many ways, but I hope these two alternatives for .NET 1.x point you in the right direction.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
|
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
Does anybody know any example code of populating windows treeview control with hierarchical dataset?
I prefer C# but vb will also do it.
I found couple samples on this side, but one uses classes, and another delegate. Both seem overkill.
My hierarchy is 3 levels deep, and the number of rows are limited. I can afford to load the treeview at the form load time.
I have been putting together following pieces, but it failed to load the tree in the relationship I want.
Thanks for any help!
private void Form1_Load(object sender, System.EventArgs e)
{
string sSQL;
SqlCommand cmd = sqlCnPubs.CreateCommand();
cmd.CommandType=CommandType.Text ;
sSQL = "Select ID, Segment from Prod_Segments";
cmd.CommandText=sSQL;
SqlDataAdapter daSeg = new SqlDataAdapter();
daSeg.SelectCommand=cmd;
DataSet dsProd=new DataSet();
daSeg.Fill (dsProd, "Segments");
sSQL = "Select ID, Catagory, SegmentID from Prod_Catagories";
cmd.CommandText=sSQL;
SqlDataAdapter daCat = new SqlDataAdapter();
daCat.SelectCommand=cmd;
DataSet dsCatagories=new DataSet();
daCat.Fill (dsProd, "Catagories");
dsProd.Relations.Add("SegtoCat", dsProd.Tables["Segments"].Columns["ID"], dsProd.Tables["Catagories"].Columns["SegmentID"]);
foreach (DataRow rowSeg in dsProd.Tables["Segments"].Rows)
{
TreeNode nodeSeg = new TreeNode();
nodeSeg.Text=rowSeg["Segment"].ToString();
nodeSeg.ImageIndex=0;
tvwProd.Nodes.Add(nodeSeg);
foreach (DataRow rowCat in rowSeg.GetChildRows("SegtoCat"))
{
TreeNode nodeCat = new TreeNode();
nodeCat.Text=rowCat["Catagory"].ToString();
tvwProd.Nodes.Add(nodeCat);
}
}
|
|
|
|
|
Make sure that an enforced DataRelation or an assumed relationship exists in a DataTable within your DataSet , like ParentID -> ID. For the top-level rows, ParentID (in this example) should be NULL.
To begin, find the rows with a NULL ParentID using DataTable.Select('ParentID = NULL') . That gives you a DataRow[] array. For each DataRow you add a TreeNode to TreeView.Nodes and then pass DataRow.GetChildRows using a DataRelation (best way of doing it), or dynamically construct another WHERE clause to pass to DataTable.Select like I did above (except replacing NULL with the current row's ID).
In a recursive method you add a TreeNode for each row and repeat the steps above, passing them to the same method. This is recursion.
The way you're doing it is not recursive and will only work for a level or two. Besides, you shouldn't do this in the Form.Load event handler. This can be a very expensive call and the form will be delayed. Users expact fast results and will think your application has hung and will most likely try to kill it or just stop using it. Consider starting a new thread (and be sure to use Control.Invoke ) that does this so the form can load, optionally with user feedback like a ProgressBar while processing results.
Take a look at http://www.codeproject.com/script/comments/forums.asp?msg=745216&forumid=1649#xx745234xx[^] where I've explained this before in this forum.
Below is something similar to a method I used before to add TreeNode s to a TreeView safely. You should read the documentation for Control.Invoke in the .NET Framework SDK for more details:
private delegate int AddTreeNodeCallback(TreeNode node);
private void SafeAddTreeNode(TreeNodeCollection nodes, TreeNode node)
{
if (treeView1.InvokeRequired)
{
AddTreeNodeCallback cb = new AddTreeNodeCallback(nodes.Add);
treeView1.Invoke(cb, new object[] {node});
}
else nodes.Add(node);
} I really do recommend starting this in another thread and using this method to add the TreeNode . Since you can easily get the row count from your DataTable , you could easily update a ProgressBar , but you should use Control.Invoke still. Always use Control.Invoke when communicating with a control created on a different thread.
Finally, try to avoid refering to DataColumn s and DataRelation s by their string name. This is very expensive as many lookups are required. Use either an integer index or the actual DataColumn or DataRelation object, which is trivial when using a typed DataSet (from using xsd.exe or VS.NET to create a new DataSet schema, which is available when you right-click on a project and select Add->Add New Item->DataSet). You can also programmatically create one and refer to the two classes I mentioned above that you programmatically added to the named DataTable , which you add to a named DataSet . This results in much faster code.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
public ArrayList strline=new ArrayList();
strline.Add("I found a reason for me. To change who I used to be. A reason to start over new. And the reason is you.");
strline.Add("Anger dwells only in the bosom of fools - John F. Kennedy.");
I hoped this could be work. but it didn't.
I want to replace string[] with readed Line by Line in txt file.
and I know how to read line by line but string[] Length is fixed as initial.
so I tried apple ArrayList in string[] but it's not easy to me.
How can we treat this?
Help me please.
Image will remind us some feelings and if I can ask someone about a problem, that's not a bad life.
|
|
|
|
|
Try using System.Collections.Specialized.StringCollection instead. Otherwise you can just returned objects from the ArrayList to string manually, string myStr = (string)myArrayList[0];
Does that answer your question?
Judah Himango
|
|
|
|
|
Hello buddie,
I load one UserControl for example c1.ascx file using LoadControl() method into a placeholder everything is ok.
I load another one as c2.ascx using LoadControl() in the first one (c1.ascx) now all eventhandlers in c2 class except Page_Load() are disabled!!!
Why is that? an ASP.NET bug or something notable can solve it?
Thank u
- Den 2 Fly
---
"Art happens when you least expect it."
|
|
|
|
|
This is the C# forum. You should post your question here[^].
Charlie
if(!curlies){ return; }
|
|
|
|
|