|
I try to use this:
Process myPro = new Process();
myPro.StartInfo.FileName = @"notepad.exe";
myPro.Start();
myPro.WaitForInputIdle(1000);
SendKeys.SendWait("Hello!");
It's OK. I created this process "myPro", but how to get the process of the current active window?
can you give me the detailed code? Thanks.
|
|
|
|
|
imbiz wrote: can you give me the detailed code?
No. First of all, this isn't a "give me code" web site. People here ask specific questions, while others provide advice and suggestions.
Second, it depends on circumstances. In simple situations, the process your app launches, will become the active app with the topmost window, and hence get whatever SendKeys sends. But then, the user (or a second automation-oriented app, or any app that feels like it) may suddenly cause another window, maybe another app, to get focus. The safer way is to check using GetForeGroundWindow (which requires P/Invoke techniques), or to send a message using SendMessage, or to create real inter-process communication by adapting both applications.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Thank you! I have found the solution! I compared all the handles(Toint32) of processes with Getforegroundwindow's, but it will tell me there more than one window. I observed that just the first one is the real active window, so I use "break" to quit "foreach". It works! That's so strange.
The code:
Process mypro=null;
foreach (Process thisproc in Process.GetProcesses())
{
if (thisproc.MainWindowHandle.ToInt32() == GetForegroundWindow().ToInt32())
{
mypro = thisproc;
break;
}
}
try
{
SendKeys.Send(@"{ENTER}\begin{{}split{}}{ENTER} &= {ENTER} &= {ENTER}\end{{}split{}}{ENTER}");
}
catch (System.NullReferenceException e)
{
}
Now, if I press the hotkey Ctrl+Shift+A, it will send some strings or commands to any active window. I use this to input some fixed blocks which need to be repeated for many times.
modified on Saturday, June 5, 2010 3:47 AM
|
|
|
|
|
Hi
can anyone help with the following
i use filesystemwatcher to watch a folder and add a new entry into a listbox everytime a new file is created, there is a second listbox that duplicates whats in the first but with some code allows me to change the file name via a text box, works ok as long as the item in listbox2 is highlighted when i change text, however if the listbox updates while i'm in process of changing text it removes the highlight from the item i'm editing and crashes if i press enter to confirm new file name.
Ther error i get is no doubt due to it not knowing what index to change as none are highlighted.
So how can i get it to keep the file i am changing name of highlighted until i enter the new text.
private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
listBox1.Items.Add(e.FullPath);
listBox2.Items.Add(e.FullPath);
listBox1.SelectedIndex = listBox1.Items.Count - 1;
listBox1.SelectedIndex = -1;
listBox2.SelectedIndex = listBox2.Items.Count - 1;
listBox2.SelectedIndex = -1;
}
private void listBox1_Click(object sender, EventArgs e)
{
listBox2.SelectedIndex = listBox1.SelectedIndex;
}
private void listBox2_Click(object sender, EventArgs e)
{
int itemSelected = this.listBox2.SelectedIndex;
string itemText = this.listBox2.Items[itemSelected].ToString();
Rectangle r = this.listBox2.GetItemRectangle(itemSelected);
this.textBox1.Location = new System.Drawing.Point(r.X + 217, r.Y + 520);
this.textBox1.Size = new System.Drawing.Size(r.Width + 4, r.Height - 10);
this.textBox1.Visible = true;
this.textBox1.Text = itemText;
this.textBox1.Focus();
this.textBox1.SelectAll();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
this.listBox2.Items[this.listBox2.SelectedIndex] = this.textBox1.Text;
this.textBox1.Visible = false;
}
if (e.KeyChar == 27)
this.textBox1.Visible = false;
}
|
|
|
|
|
The solution is very simple.
Just store the selectedindex value in some global variable,
and keep listbox2's item selected while some new item is
added to it.
Track listbox2's CollectionChanged event and write code to make
the previously selected item (which is now not selected bcoz
new item is added to listbox2) selected using our global variable
in which we stored selectedindex of listbox2.
And ya update the global variable on following event.
private void listBox1_Click(object sender, EventArgs e)
{
listBox2.SelectedIndex = listBox1.SelectedIndex;
Global_Listbox2_selectedIndex = listBox1.SelectedIndex;
}
HTH
Jinal Desai - LIVE
Experience is mother of sage....
|
|
|
|
|
Thanks for reply, i've removed second list box as i dont think it has any use for what i want it to do.
i now use the textbox to change name of listbox1 selecteditem and i am now trying to rename the actual file with "file.move" but i'm having a problem with second part of "file.move"
private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
listBox1.Items.Add(e.FullPath);
listBox1.SelectedIndex = listBox1.Items.Count - 1;
listBox1.SelectedIndex = -1;
}
private void listBox1_Click(object sender, EventArgs e)
{
int itemSelected = this.listBox1.SelectedIndex;
string itemText = this.listBox1.Items[itemSelected].ToString();
Rectangle r = this.listBox1.GetItemRectangle(itemSelected);
this.textBox1.Location = new System.Drawing.Point(r.X + 217, r.Y + 520);
this.textBox1.Size = new System.Drawing.Size(r.Width + 4, r.Height - 10);
this.textBox1.Visible = true;
this.textBox1.Text = itemText;
this.textBox1.Focus();
this.textBox1.SelectAll();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
File.Move(Path.GetFullPath(listBox1.SelectedItem.ToString()), this.textBox1.Text.ToString()+".avi" );
this.listBox1.Items[this.listBox1.SelectedIndex] = this.textBox1.Text;
this.textBox1.Visible = false;
}
if (e.KeyChar == 27)
this.textBox1.Visible = false;
It renames the file ok but moves file to debug directory of my project instead of leaving it where it is and just renaming it
kenny
|
|
|
|
|
hi everone,
I want to change/apply new themes on controls of win form in C# VS 2005.
I s there any open source or any other dll ?
Thanks in adv.
|
|
|
|
|
How many times are you going to ask this question?
Nobody can answer it for you if you don't explain exactly what you want to change about the controls....
|
|
|
|
|
You've asked a similar question before.
There are some third party dll's available - see here.
My signature "sucks" today
|
|
|
|
|
He's asked a lot more times than once before...
|
|
|
|
|
How many times are you going to ask this question and ignore the answers you've been given?
|
|
|
|
|
Maybe he hasn't been given an answer that solves his particular problem, but then he ought to rephrase and elaborate on the question so that we can actually guess what he wants...
|
|
|
|
|
Yes, he has. He's also been asked question to elaborate if were not understanding his question, which he's never replied to either.
|
|
|
|
|
Dear friends,
i have some problems like
i need to create application , that "creates Database,Table,Views" and need to insert default values.
at first i have created script file which contains all the operation,
then i have Executed script with help of SMO, and everything was fine, but the SMO works only where SQL server components are installed.
is it possible to create the DB,Tables, View in any other way ??
Thank you
|
|
|
|
|
You can keep your scripts in .sql files and then execute them using ADO.Net through the code.
|
|
|
|
|
Thanks....for reply
i got info form [^]
|
|
|
|
|
Hi!!
You can execute them using the .NET Sql or OleDB connector. Something like this will work
System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection([ConnectionString]);
cnn.Open();
using (System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand([query], cnn))
{
Command.ExecuteNonQuery();
Command.Dispose();
cnn.Close();
cnn.Dispose();
}
Obviously if you are going to execute a bunch of query because you're going to create all the tables, insert all the defaults and such in the same process you should use transactions. Information about how to do that can be found in the MSDN, you only need to add a couple of lines more.
Hope this helps
|
|
|
|
|
I may be having dense programmer moment, but I just spent an hour debugging something that came down to a really unexpected (by me, at least) behavior with List<> vs. array. If you create a struct with a method that modifies a member, and invoke that method on an instance of the struct that resides within an array, the array element is modified as you would expect. But if you perform the identical operation on the struct that is contained in a List<structs>, the item in the List is NOT modified. The following program illustrates this...the effect is observable in VS 9 and 10.
Does this seem correct or reasonable? It certainly doesn't adhere to one of my favorite principles of language design: the principle of least surprise!
thanks, jim
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace TestByValue
{
class Program
{
static void Main(string[] args)
{
Foo[] aArray = new Foo[3];
aArray[0].Increment();
Console.WriteLine("aArray[0].x is {0} and should be {1}", aArray[0].x, 1);
Debug.Assert(aArray[0].x == 1);
List<Foo> aList = new List<Foo>();
aList.Add(new Foo());
aList[0].Increment();
Console.WriteLine("aList[0].x is {0} and should be {1}", aList[0].x, 1);
Debug.Assert(aList[0].x == 1);
}
struct Foo
{
public int x;
public void Increment()
{
x++;
}
}
}
}
|
|
|
|
|
Normally in C# most things are passed by reference so when you access the item in your list and call Increment on it you modify the actual object in the list.
However in C# structs are value types (like an Int for example) and when they are passed around what you actually get are copies. So if you had a list of Ints and you accessed them in your list you wouldn't get the actual object from the list, you just get a copy that you can do whatever you like with. If you modified the Int you'd have to actually set it back to the list to actually make any changes:
List<int> myList = new List<int>();
myList.Add(5);
myList.Add(2);
int number = myList[1];
number += 3;
myList[1] = number;
With a normal array you are actually accessing the items directly so in your case they get modified as expected, but with a generic list the array subscript operator is the same as calling a function like
int GetAtIndex(int i)
{
return theActualList[i];
}
And because the struct isn't passed by reference what you end up with is a copy of what's stored in the list and that's what you're actually modifying.
If you wanted it to work the same for both then you could simply change your struct for a class
http://msdn.microsoft.com/en-us/library/ms173109.aspx[^]
Well I hope that makes sense, it's rather late and I'm off to bed now
And I also hope I haven't made and blindingly obvious mistakes.
My current favourite quote is: Punch them in the face, see what happens!
-SK Genius
modified on Thursday, June 3, 2010 11:28 PM
|
|
|
|
|
Thanks, SK...that's very clear, though I do think that having anObject[i].Increment() behave differently according to whether anObject is an array of Foo vs. List<foo> when Foo is a struct is error prone. I actually think I would prefer that array indexing behaved the same way...that is, the indexer returns a copy. Now that I've had time to sleep on it, I remember that a few years ago I fought through creating an indexer for my own particular struct and came to realize that I had no choice but to return a copy...so I was getting the exact same semantics as occurs with List<foo>. Maybe I won't fall into the same trap again in a couple of years. But I wouldn't count on it!
cheers, jim
|
|
|
|
|
Hi,
I'm making an app that needs a simple English wordlist. of all words in the English dictionary. I'm making a text manipulation-related application.
I need a databse or even a simple wordlists in other formats (.txt,.pdf) will do
can anyone get it for me?
|
|
|
|
|
I have something like that in an article I wrote here. It only contains 3-10 letter words (approximately 128,000 words), but you can easily find 1-2, and 11-???. I searched google for scrabble words by character count.
The article here is Anagrams - A Word Game in C#[^].
With a simple google search, I found this page (scroll down to the bottom):
http://www.manythings.org/vocabulary/lists/l/[^]
You can google, too. Really.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Hey I got it1
I pasted the "Words" folder inside the Debug folder. Thanks
Now its working.
Still.. Don't you think putting them in a simple database will be more elegant (& maybe faster!!) ??
P.S: i found a small correction. It says "Prepare urself" even when 0 words are found.
Anyway nice easy to understand code!!
-- Modified Friday, June 4, 2010 3:33 AM
|
|
|
|
|
I have a simple word check program. I got lists of (167889) words from a Scrabble site somewhere.
There are two lists, one with and one without "naughty" words.
As text, they are about 2MB. As an Access database, they are about 40MB.
So I would distribute the text files, the user can then import them to the application.
|
|
|
|
|
|