|
what is the code in c# in mouse move event to display the current mouse position in form?
|
|
|
|
|
You can try MouseHover [^] or MouseMove [^].
|
|
|
|
|
To get the position you just use the MouseEventArgs X and Y values.
Using the standard visual studio format your code would look like this for a label called label1 that moved with the mouse.
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
label1.Location = new Point(e.X, e.Y);
}
From here
|
|
|
|
|
Try:
Point mouseAt = PointToClient(Cursor.Position);
Console.WriteLine(mouseAt);
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
I have a program that needs to run at startup for each user on the pc and it reads a file that was made using binary serialization. The file just sits in the program folder but the path that comes up (I used Application.StartupPath) points me to the user folder of the active user if I am not mistaken. So I thought I would access the registry entry that I put in order for it to start up at startup but I get access denied. I tried setting an environment variable, but it requires admin privileges to access the variables. I must be going about this the wrong way. Could someone please point me in the right direction.
|
|
|
|
|
bfis108137 wrote: The file just sits in the program folder Can you give us the full path? Also from the serialized file you're reading?
bfis108137 wrote: (I used Application.StartupPath) points me to the user folder of the active user if I am not mistaken From MSDN[^]: "Gets the path for the executable file that started the application, not including the executable name."
bfis108137 wrote: So I thought I would access the registry entry that I put in order for it to start up at startup but I get access denied. So the app is started from the registry ("Windows/CurrentVersion/Run"), and not from, say, the start-menu folder "startup" for all users? Is it pointing to the full path?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
The path I tried was in 2 places. First I made a folder in the c root. Then I thought it had to do with permissions so I used the public folder. This is c:\users\public on win7/vista and it's c:\documents and settings\All Users in xp. Like I said I used Application.StartupPath and it works but only when I start it myself. When it runs automatically at startup, "Application.StartupPath" points to c:\users\current user. With all that being said I found a solution. I add an environment variable which does require elevation but it's a one time thing when the app is first installed which i use a separate app to do(which i call from the main app) so as to not require the higher privilege all the time. That much I had working before. Where I got stuck was trying to retrieve the variable. It would require elevation as well which was unacceptable. However I found a solution.
I found this on another site but since I know how some sites are with links I am not going to post the link. Here is the code I used to retrieve an environment variable without elevation.
string keyName = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment\";
string path = (string)Registry.LocalMachine.OpenSubKey(keyName).GetValue("variablename", "", RegistryValueOptions.DoNotExpandEnvironmentNames);
|
|
|
|
|
bfis108137 wrote: Like I said I used Application.StartupPath and it works but only when I start it
myself. When it runs automatically at startup, "Application.StartupPath" points
to c:\users\current user.
No it doesn't. Not unless your .EXE is in that path and was launched from there. I'll guarantee it.
bfis108137 wrote: Where I got stuck was trying to retrieve the variable. It would require
elevation as well which was unacceptable.
You do not need elevated priv's to get an environment variable. You only need them to create a system-wide, or machine, environment variable.
Everything you're telling us is backwards from how it actually works. There's something you're not telling us about this code. Since we can't see it, we can't tell you why it's not working.
|
|
|
|
|
Well it was a machine environment variable since the path was going to be the same across all users. And you are right Application.StartupPath does work correctly but here is where the problems started. I was using a dll. I wasn't using Application.StartupPath. I was just trying to get information out of a file so I wasn't even using a path. I was just doing something like
string someString = File.ReadAllText("FileName.txt");
When it would run normally (not at startup) then it would read and write to the folder where the app was. If it was run at startup however, it would read or write to the user's folder. I misspoke myself about the Application.StartupPath. I have been doing a lot of debugging and apparently that wasn't what happened but I assure you the issue I just presented did happen. The file with a dll is in c:\test. Here is the code. Now the first time it's run as expected there is a file called error.txt which says "Could not find file 'C:\test\test.txt'." The path.txt file and the test.txt files are as expected. However when it runs at startup, the time in the test.txt file doesn't change and when I look in the user folder, I see a new error.txt file along with a test.txt file. The error.txt file says "Could not find file 'C:\Documents and Settings\User\test.txt'." and the time in the test.txt file is more up to date. So again I misspoke before and I assure you it was accidental but and I have found a way to solve my problem but this was my situation. I hope you accept my apology.
A button and form load with the following code.
private void Form1_Load(object sender, EventArgs e)
{
TestClass tc = new TestClass();
tc.ReadFile();
tc.WriteFile();
}
private void button1_Click(object sender, EventArgs e)
{
string path = Application.StartupPath + @"\CurrentFolderTest.exe";
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
File.WriteAllText("path.txt", Application.StartupPath);
key.SetValue("foldertest", path);
}
Here is the code from the dll
public void WriteFile()
{
File.WriteAllText("test.txt", DateTime.Now.ToLongTimeString() + DateTime.Now.Millisecond.ToString());
}
public void ReadFile()
{
try
{
string test = File.ReadAllText("test.txt");
}
catch (Exception ex)
{
File.WriteAllText("error.txt", ex.Message);
}
}
|
|
|
|
|
That's better.
Two problems. First, normal users can't write to the Localmachine registry hive. Writing this key is something you would do at install time, not run time.
Second, whenever you read or write files, it is best practice to always build a fully qualified path to the file using a well known folder, like CommonAppData. Use Environment.GetFolderPath get the base folder path and the use Path.Combine to build your fully qualified file path from it.
Like you already found, the base folder paths between Windows XP and 7 are different. They can even change between 32 and 64 bit versions of Windows.
modified 10-Jun-13 14:15pm.
|
|
|
|
|
hi i just try to make a crystal report in visual studio 2008 using C#, OLE DB(ADO) and the connection was successfully made. but every time when i start to run the application it asks for a login USER NAME and PASSWORD but my database is not secured with any password here is my pies of code to view in CrystalViewer
ReportDocument cryRpt = new ReportDocument();
cryRpt.Load("CrystalReport1.rpt");
crystalReportViewer1.ReportSource = cryRpt;
crystalReportViewer1.Refresh();
zakirox123
modified 9-Jun-13 3:15am.
|
|
|
|
|
Please do not post the same question in QA and the forums - it duplicates work and annoys people.
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
sorry im totally confused thats why i did so?
|
|
|
|
|
Here[^] is a nice article that might help you.
|
|
|
|
|
Hi All,
Can anyone help me how to convert Coordinate from SAD_1969_UTM_Zone_23S to WGS84 using C#.
Ex :-
SAD_1969_UTM_Zone_23S coordiantes
Easting : 1472435.72
Northing : 9484899.68
WGS84 coordinates
lon : -44.0631144
lat : -19.9752943
Thanks & Regards,
Subbarao
|
|
|
|
|
Can you give us the definition of that format?
|
|
|
|
|
I am having datagridview in c#
I populated data inside datagridview.
I created Serial No inside datagridview along with other columns.
If i click serial no head,the serial no is sorted. If i do sorting in other columns,the serial no is also sorted.
How to avoid sorting serial no, if I click other column headers.
I have given the code below
dsstock = new DataSet();
dsstock = fetchDataSetValues(statement);
// grdStock.Rows.Clear();
if (dsstock.Tables[0].Rows.Count > 0)
{
grdStock.Columns.Add("Sl. No.", "Sl. No.");
grdStock.Columns[0].Width = 80;
grdStock.Columns.Add("ItemID", "Item ID");
grdStock.Columns[1].Width =300;
grdStock.Columns.Add("ItemName", "Item Name");
grdStock.Columns[2].Width = 600;
grdStock.Columns.Add("ItemQuantity", "Item Quantity");
grdStock.Columns[3].Width = 150;
int row = dsstock.Tables[0].Rows.Count - 1;
for (int r = 0; r <= row; r++)
{
grdStock.Rows.Add();
grdStock.Rows[r].Cells[0].Value = r + 1;
grdStock.Rows[r].Cells[1].Value = dsstock.Tables[0].Rows[r].ItemArray[0];
grdStock.Rows[r].Cells[2].Value = dsstock.Tables[0].Rows[r].ItemArray[1];
grdStock.Rows[r].Cells[3].Value = dsstock.Tables[0].Rows[r].ItemArray[2];
}
}
Thanks
Chandran
|
|
|
|
|
You can set a particular column sort to Programmatic or NotSortable .
For e.g. DataGridView1.Columns.Item(0).SortMode = DataGridViewColumnSortMode.Programmatic;
More about column sort modes here[^].
|
|
|
|
|
I have generated a product key of 16 digits for my windows application.But i don't know how to use this product key in the application so that whenever a user installs the application, he has to use this key at the time of installing the application.
Please suggest some solution.
AK Barai
|
|
|
|
|
|
How to validate a textbox in my windows form application using c#.
The controls for validation are disabled so i add Validator.dll from external source.But it is not working. Please suggest some solution. What would be the regular expression for validating a text box for a 21 length string with alphabets and digits at fixed places in it.
AK Barai
|
|
|
|
|
Firstly, the standard TextBox has a Validating event which is triggered when the user tries to leave the control - it may be that this is sufficient for your needs.
Secondly, we can't tell you a regex "for validating a text box for a 21 length string with alphabets and digits at fixed places in it" because we have no idea where the various alpha and numeric characters have to be!
The regex will vary considerably depending on exactly what format you require your user to enter.
Thirdly, it may be that you could use a MaskedTextBox instead - these can be set to limit what the user can enter where quite easily.
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
|
Hi everyone,
I am currently writing a program in C#. I would love it if I was able to press a button on the keyboard which would cause the program to stop, jump to another section of code, execute the code. Then jump back to where it left off. I understand you can do this with a condition flag (and poling) but coming from embedded programming background there should be a similar way to do this in C# ( i hope!).
If not, what is the best way to go about this.
I appreciate the help.
Thanks
|
|
|
|
|
If I understand what you're talking about, there is no similar construct in C# or .NET.
|
|
|
|