Saving textBox input to .txt file without use of SaveFileDialog - c#

I want to eliminate the need for SaveFileDialog and just add the ability for a user to save the input onClick without having to go through the SaveFileDialog. The filename will be set by txtModuleName.Textand the location/directory and file type will always remain the same.
string Saved_Module = "";
SaveFileDialog saveFD = new SaveFileDialog();
saveFD.InitialDirectory = "C:/Modules/";
saveFD.Title = "Save your file as...";
saveFD.FileName = txtModuleName.Text;
saveFD.Filter = "Text (*.txt)|*.txt|All Files(*.*)|*.*";
if (saveFD.ShowDialog() != DialogResult.Cancel)
{
Saved_Module = saveFD.FileName;
RichTextBox allrtb = new RichTextBox();
// This one add new lines using the "\n" every time you add a rich text box
allrtb.AppendText(txtModuleName.Text + "\n" + ModuleDueDate.Text + "\n" + txtModuleInfo.Text + "\n" + txtModuleLO.Text);
allrtb.SaveFile(Saved_Module, RichTextBoxStreamType.PlainText);
RE-ATTACK - How do I replace the use of the Dialog box when saving with a simple button that says 'Save' which OnClick uses the txtModuleName.Text as its name and saves it to a set directory C:/Modules/ in the form of a .txt file.
Thanks in advance!

You need to provide the full path and filename to RichTextBox.SaveFile instead of using SaveFileDialog.FileName. Add using System.IO;, and then use something similar to this:
private void button1_Click(object sender, EventArgs e)
{
String Saved_Module = Path.Combine("C:\\Module", txtModuleName.Text);
allrtb.SaveFile(Saved_Module, RichTextBoxStreamType.PlainText);
}

Related

How to save file in C# on RichTextBox with keyboard shorcut

I am writing the code editor in C# and i want to make the saving with keyboard shorcut CTRL + S this is my saving file code
private void saveToolStripMenuItem1_Click(object sender, EventArgs e)
{
var di = new SaveFileDialog();
di.Filter = "Text Files|*.txt";
di.FileName = "Txt" + ".txt";
var result = di.ShowDialog();
if (result == DialogResult.OK)
{
File.WriteAllText(di.FileName, textBox1.Text);
}
}
I want the tip how to solve it or piece of code that will work.
Select the Save menu strip item:
Open the properties window and set the keyboard shortcut:
Note that your code is only saving the rich text (rich = formatted) as plain text, so your file will not have that formatting. If you want to save as rich text, use
richTextBox1.SaveFile("whatever.rtf");

Saving File with selectable extensions

So I made code again about saving files (you can select file name)
here:
private void button3_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
using (Stream s = File.Open(saveFileDialog1.FileName, FileMode.CreateNew))
using (StreamWriter sw = new StreamWriter(s))
{
sw.Write(fastColoredTextBox1.Text);
}
}
}
the problem is I want to have 2 selectable extensions:
-.txt
-.md
because the code I wrote can save to any type of file(and if you didn't put anything I will save as . FILE) and I just want only 1 save file type.
Save dialog
You need to set the dialog's Filter property according to the pattern:
Text to show|Filter to apply|Text to show 2|Filter to apply 2|...
For example in your case, where you seem to be saying you want the save dialog to have a combo dropdown containing two things, one being Text and the other being Markdown, your Filter has 4 things:
.Filter = "Text files|*.txt|Markdown files|*.md";
It is typical to put the filter you will apply into the text you will show too, so the user can see what you mean by e.g. "Text files" but I've left that out for clarity.
For example you might choose to make it say Text files(*.txt)|*.txt - the text in the parentheses has no bearing on the way the filter works, it's literally just shown to the user, but it tells them "files with a .txt extension"
Footnote, you may have to add some code that detects the extension from the FileName if you're saving different content per what the user chose e.g.:
var t = Path.GetExtension(saveFileDialog.FileName);
if(t.Equals(".md", StringComparison.OrdinalIgnoreCase))
//save markdown
else
//save txt
You can just use the filter of your dialog to set the allowed extensions:
// Filter by allowed extensions
saveFileDialog1.Filter = "Allowed extensions|*.txt;*.md";

How do I use the variable of the selected folder?

When i select a folder manually, through FolderBrowserDialog, my need to substitute the path to the code below.
Where "%ProgramFiles (x86)%\MyApp" must be replaced with the variable of the selected folder + file name.
As a result, when selecting a folder, the file size "testFile.txt" in "label1" should be displayed (there will be two or more such files).
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
DialogResult result = folderBrowser.ShowDialog();
if (result == DialogResult.OK)
{
// Determine the size of the file in KB (dividing the number of bytes by 1024)
FileInfo fs1 = new FileInfo(Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%\\MyApp\\testFile.txt"));
long FileSize1 = fs1.Length / 1024;
label1.Text = "testFile.txt (" + Convert.ToString(FileSize1) + " KB)";
if (FileSize1 > 180 & FileSize1 < 186) // If the file is larger and smaller than the specified sizes
{
label1.ForeColor = Color.Green;
}
else
{
label1.ForeColor = Color.Red;
}
}
}
Decision, edit:
FileInfo fs1 = new FileInfo(folderBrowser.SelectedPath + "\\testFile.txt");
or
FileInfo fs1 = new FileInfo(Path.Combine(folderBrowser.SelectedPath, "testFile.txt"));
The official documentation has an item dedicated specifically to this: How to: Choose Folders with the Windows Forms FolderBrowserDialog Component
To choose folders with the FolderBrowserDialog component
In a procedure, check the FolderBrowserDialog component's DialogResult property to see how the dialog box was closed and get the
value of the FolderBrowserDialog component's SelectedPath property.
If you need to set the top-most folder that will appear within the tree view of the dialog box, set the RootFolder property, which takes
a member of the Environment.SpecialFolder enumeration.
Additionally, you can set the Description property, which specifies the text string that appears at the top of the
folder-browser tree view.
In the example below, the FolderBrowserDialog component is used to
select a folder, similar to when you create a project in Visual Studio
and are prompted to select a folder to save it in. In this example,
the folder name is then displayed in a TextBox control on the form. It
is a good idea to place the location in an editable area, such as a
TextBox control, so that users may edit their selection in case of
error or other issues. This example assumes a form with a
FolderBrowserDialog component and a TextBox control.
public void ChooseFolder()
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = folderBrowserDialog1.SelectedPath;
}
}
According to that example, you can get a string to the chosen folder using folderBrowser.SelectedPath.

