Show text in a richTextBox on a secon form - c#

on the first form I've a load button that load the file and call the second form. In the second form I've a richTextBox that has to show me text from the opened file, but it doesn't show nothing, here is what i've tried (I made richTextBox1 public to have access to it)
private void btnLoad_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
FormEditor f2 = new FormEditor();
f2.ShowDialog();
using (System.IO.StreamReader sr = new System.IO.StreamReader(ofd.FileName))
{
f2.richTextBox1.Text = sr.ReadToEnd();
}
}
}
If I try the same code putting richTextBox in the first form it works.

When you open f2 (f2.ShowDialog()), the code for filling richtextbox has not been executed, so you get an empty textbox on f2 (The code after ShowDialog() , will execute as soon as you close f2). Try:
FormEditor f2 = new FormEditor();
using (System.IO.StreamReader sr = new System.IO.StreamReader(ofd.FileName))
{
f2.richTextBox1.Text = sr.ReadToEnd();
}
f2.ShowDialog();

FormEditor should be reponsible of showing the text, not the current form.
Write constructor with parameter for FormEditor and pass text to it, then save it in a variable and show it in richtextbox on form load.
Your FormEditor class should look like this:
private string textForEdit{get;set;}
public FormEditor(string txt)
{
textForEdit = txt;
}
private void FormEditor_load(object sender, EventArgs e)
{
richTextBox1.Text = textForEdit;
}
Then, change inside of your if block to this:
using (System.IO.StreamReader sr = new System.IO.StreamReader(ofd.FileName))
{
FormEditor f2 = new FormEditor(sr.ReadToEnd());
f2.ShowDialog();
}

Related

Form ShowDialog won't load image in picture box

When I refresh my form with SHOWDIALOG my image doesn't load in my picture-box but if i refresh with only SHOW it works fine.
I want to be able to refresh with SHOWDIALOG and still have my picture load method work.
I have tried clearing data-bindings of both picture-box and the brows button.
private void formrefresh()
{
FoodItem FoodItem = new FoodItem();
FoodItem.ShowDialog();
this.Close();
}
public void GetImage()
{
OpenFileDialog BrowseImage = new OpenFileDialog();
BrowseImage.Filter = "Image Files(*.jpg; *.gif;)|*.jpg; *.gif";
if (BrowseImage.ShowDialog() == DialogResult.OK)
{
TextBox t =
Application.OpenForms["FoodItem"].Controls["imagePath"] as TextBox;
t.Text = BrowseImage.FileName;
filenametext = BrowseImage.FileName;
PictureBox p = Application.OpenForms["FoodItem"].Controls["foodImage"] as PictureBox;
p.Image = new Bitmap(BrowseImage.FileName);
}
}
private void BrowsImage_Click(object sender, EventArgs e)
{
GetFoodImage image = new GetFoodImage();
image.GetImage();
}
Finding forms (Application.OpenForms) is not a very good approach to use. Try to avoid that if possible. For example, there can be complications if there are multiple instances of a certain Form and you will have to find the exact instance you want to update.
It doesn't make much sense to use a Library to GetImage in your example. If you really need it in a separate Class Library, just return the path. Just return the image path from GetImage method and set the PictureBox from FoodImage.
private void formrefresh()
{
FoodItem foodItem = new FoodItem();
foodItem.ShowDialog();
this.Close();
}
private void BrowsImage_Click(object sender, EventArgs e)
{
GetFoodImage image = new GetFoodImage();
var imagePath = image.GetImage();
this.foodImage.Image = new Bitmap(imagePath);
this.imagePath.Text = imagePath;
}
public string GetImage(FoodItem foodItem)
{
OpenFileDialog BrowseImage = new OpenFileDialog();
BrowseImage.Filter = "Image Files(*.jpg; *.gif;)|*.jpg; *.gif";
if (BrowseImage.ShowDialog() == DialogResult.OK)
{
return BrowseImage.FileName;
}
return "";
}

c# saving a text in a specific directory

