Hello I am working on c# simple project.
This is the code:
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Win32;
using System.IO;
private void SetStartup()
{
RegistryKey rk = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (chkStartUp.Checked)
rk.SetValue(AppName, Application.ExecutablePath);
else
rk.DeleteValue(AppName,false);
}
namespace web1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// open browser
Thread.Sleep(6000);
System.Diagnostics.Process.Start("https://google.com/");
}
}
}
It shows errors "The name "AppName" does not exist in the current context" and "The namespace cannot contain members such as fields or methods directly"
I want to:
If I click on this program .exe, it should run on the startup (every startup).
How to deal with it? I've tried so many ideas, but doesn't helped.
Thanks in advance.
BTW: it's my first time here, nice to meet you all!
Related
I'm trying to display a google map over winforms.
In my project I did : Tools > NuGET Package Manager > Manage NuGET packages for Solution... > Browse
type to search gmap and installed the top one version 2.1.7
then in form1 :
private void btnShowOnGoogleMaps_Click(object sender, EventArgs e)
{
GoogleMaps googleMaps = new GoogleMaps();
googleMaps.Show();
}
and in the GoogleMaps form :
using GMap.NET;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Weather
{
public partial class GoogleMaps : Form
{
public GoogleMaps()
{
InitializeComponent();
}
private void GoogleMaps_Load(object sender, EventArgs e)
{
gmap.MapProvider = GMap.NET.MapProviders.BingMapProvider.Instance;
GMap.NET.GMaps.Instance.Mode = GMap.NET.AccessMode.ServerOnly;
gmap.SetPositionByKeywords("Paris, France");
}
}
}
then when running the application and clicking the button to show the google maps i'm getting this :
Hello it is probably easy question for you, I'm a beginner and I'm making my own simple game and I want to use a Class:Gamer, which I want to initialize in MainWindow(Form1.cs) from a save file. From then, I want to use it on another Forms aswell, but somehow I can't make the instance go public.
Could you tell me what I'm doing wrong? Or is there another way how to solve this?
Thank you :)
Code on Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Text;
using System.IO;
namespace THE_GAME
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static Gamer Player;
private void MainWindow_Load(object sender, EventArgs e)
{
//load from savefile lvl;hp;money;gun;armor,name
string allData = File.ReadAllText("../../saveFile/save.txt");
string[] dataFromSave = new string[5];
dataFromSave = allData.Split(';');
Player = new Gamer(dataFromSave[0], dataFromSave[1], dataFromSave[2], dataFromSave[3], dataFromSave[4], dataFromSave[5]);
}
}
}
Code on secondForm2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Text;
namespace THE_GAME
{
public partial class Statistics : Form1
{
public Statistics()
{
InitializeComponent();
}
private void Statistics_Load(object sender, EventArgs e)
{
//labels stats
labelName.Text = Form1.Player.GetName();
labelHealth.Text = Form1.Player.GetHealth().ToString();
labelMoney.Text = Form1.Player.GetMoney().ToString();
}
private void buttonBack_Click(object sender, EventArgs e)
{
MainMenu menu = new MainMenu();
menu.Show();
this.Close();
}
}
}
Thank you for your time.
To get at the Gamers Player object from a different Form just do
Form1.Player;
ie
var nam = Form1.Player.Name;
Form1.Player.Die();
etc
PS As I said in a comment - its extremely odd to dereive a form of yours from another one of your forms. Like this
public partial class Statistics : Form1
This question already has answers here:
Why does Property Set throw StackOverflow exception?
(3 answers)
Closed 2 years ago.
I'm using C# to develop a windows forms application and I required to store certain values (Ex: UserID and Role), in order to use them again in various forms throughout the application.
The User ID and the Role will be changed with each login.
So tried using static classes.
To test it out first, I did the following.
Created "Form1" with a textbox and a button.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnDisplay_Click(object sender, EventArgs e)
{
common.text = textBox1.Text;
Form2 obj = new Form2();
obj.Show();
}
}
}
Then created "Form 2" with only a label.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
label1.Text = common.text;
}
}
}
And to interconnect these two forms, created the following class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication3
{
public static class common
{
public static string text
{
get
{
return text;
}
set
{
text = value;
}
}
}
}
The purpose was to see if the label text on form2 would change when clicked on the button after entering text into the textbox in form1.
When running the code, the following error was thrown. Displays that this is thrown from the "set" method of the class.
An unhandled exception of type 'System.StackOverflowException' occurred in WindowsFormsApplication3.exe
If anyone could provide any clarity on this, it would be highly appreciated.
Thanks in advance.
Your set method is calling itself.
You need to add private string and change it and then return the changes via get.
Try this:
private static string _text;
public static string text
{
get
{
return _text;
}
set
{
_text = value;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
namespace Garfield
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
download("https://d1ejxu6vysztl5.cloudfront.net/comics/garfield/2019/2019-11-25.gif?v=1.1", "e.gif");
}
public void download(string link, string name)
{
using (WebClient Client = new WebClient())
{
Client.DownloadFile(link, name);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
Using the code above I downloaded an image online, now how do I set the location of that file to somewhere else instead of it being in the debug folder?
This can be done through mentioning a valid desired location and name when download.
Client.DownloadFile("https://d1ejxu6vysztl5.cloudfront.net/comics/garfield/2019/2019-11-25.gif?v=1.1",
#"C:\yourfolder\yourfilename.png");
You can pass an entire path to your download method:
download("https://d1ejxu6vysztl5.cloudfront.net/comics/garfield/2019/2019-11-25.gif?v=1.1", "C:\\Users\\(You)\\Documents\\e.gif");
Just replace everything before "e.gif" with the path you desire.
See more here: https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.downloadfile?view=netframework-4.8
Why some of the applications work with waitforexit but some doesn't work like notepad works fine bit dcomcnfg.exe doesn't what is the reason
Has edited the app.manifest to run this application with admin right
Application is debuged for any cpu
My code is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace prchk
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Method();
}
public void Method()
{
Process prcs = new Process();
prcs.StartInfo.FileName = #"C:\Windows\System32\dcomcnfg.exe";
//p.StartInfo.Arguments = argu;
prcs.StartInfo.UseShellExecute = true;
prcs.Start();
prcs.WaitForExit();
MessageBox.Show("Exited");
}
}
}
Here exited is showing before the application is exited
What i tried:
1> Has tried hasexited
2> Has tried useshellexecution = false
Is there any way to wait for the application to exit for this kind of applications?