C# - Save a Multi-Line Textbox to a text file - c#

I am creating a small app for personal use that allows me to clean my lists of objects. I am using a variety of filters to get a finalized list in a multiline text box. When i am finished, I use the following code to copy to the textbox to the clipboard.
#region COPY BUTTON
private void button3_Click(object sender, EventArgs e)
{
Clipboard.SetText(textBox_ListDestination.Text);
}
#endregion
What i would like to do now is add another button that allows me to save this same text to a .txt file using the SaveFileDialog. Can anyone help me with this? I am assuming I would use Streaming of some type, but I am out of my element here. Any help would be appreciated.

try
File.WriteAllText (TargetFilePath, textBox_ListDestination.Text);
For more information including sample code see MSDN.
If you want to obtain TargetFilePath via a SaveFileDialog see MSDN.
UPDATE
Sample code using SaveFileDialog:
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
File.WriteAllText (saveFileDialog1.FileName, textBox_ListDestination.Text);
}

Related

Is it possible to copy selected items from Datagrideview to clipbord c#

I want to copy the selected items in a datagridview to the clipbord, Here is what i have tried.
private void button3_Click(object sender, EventArgs e)
{
Clipboard.SetText(dataGridView1.SelectedRows);
}
From This Article I'm sure you can achieve what it is you want. I'll give you an example and have you do some small changes if you need.
In your Form_Load event add this line of code this.dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText; This enables users to copy multiple cells.
In your Button_Click event you're going to want to add the code below:
if (this.dataGridView1
.GetCellCount(DataGridViewElementStates.Selected) > 0)
{
try
{
// Add the selection to the clipboard.
Clipboard.SetDataObject(
this.dataGridView1.GetClipboardContent());
}
catch (System.Runtime.InteropServices.ExternalException)
{
MessageBox.Show("Error");
}
}
That should copy all the selected cells to the clipboard. Again, I got all this code directly from Microsofts Doc on how to do this.
Yes, You can copy it. You either can do it manually or by coding
Get(this.dataGridViewEmployee, new DataGridViewCellEventArgs(this.dataGridViewEmployee.CurrentCell.ColumnIndex, this.dataGridViewEmployee.CurrentRow.Index));
This is the code that I used for one of my projects. Hope this works with you

Upload a file in a directory using C# allow to view it

I am new to C#. How can i possible upload a file like pdf and image in a particular folder using C# windows gui? Do i need to add library for it to work in viewing it directly to the gui program? Also, i need to allow the user to upload multiple files. Thanks in advance !
If you want to browse and display picture inside a picture box on your windows form you can try something like this:
Design of the form:
Code:
public PictureDisplay()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog newDialog = new OpenFileDialog();
if (newDialog.ShowDialog() == DialogResult.OK)
{
try
{
picBoxDisplay.Load(newDialog.FileName);
picBoxDisplay.SizeMode = PictureBoxSizeMode.Zoom;
}
catch
{
MessageBox.Show("Wrong file type!", "Error");
}
}
}
Code opens file dialog and if you choose a file of image type it's displayed to fit into a picture box while maintaining resolution ratio. If any other type is chosen MessageBox pops and makes you aware of an error.
Results:
Hopefully it covers the image upload to GUI part. For any further help, please explain the question better, thanks!
Open file png/.
http://msdn.microsoft.com/ru-ru/library/system.windows.forms.openfiledialog(v=vs.110).aspx
View Pictures
http://msdn.microsoft.com/ru-ru/library/system.windows.forms.picturebox.aspx

Remove table definition from RichTextBox in WPF?