Here is my code;
private void button1_Click(object sender, EventArgs e)
{
string newFile =textBox1.Text;
string temp = newFile.Replace("YNATEST.", "");
SaveFileDialog a1 = new SaveFileDialog();
a1.FileName = "";
a1.Filter = "Text Files(*txt)|*.txt";
a1.DefaultExt = "txt";
a1.ShowDialog();
StreamWriter yazmaislemi = new StreamWriter(a1.FileName);
yazmaislemi.WriteLine(temp);
yazmaislemi.Close();
}
it is saving the text on Desktop but i want to save it to the following path:
C:\Users\esra.ur\Desktop\projee1
use save file dialog, so you can save your text in your specific directory
using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApplication30
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// When user clicks button, show the dialog.
saveFileDialog1.ShowDialog();
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
// Get file name.
string name = saveFileDialog1.FileName;
// Write to the file name selected.
// ... You can write the text from a TextBox instead of a string literal.
File.WriteAllText(name, "test");
}
}
}
this code snippets is from this link http://www.dotnetperls.com/savefiledialog
I hope it will help
1) Wrap your show dialog to check the result.
if(a1.ShowDialog() == DialogResult.OK)
2) The SaveFileDialog has a property for setting an initial path. This is for the directory which will be shown when the dialog is first open. For the desktop you want to use the Environment.GetFolderPath like so.
a1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
3) Try to separate concerns:
private string OutputFile {get;set;}
private void button1_Click(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(this.OutputPath))
{
SaveFileDialog a1 = new SaveFileDialog();
a1.FileName = textBox1.Text;
a1.Filter = "Text Files(*txt)|*.txt";
a1.DefaultExt = "txt";
a1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
if(a1.ShowDialog() == DialogResult.OK)
{
this.OutputFile = ai.FileName
}
}
this.SaveFile(this.OutputFile);
}
private void SaveFile(string FileName)
{
string newFile = FileName;
string temp = newFile.Replace("YNATEST.", "");
using(StreamWriter yazmaislemi = new StreamWriter(temp))
{
yazmaislemi.WriteLine(temp);
yazmaislemi.Close();
}
}
The SaveFileDialog object has a property called InitialDirectory, which is a string you can specify, for example
SaveFileDialog a1 = new SaveFileDialog();
a1.InitialDirectory = #"C:\Users\esra.ur\Desktop\projee1";
If this directory doesn't exist, it will default back to documents. Be careful about writing a file even if the user tries to cancel. Hope this helps?
In response to your comment, it sounds like you want to hard code the destination file name. This is dangerous as you can get an exception if the directory doesn't exist, but you can use the following: (I'm not sure what you want to do with the file name)
'string newFile = textBox1.Text;
string temp = newFile.Replace("YNATEST.", "");
StreamWriter yazmaislemi = new StreamWriter(#"C:\Users\esra.ur\Desktop\projee1\" + temp + ".txt");
yazmaislemi.WriteLine(temp);
yazmaislemi.Close();
In this case you don't need the SaveFileDialog at all. I think this is what you're asking for, but it's dangerous to code in this way.

opening a .txt in a windows form richtextbox

There is a question like this: Open a .txt file into a richTextBox in C#
But I need something a little different, I can open the txt in a richTextBox but I am using a button to make the file open. I want to skip the button and just have it load in the richTextBox.
Here is the button code, how do I move it to the richTextBox?:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.FileName = "Changes.txt";
string strfilename = openFileDialog1.FileName;
string filetext = File.ReadAllText(strfilename);
richTextBox1.Text = filetext;
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
By double clicking the rich I get richTextBox1_TextChanged:
How about the load event of the Form?
private void form_load(object sender, Eventargs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.FileName = "Changes.txt";
string strfilename = openFileDialog1.FileName;
string filetext = File.ReadAllText(strfilename);
richTextBox1.Text = filetext;
}
I'm not sure what you want to do in the richTextBox1_TextChanged event, maybe you could provide more information regarding this.
Having seen your edit, your'e still better off using the button to open the dialog and selecting a file that way. More intuitive then selecting the RichTextBox to trigger the OpenFileDialog.
From what I understand, you are probably searching for the event RichTextBox_Click. In your form.cs design view, click your RichTextBox and press F4 to see its properties and click the thunderbolt icon to see its events. Search for "Click" and then double click it. That will create the click event in your class, and you can add your logic there.
modify your code with this. when dialogbox visible select the text file which you named here as "Changes.txt".
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.FileName = "Changes.txt";
openFileDialog1.ShowDialog();
string strfilename = openFileDialog1.FileName;
string filetext = File.ReadAllText(strfilename);
richTextBox1.Text = filetext;
}

How can i keep rewriting to text file to add text?

