Skip to content

OpenFileDialog

ITAgnesmeyer edited this page Feb 22, 2019 · 1 revision

OpenFileDialog

With the object OpenFileDialog a file dialog can be opened.

private void FileOpen_Click(object sender, MouseClickEventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    
    //Title of the Dialog
    ofd.Title = "Open a Text File...";
    
    //All Values have to be separated with \0 
    ofd.Filter = "All File(*.*)\0*.*\0Text(*.txt)\0*.txt\0";

    //Shows Text(*.txt) as default
    ofd.DefaultFilterIndex = 2;
    
    //Show the Dialog modal to the current Form.
    if (ofd.Show(this))
    {
        //Read the selected Path
        string fileName = ofd.File;
        //File Actions.
        string text = File.ReadAllText(fileName);
        this._TextBox.Text = text;
        this.Text = "Little Edit:" + fileName;
    }
}
Clone this wiki locally