in einer Windows Form Aplication eine Textbox und eine CheckBox Speichert und wie ihr per button Click einen Open File Dialog öffnet und den Pfad der ausgewählten datei in eure Textbox einfügt.
Hier Das Video:
Hier nochma der Code:
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
if (Properties.Settings.Default.CB_Save)
{
textBox1.Text = Properties.Settings.Default.Pfad;
checkBox1.Checked = Properties.Settings.Default.CB_Save;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.checkBox1.Checked)
{
Properties.Settings.Default.Pfad = this.textBox1.Text;
}
else
{
// Alte Einstellungen löschen
Properties.Settings.Default.Pfad = "";
}
{
Properties.Settings.Default.CB_Save = this.checkBox1.Checked;
Properties.Settings.Default.Save();
}
}
private void button1_Click(object sender, EventArgs e)
{
string Pfad = string.Empty;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
Properties.Settings.Default.Pfad = openFileDialog1.FileName;
}
}
}
Form1.Design.cs
namespace WindowsFormsApplication1
{
partial class Form1
{
///
/// Erforderliche Designervariable.
///
private System.ComponentModel.IContainer components = null;
///
/// Verwendete Ressourcen bereinigen.
///
/// True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False. protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Vom Windows Form-Designer generierter Code
///
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
///
private void InitializeComponent()
{
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.button1 = new System.Windows.Forms.Button();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(149, 92);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Checked = global::WindowsFormsApplication1.Properties.Settings.Default.CB_Save;
this.checkBox1.DataBindings.Add(new System.Windows.Forms.Binding("Checked", global::WindowsFormsApplication1.Properties.Settings.Default, "CB_Save", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
this.checkBox1.Location = new System.Drawing.Point(135, 69);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(80, 17);
this.checkBox1.TabIndex = 1;
this.checkBox1.Text = "checkBox1";
this.checkBox1.UseVisualStyleBackColor = true;
//
// textBox1
//
this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", global::WindowsFormsApplication1.Properties.Settings.Default, "Pfad", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
this.textBox1.Location = new System.Drawing.Point(124, 43);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 2;
this.textBox1.Text = global::WindowsFormsApplication1.Properties.Settings.Default.Pfad;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 264);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
}
#endregion
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.TextBox textBox1;
}
}
Keine Kommentare:
Kommentar veröffentlichen