How to add Save file Dialog Box using C#

I need to implement something similar to Notepads' save option. Assuming I have a button placed next to a RichTextBox, what I want is, when this button is clicked, a Dialogue box will open up, which will look similar to the one that appears when Save As is clicked. I would like to save the content of the RichTextBox in text format, by entering the name of file in the Save Dialogue box.
private void Save_As_Click(object sender, EventArgs e)
{
SaveFileDialog _SD = new SaveFileDialog();
_SD.Filter = "Text File (*.txt)|*.txt|Show All Files (*.*)|*.*";
_SD.FileName = "Untitled";
_SD.Title = "Save As";
if (__SD.ShowDialog() == DialogResult.OK)
{
RTBox1.SaveFile(__SD.FileName, RichTextBoxStreamType.UnicodePlainText);
}
}
For WPF you should use this SaveFileDialog.
var dialog = new Microsoft.Win32.SaveFileDialog();
dialog.Filter = "Rich Text File (*.rtf)|*.rtf|All Files (*.*)|*.*";
dialog.FileName = "Filename.rtf"; //set initial filename
if (dialog.ShowDialog() == true)
{
using (var stream = dialog.OpenFile())
{
var range = new TextRange(myRichTextBox.Document.ContentStart,
myRichTextBox.Document.ContentEnd);
range.Save(stream, DataFormats.Rtf);
}
}
This works for text files and was tested in WPF.
var dialog = new Microsoft.Win32.SaveFileDialog();
dialog.Filter = "Text documents (.txt)|*.txt|All Files (*.*)|*.*";
dialog.FileName = "Filename.txt";
if (dialog.ShowDialog() == true)
{
File.WriteAllText(dialog.FileName, MyTextBox.Text);
}
SaveFileDialog sfDialog = new SaveFileDialog();
sfDialog.ShowDialog();
OutputStream ostream = new FileOutputStream(new File(sfDialog.FileName));
WorkBook.write(ostream);
ostream.close();
misread the question - Ray's answer is valid for OP
This works only in Windows Forms.
You should take a look at the SaveFileDialog class: http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx
And save the file using something like this (see here):
rtf.SaveFile(dialog.FileName);
There is a SaveFileDialog component which you can use, read here to find out how it works and a working sample.

Displaying multiple images with image control in asp.net 3.5

I want to display all the images from given directory. For that i have given image controls to display images, but i want to display images control with that url automatically as per images in that directory. Suppose in my directory 5 images are present then 5 image controls should be display in my button click event.
I have written code in button_click event that displays only two images from the given directory is as follows :
protected void btncompare_Click(object sender, EventArgs e)
{
Bitmap searchImage;
searchImage = new Bitmap(#"D:\kc\ImageCompare\Images\img579.jpg");
string dir = "D:\\kc\\ImageCompare\\Images";
DirectoryInfo dir1 = new DirectoryInfo(dir);
FileInfo[] files = null;
files = dir1.GetFiles("*.jpg");
double sim;
foreach (FileInfo f in files)
{
sim = Math.Round(GetDifferentPercentageSneller(searchImage, new Bitmap(f.FullName)), 3);
if (sim >= 0.95)
{
string imgPath = "Images/" + files[0];
string imgPath1 = "Images/" + files[1];
Image1.ImageUrl = "~/" + imgPath;
Image2.ImageUrl = "~/" + imgPath1;
Response.Write("Perfect match with Percentage" + " " + sim + " " + f);
Response.Write("</br>");
}
else
{
Response.Write("Not matched" + sim);
Response.Write("</br>");
}
}
}
Seems like you're trying to fit a square peg into a round hole here. The Image control is designed to show a single image.
If you want to display multiple images, use multiple image controls. I suspect that the reason you're not doing this is because you don't know how many images that you're going to need to display.
If I had the same problem, I would turn the contents of the image directory into something that could be bound onto a Repeater. Each ItemTemplate would contain its own Image control, allowing you to display as many images as you need without resorting to hackery.

Categories

Resources