I am still only just learning C# in Visual Studio and I am trying to make a simple text encryption application. My problem at the moment is that when I use the command:
File.WriteAllText(name, inputTextBox.Text);
(Where name is the name of the file chosen in a SaveFileDialog and inputTextBox.Text is the text in a textbox on the main form)
however the file is never actually created. I even tried to build the application and run it as administrator but nothing happened.
What's even weirder is when I opened File Explorer, in the Quick Access section where it shows recent files, all the files that were supposed to be created show up there but don't exist when I click "Open File Location" and if I just try to open them, notepad just tells me the file doesn't exist.
The files are also not in my recycle bin or anything.
Here's the rest of my code in case it's something wrong with that:
public Form1()
{
InitializeComponent();
}
private void saveButton_Click(object sender, EventArgs e)
{
saveDialog.ShowDialog();
}
private void saveDialog_FileOk(object sender, CancelEventArgs e)
{
string name = saveDialog.FileName;
File.WriteAllText(name, inputTextBox.Text);
}
And in case you're wondering saveDialog is already an element in my form so there's no problem with that.
Since in your posted code the initialization of the SaveFileDialog is missing, and you say in your comment that the Debugger doesn't halt in the event body I take the long shot to assume that the event is not registered appropriately.
Try to make sure that your class (minimally) looks like the following example:
public partial class Form1 : Form
{
SaveFileDialog saveDialog;
public Form1()
{
InitializeComponent();
// create instance of SaveFileDialog
saveDialog = new SaveFileDialog();
// registration of the event
saveDialog.FileOk += SaveDialog_FileOk;
}
private void saveButton_Click(object sender, EventArgs e)
{
saveDialog.ShowDialog();
}
private void saveDialog_FileOk(object sender, CancelEventArgs e)
{
string name = saveDialog.FileName;
File.WriteAllText(name, inputTextBox.Text);
}
}
If your problem still remains, then I will remove my answer
Related
I've got an OpenFileDialog in a Windows Form which runs absolutely fine, howevever, when I want to do the same within an Excel Add-In, it does not seem to do anything. Below is an extract of my code, however I am struggling to see where the issue is as the code is identical for both (text box, openFileDialog and button names are identical).
public partial class DashboardControl : UserControl
{
public DashboardControl()
{
InitializeComponent();
}
private void DashboardControl_Load(object sender, EventArgs e)
{
}
private void fileLocationText_TextChanged(object sender, EventArgs e)
{
}
private void openFile_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
fileLocationText.Text = openFileDialog1.FileName;
}
}
Looks like the dialog is shown behind the Excel window. You need to set a parent window hanlde for the dialog ro bring it to the front. See SetForegroundWindow functin which brings the thread that created the specified window into the foreground and activates the window. Keyboard input is directed to the window, and various visual cues are changed for the user. The system assigns a slightly higher priority to the thread that created the foreground window than it does to other threads.
Note, the Show and ShowDialog methods of the System.Windows.Forms.Form class accepts an instance of the IWin32Window interface which allows to specify the parent window handle.
I am new to programming and stuck on a little thing. I have a button on my Windows application and I want to open Notepad when I click the button. I used all the available codes from internet starting from process.start() to even envirnoment.path but the button doesn't show the Notepad. Here is what I have already tried.
private void btnNotepad_Click(object sender, EventArgs e)
{
string notepadPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "notepad.exe");
System.Diagnostics.Process.Start(notepadPath);
}
Or simply:
system.diagnostics.process.start(#"notepad.exe");
Also did this:
string theData = txtbxRepeat.Text;
FileStream aFile = new FileStream("myTextFile.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(aFile);
txtbxRepeat.Text = theData;
sw.WriteLine(theData);
sw.Close();
Please help me in this.
You are heading in the correct direction with the first and 2nd code snippet. However, you need to specify the full path to the notepad++ exe.
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "Notepad++", #"notepad++.exe"));
}
However, keep in mind the user may have installed notepad++ in a different directory (for example they don't have an x86 directory).
UPDATED: updated to include environment paths rather than hard-coded path.
I am trying to simply add a webbrowser control to a window and then have it open up a page. I tried a web URL as well as a local HTML file to no avail. Here is my code:
namespace qTab1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
FileStream source = new FileStream("index.html", FileMode.Open, FileAccess.Read);
webBrowser1.DocumentStream = source;
//// When the form loads, open this web page.
//webBrowser1.Navigate("www.google.com");
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
// Set text while the page has not yet loaded.
this.Text = "Navigating";
}
private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
// Better use the e parameter to get the url.
// ... This makes the method more generic and reusable.
this.Text = e.Url.ToString() + " loaded";
}
}
}
This is my project at the moment:
What am I doing wrong?
The reason this happens is that when you press Debug or build your project any other way the root directory is the directory of the executable (so it would be - ./bin/Debug), not the directory of the project.
To fix this, you can do the following:
Right click the html file, click Properties and set the variable "Copy to output directory" to Copy always. That way, the html file will get copied with your executable.
Now you have to load the local file into the WebBrowser control. The following should work :
webBrowser1.Url = new Uri("file:///" + Directory.GetCurrentDirectory() + "/index.html");
Is there an easy way to show an dialog when the program is started for the first time (and only the first time), for some kind of instruction or specifying settings?
You could save it as a bool in your settings and you should check at load event of first form.
Your settings file should have a setting that I called "FirstRun" do this with following steps:
Right click your Project
Click "Properties"
Click "Settings" tabpage(probably on the left)
Add setting like I did as seen in image above
Note: The Scope can be changed to "Application", if that is your application's need, since you didn't mention in your question.
Your Settings file should look like image below:
public void Form1_Load(object sender, EventArgs e)
{
if((bool)Properties.Settings.Default["FirstRun"] == true)
{
//First application run
//Update setting
Properties.Settings.Default["FirstRun"] = false;
//Save setting
Properties.Settings.Default.Save();
//Create new instance of Dialog you want to show
FirstDialogForm fdf = new FirstDialogForm();
//Show the dialog
fdf.ShowDialog();
}
else
{
//Not first time of running application.
}
}
Note: wrote this from my phone, so I couldn't compile to test
Edit: Checked code and added image from desktop.
You can have bool value in your settings file which is a "user setting" which means you can change it to true save it for this specific user.
When your application starts just check that value. If it's false show your dialog and change it to true and it will stay true.
public void Form_Load(object sender, EventArgs e)
{
if(Settings.Default.ShowDialog)
{
Settings.Default.ShowDialog = false;
Settings.Default.Save();
// show first disalog
}
// rest of code if needed
}
Here's an MSDN link on user settings:
http://msdn.microsoft.com/en-us/library/bb397750(v=vs.110).aspx
Ok, so I assume you're creating WinForms application. First of all, locate the Load event in your main Form event lists (or simply double click your Form in Designer panel). The following method stub will pop up:
public void Form1_Load(object sender, EventArgs e)
{
}
And modify it like this:
public void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("Your message here");
}
I have a VSTO Project (WinWord addin) using c#.
The Project has only one window, it must be showed when click on some button, the problem is that it only happens when i'm debuging, if i try to use it after run the installation, it doesn't show the window. Here is my ribbon code:
public partial class MyRibbon
{
private void MyRibbon_Load(object sender, RibbonUIEventArgs e)
{
}
private void btnPublicar_Click(object sender, RibbonControlEventArgs e)
{
MyForm form = new MyForm();
//form.TopLevel = true;
form.ShowDialog();
//form.Show();
}
private void gallery1_Click(object sender, RibbonControlEventArgs e)
{
}
private void editBox1_TextChanged(object sender, RibbonControlEventArgs e)
{
}
}
The commented code are some tries. Any help tks.
After very deep debug, found my addin were throwing an non handeled exception (some XML files needed by addin are lost. They weren't where they should), the extrange thing is that Word doesn't show the problem, simply doesn´t open the form.