In top of form1 i did:
StreamWriter w;
In the constructor i did:
w = new StreamWriter(#"d:\test.txt");
Now i have a checkbox that enable a button:
if (checkBox2.Checked)
{
if (marklightnings == true)
{
myTrackPanelss1.panel1.Enabled = true;
myTrackPanelss1.panel1.Visible = true;
button1.Enabled = true;
}
}
Now in the button1 click event i write to the text file:
private void button1_Click_1(object sender, EventArgs e)
{
w.WriteLine("test");
w.Close();
}
What i want to do is that each time i check the checkbox it will open the text file for writing but not to create a new text file but to use the same exist text file and just add a text to it.
Its not a settings file but it is a text file i need later to read the text from it.
What i need is that when the button1 is enabled i will be able to click on it ant it will add text to the text file each click.
So i did w.Close();
But then it cant be add more text to be written. If i will make new instance for w it will create new empty text file.
Try:
w = new StreamWriter(#"d:\test.txt", true);
Or:
private void button1_Click_1(object sender, EventArgs e)
{
using (StreamWriter w = File.AppendText(#"d:\test.txt"))
{
w.WriteLine("test");
}
}

Cannot insert Item to listview

Here are all my classes.
MainForm = listview,
CustomerFrame = textboxes
When I compile my program, my MainForm appears with an empty listview, and when I press on the add button to insert an item, my CustomerFrame class appears. When writing in the textboxes and clicking ok, no item inserted in my listview (MainForm). Why?
Some code:
MainForm
using(var customerframe = new CustomerFrame())
{
if (customerframe.DialogResult == DialogResult.OK)
{
CustomerFiles.Contact contact = customerframe.GetContact();
CustomerFiles.Address address = customerframe.GetAddress();
CustomerFiles.Phone phone = customerframe.GetPhone();
CustomerFiles.Email email = customerframe.GetEmail();
//Items in my listview
listviewitem = new ListViewItem();
listviewitem.SubItems.Add(contact.FirstName);
listviewitem.SubItems.Add(contact.LastName);
listviewitem.SubItems.Add(phone.Home);
listviewitem.SubItems.Add(phone.Mobile);
listviewitem.SubItems.Add(address.Country);
listviewitem.SubItems.Add(address.ZipCode);
listviewitem.SubItems.Add(address.City);
listviewitem.SubItems.Add(address.Street);
listviewitem.SubItems.Add(email.Personal);
this.listView1.Items.Add(listviewitem);
}
}
MainForm
private void addToolStripMenuItem_Click_1(object sender, EventArgs e)
{
customerframe.Show();
CustomerManager cm = new CustomerManager();
}
CustomerFrame
private void btnOk_Click(object sender, EventArgs e)
{
MainForm main = new MainForm();
DialogResult = DialogResult.OK;
}
By the way, when I use
if (customerframe.ShowDialog() == DialogResult.OK)
this will make the CustomerFrame form appear before the MainForm (which I don't want) and it will insert item, but only once.
Why do you open ANOTHER main form from DialogBox? I think that you should remove this line.
MainForm main = new MainForm();
And add this
DialogResult = DialogResult.OK;
Close();
Argh, to simplify - code in ButtonOK should look like this:
private void btnOk_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
Close();
}
EDIT: response to new problem
First create new CustomerFrame, show it and wait for it to close; then transfer new data to your ListView. I believe that your add handler should look like this:
private void addToolStripMenuItem_Click_1(object sender, EventArgs e)
{
using(var customerframe = new CustomerFrame())
{
// I don't know what this line does
CustomerManager cm = new CustomerManager();
if (customerFrame.ShowDialog() == DialogResult.OK)
{
CustomerFiles.Contact contact = customerframe.GetContact();
CustomerFiles.Address address = customerframe.GetAddress();
CustomerFiles.Phone phone = customerframe.GetPhone();
CustomerFiles.Email email = customerframe.GetEmail();
//Items in my listview
listviewitem = new ListViewItem();
listviewitem.SubItems.Add(contact.FirstName);
listviewitem.SubItems.Add(contact.LastName);
listviewitem.SubItems.Add(phone.Home);
listviewitem.SubItems.Add(phone.Mobile);
listviewitem.SubItems.Add(address.Country);
listviewitem.SubItems.Add(address.ZipCode);
listviewitem.SubItems.Add(address.City);
listviewitem.SubItems.Add(address.Street);
listviewitem.SubItems.Add(email.Personal);
this.listView1.Items.Add(listviewitem);
}
}
}

Categories

Resources