i need to open a file in the parent form then display it in the child form. i have a FORM1.cs file that opens the file, the opens the child window but i cant seem to understand how to show the text file contents in the child window.
I am doing this in the openToolStripMenuItem function.
how can i get the value into the child form?
any suggestions would be greatly appreciated.
-----------------------------------code start -------------------------------------------
private void openToolStripMenuItem_Click_1(object sender, EventArgs e)
{
OpenFileDialog ofdOpenFile = new OpenFileDialog();
ofdOpenFile.Title = "Select a File To Open";
ofdOpenFile.FileName = "";
ofdOpenFile.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
if (ofdOpenFile.ShowDialog() == DialogResult.OK)
{
RichTextBox rtb = GetRichTextBox();
if (rtb == null)
{
FormChild fc = new FormChild();
fc.MdiParent = this;
fc.Show();
string str = "dammit";
fc.Text = str;
}
if (rtb != null)
{
rtb.LoadFile(ofdOpenFile.FileName, RichTextBoxStreamType.RichText);
}
}
}
Loading
VulpesPosted Jun 19, 2013, 8:06 AM
If that's the case, I'd try changing the offending line to this:
JosephPosted Jun 19, 2013, 8:50 AM
JosephPosted Jun 19, 2013, 7:51 AM
namespace MDI_Simple_Text_Editor
{
public partial class FormChild : Form
{
public FormChild(string fileName)
{
InitializeComponent();
richTextBox1.LoadFile(fileName, RichTextBoxStreamType.RichText);
}
public RichTextBox GetRichTextBox()
{
RichTextBox rtb = null;
if (ActiveMdiChild != null)
{
rtb = ActiveMdiChild.Controls[0] as RichTextBox;
}
return rtb;
}
private void button1_Click(object sender, EventArgs e)
{
SaveFileDialog sfdSaveFile = new SaveFileDialog();
sfdSaveFile.Title = "Select a File To Save";
sfdSaveFile.FileName = "";
sfdSaveFile.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
if (sfdSaveFile.ShowDialog() == DialogResult.OK);
}
}
}
JosephPosted Jun 19, 2013, 7:50 AM
SUDU8716161,2PHLW342EN
SUDU8789384,2PHLW345EN
SUDU7753535,2PHLW346EN
SUDU5649080,2PHLW349EN
SUDU8666625,2PHLW353EN
HASU4036332,2PHLW358EN
-joe
JosephPosted Jun 19, 2013, 7:46 AM
now in the child form at runtime at this line:
richTextBox1.LoadFile(fileName, RichTextBoxStreamType.RichText);
i get this error:
System.ArgumentException was unhandled
Message=File format is not valid.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.RichTextBox.StreamIn(Stream data, Int32 flags)
at System.Windows.Forms.RichTextBox.LoadFile(Stream data, RichTextBoxStreamType fileType)
at System.Windows.Forms.RichTextBox.LoadFile(String path, RichTextBoxStreamType fileType)
at MDI_Simple_Text_Editor.FormChild..ctor(String fileName) in C:\Users\jlongo\Desktop\MDI3\MDI Simple Text Editor\MDI Simple Text Editor\FormChild.cs:line 18
at MDI_Simple_Text_Editor.Form1.openToolStripMenuItem_Click_1(Object sender, EventArgs e) in C:\Users\jlongo\Desktop\MDI3\MDI Simple Text Editor\MDI Simple Text Editor\Form1.cs:line 95
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at MDI_Simple_Text_Editor.Program.Main() in C:\Users\jlongo\Desktop\MDI3\MDI Simple Text Editor\MDI Simple Text Editor\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
VulpesPosted Jun 19, 2013, 7:39 AM
JosephPosted Jun 19, 2013, 7:37 AM
FormChild fc = new FormChild(ofdOpenFile.FileName);
i get an error:
'MDI_Simple_Text_Editor.FormChild' does not contain a constructor that takes 1 arguments C:\Users\jlongo\Desktop\MDI3\MDI Simple Text Editor\MDI Simple Text Editor\Form1.cs
VulpesPosted Jun 19, 2013, 7:25 AM