PDA

View Full Version : مشکل با Open File Dialog



Big Brother
سه شنبه 25 بهمن 1384, 13:20 عصر
راستش من مدتیه با این سایت آشنا شدم و تا الان هم برای پیدا کردن جوابم هم سرچ کردم و هم تا صفحه 46 قسمت: "برنامه نویسی در #C" رو دنبال تاپیکی که جوابم رو توش پیدا کنم گشتم خیلی چیزا پیدا کردم ولی راه حل مورد نظرم رو پیدا نکردم! با توجه به وقت کمی که برای تحویل این پروژه دارم مجبور شدم این تاپیک رو بزنم.

و اما مشکل:


متاسفانه من توی سی شارپ یه مبتدی هستم. کاری که میخوام بکنم اینه که میخوام برنامه, یه فایل متنی (txt یا doc یا هردو!) که آدرسش رو یوزر میده رو باز کنه و محتویاتش رو تماما به یه textBox (یا RichTextBox هر کدوم مناسبتره) منتقل کنه. فقط همین! (یعنی قصد تغییر در فایل مربوطه رو ندارم)

برای این کار من از Open File Dialog استفاده کردم (با راهنمایی های یه کتاب). حالا در زمان اجرا کادر محاوره ای open file ظاهر میشه ولی توی قسمت تایپش (تایپ های قابل پشتیبانی) هیچی نیست. و اگه یه فایل متنی رو باهاش open کنم Error میده که فایل قابل شناسایی نیست!

اینم کد برنامه ی من هست. (امیدوارم اشتباه احمقانه ای توش نباشه!:لبخند: )


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;


namespace test_project
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.RichTextBox richTextBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private FileStream input;
private string fileName;

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(168, 176);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "Open";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(48, 8);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(296, 128);
this.richTextBox1.TabIndex = 1;
this.richTextBox1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(432, 310);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{

}

private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog fileChooser = new OpenFileDialog();
DialogResult result = fileChooser.ShowDialog();


if(result==DialogResult.Cancel )
return;

fileName = fileChooser.FileName ;

if(fileName == ""||fileName == null)
MessageBox.Show("Invalid File Name","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);

input = new FileStream(fileName,FileMode.Open,FileAccess.Read) ;
richTextBox1.LoadFile(fileName);


}
}
}

اگه لطف کنید مشکل این برنامه رو اصلاح کنید یا برنامه صحیح برای انجام اینکار رو برام بنویسین. خیلی خیلی ممنون میشم. بدجوری لنگش هستم.

مطهر
سه شنبه 25 بهمن 1384, 13:52 عصر
دوست عزیز ورودت را به "برنامه نویس" خوش آمد می گویم
البته نیاز همه ی پرو|ه را کپی کنی اینجا . فقط قسمت های مورد نظر
ضمناً من کد شما را نخوندم(ببخشید ) ولی اینجوری نوشتم.

Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
int i = 0 ;

openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 1 ;
openFileDialog1.RestoreDirectory = true ;

if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
if((myStream = openFileDialog1.OpenFile())!= null)
{
// Insert code to read the stream here.
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(myStream))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
textBox1.Text = textBox1.Text + "\r\n" + line ;
}
}
}
catch (Exception err)
{
// Let the user know what went wrong.
MessageBox.Show(err.Message);
}

myStream.Close();
}
}

Big Brother
سه شنبه 25 بهمن 1384, 21:00 عصر
با تشکر از جناب مطهر:قلب:

خیلی ممنون از کدی که گذاشتی.

البته با فارسی مشکل داشت که خودم درستش کردم الان اون مشکل حل شده.


حالا مشکل جدید اینه که چرا RichTextBox مثل TextBox رویداد دابل کلیک نداره!

من بهش احتیاج دارم. آیا راه حلی وجود داره؟

مطهر
چهارشنبه 26 بهمن 1384, 16:06 عصر
البته با فارسی مشکل داشت که خودم درستش کردم الان اون مشکل حل شده
فکر نکنم. لطفاً بگید چیکار کردید

Big Brother
پنج شنبه 27 بهمن 1384, 15:58 عصر
حالا مشکل جدید اینه که چرا RichTextBox مثل TextBox رویداد دابل کلیک نداره!

من بهش احتیاج دارم. آیا راه حلی وجود داره؟


من متوجه شدم که توی ویژوال استدیو 2005 RichTextBox هم رویداد دابل کلیک داره ولی فعلا

فعلا من بهش دسترسی ندارم تا حدودی هم عجله دارم. لطفا اگه کسی راه حلی داره بگه.

جناب مطهر شرمنده الان وقتم خیلی کمه به اندازه چند دقیقه فقط تونستم بیام این پست رو بفرستم به زودی کد مربوطه رو میگذارم:چشمک:

Big Brother
جمعه 28 بهمن 1384, 23:28 عصر
کسی که جواب نداد!:گریه: ولی به هر حال با هر مکافاتی بود ویژوال استدیو 2005 رو نصب کردم!


حالا یه مشکل دیگه به وجود اومده لطفا به من عاجز بیسواد کمک کنید!!!:لبخند:


اینو چیکارش کنم:


http://www.asemoon.persiangig.com/image/khata.JPG

ali_kolahdoozan
شنبه 29 بهمن 1384, 09:49 صبح
پیغام خطات روی کدهای بالای صفحه رو گرفته ولی فکر کنم اشکال از عدم تطابق پسوند فایلهایی است که در قسمت فیلتر قرار دادی . من حدس می زنم . کدهای بالای صفحه رفته زیر پیغام خطات

مطهر
شنبه 29 بهمن 1384, 10:11 صبح
چک کن ببین [STAThread] را قبل از void Main نوشته ای یا نه

Big Brother
چهارشنبه 03 اسفند 1384, 16:17 عصر
عجیبه توی نقل و انتقال از 2003 به 2005 [STAThread] حذف شده بود!

با گذاشتنش قبل از مین مشکل ارور کامپایلر حل شد.(با تشکر ویژه از آقای مطهر)


ولی مشکل کجا بود؟

این بود که یه حالت استثنا وجود داشت : "موقعی که روی richTextBox خالی دابل کلیک بشه"

با اضافه کردن یه if برای این حالت خاص مشکل به کلی حل شد.


ممنون.

mzd8019
شنبه 25 خرداد 1387, 10:42 صبح
با سلام
من می‌خوام کدی را بنویسم که یک فایل Text را با OpenFileDialog باز کرده و در یک TextBox نشان بده.
کد زیر را وارد می‌کنم. ولی نمی‌توانم محتویات فایل را درون آن بریزم. در ضمن من با دات نت 2003 کار می‌کنم.
OpenFileDialog1.Filter="Text Files(*.txt)|*.txt|"+"All files(*.*)|*.*";

OpenFileDialog1.FilterIndex=1;
OpenFileDialog1.Title="Demo Open File Dialog";
if(OpenFileDialog1.ShowDialog()==DialogResult.OK)
{
strFileName=OpenFileDialog1.FileName;
txtFile.Text=System.IO.File.OpenRead(strFileName);