I have a RichTextBox control in my view. I'm using code-behind (UI logic only) to format the RTF within my RichTextBox which is working from a 'Format' button click event which instantiates a TextRange:
private void _btnFormat_Click(object sender, RoutedEventArgs e)
{
TextRange rangeOfText = new TextRange(richTextBoxArticleBody.Document.ContentStart, richTextBoxArticleBody.Document.ContentEnd);
rangeOfText.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);
rangeOfText.ApplyPropertyValue(TextElement.FontSizeProperty, "12");
rangeOfText.ApplyPropertyValue(TextElement.FontFamilyProperty, "Arial");
rangeOfText.ApplyPropertyValue(TextElement.FontStyleProperty, "Normal");
rangeOfText.ApplyPropertyValue(Inline.TextDecorationsProperty, null);
}
I want to also remove any tables within the RTF. Can I use the same approach maybe from the Table class to remove tables from my RichTextBox? Thanks
You'll have to climb down the Blocks and get the descendants
of the FlowDocument and get all the Tables and then remove them from the its Parent.
Ok, if anyone is trying to achieve this I don't think it's possible. Maybe you can iterate over a simple table in an Rtf string and remove the tags but if you can't determine user input the Rtf is by far too complex. Therefore here's my solution (of sorts...)
private void _btnFormat_Click(object sender, RoutedEventArgs e)
{
TextRange rangeOfText = new TextRange(richTextBoxArticleBody.Document.ContentStart, richTextBoxArticleBody.Document.ContentEnd);
rangeOfText.ApplyPropertyValue(Table.BorderThicknessProperty, "3");
rangeOfText.ApplyPropertyValue(Table.BorderBrushProperty, Brushes.Red);
}
In the 'Format' button click event I've set table borders to Red. On my save back to the database method I've used this simple if statement:
private void SaveToDbCommandAction()
{
if(PastedText.Contains("trowd"))
{
Xceed.Wpf.Toolkit.MessageBox.Show("Cannot save Article. Please remove pasted tables");
}
else
{
SaveToDb(RTBText);
}
}
Therefore when the user pastes in a table they are warned via the red cell borders. This is particularly useful if they paste a table with invisible borders and can't actually see the table. The If statement then determines whether the Rtf string contains a 'trowd' tag therefore preventing the save.

How to paste text to RichTextBox as a plain text

If I copy some text with different format and paste it to my richtextbox it is not plain I mean its format will be copied as well.
Is there anyway I can copy-paste as a plain text?
By the way my program is on WinForm
thanks for any answer
you must use WinForm RichTextBox (not in UI, just in code), even if you are on WPF, in order to convert RTF to plain text. Use this method in your Copy event.
C# code :
private String ConvertRtfToText()
{
System.Windows.Forms.RichTextBox rtfBox = new System.Windows.Forms.RichTextBox();
rtfBox.Rtf = this.rtfData;
return rtfBox.Text;
}
VB.Net Code :
Private Function ConvertRtfToText() As String
Dim rtfBox As RichTextBox = New RichTextBox()
rtfBox.Rtf = Me.rtfData
Return rtfBox.Text
End Function
source : http://msdn.microsoft.com/en-US/en-en/library/vstudio/cc488002.aspx
I recently had the same issue. I did want to retain some of the formatting, i.e. paragraphs and line feeds, but I required all the addition text format to be removed.
I'm working in WPF but the RichTextBox interface is the same. I have created a button that will allow users to select some text and remove the formatting. It is very simple, you just need to use the ClearAllProperties() method on the TextSelection object.
C# Code (WPF):
private void ClearFormat_Click(object sender, RoutedEventArgs e)
{
rtbText.Selection.ClearAllProperties();
}
This is a super easy solution but perhaps not super elegant...
1) Add a plain textbox to your form and make it hidden
2) Create a button to remove the formatting (or you can do this
automatically when the text is pasted)
3) In the OnClick (or OnPaste) code just copy the text from the rich
textbox control to the plain textbox control then copy the text
from the plain textbox back to the rich textbox control (see example
below)
private void btnRemoveFormatting_Click(object sender, EventArgs e)
{
txtPlainText.Text = txtRTF.Text;
txtRTF.Text = ""; // Required - this makes sure all formatting is gone
txtRTF.Text = txtPlainText.Text;
}

How to pop up a text file in C#

This small program shows how to open a txt file on the hard drive, is there a way I can have a procedure that when I click a button a txt file pops up, may someone help me with it or how I can go about it....
This is the procedure below
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
of.ShowDialog();
textBox1.Text = of.FileName;
}
May somebody tell me what I can do or give some material to read kind of new in C#
Assuming that you like opening notepad with the text file showing on it, you could use:
System.Diagnostics.Process.Start(of.FileName);
This will open the file using the default text editor of the computer.
EDIT
According to your comment, you should the do it like this:
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(#"C:\dir1\dir2\yourfile.txt");
}
Obviusly, you should replace that with the path for your specific file.
Try
MessageBox.Show(File.ReadAllText(of.FileName));
After it, try to learn each component of the statement, what it does.
BTW,
You also need:
if (of.ShowDialog() == DialogResult.OK)
before that, to avoid displaying message in case user clicks Cancel.
Based on the clarification, this is pretty straightforward. Just create a new form class that contains a textbox (and probably a Close button). You'll want a property on the form that will set the textbox. You can launch the this form from your button event handler (the one you have in your example) like this:
using(var myForm = new TextBoxForm()) {
myForm.TextFileContents = <file contents>
myForm.ShowDialog();
}
As for reading in the file contents, you'll want to use File.ReadAllText() as described in Daniel's answer. See the MSDN documentation for more on that. I'll leave the remaining details as an exercise to the reader.

Categories

Resources