|
|
|
C# WinApp linkLabel อยากให้ เวลาเปิดไฟล์ที่ไม่รู้จัก แล้ว แจ้งเตือน ครับ |
|
|
|
|
|
|
|
ที่มันหาไฟล์ไม่เจอเพราะออปเจ็ก LinkCollection ยังไม่มีข้อมูล(ลิงค์) ต้องเพิ่มลิงค์เข้าไปก่อนครับ ด้วยเมธอด Add() ของคลาส LinkCollection
LinkLabel.LinkCollection.Add Method
|
|
|
|
|
Date :
2019-07-06 13:00:42 |
By :
PhrayaDev |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณมากครับ
|
|
|
|
|
Date :
2019-07-07 09:28:15 |
By :
lamaka.tor |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
โค้ด TextboxFileDialog ครับ
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Versioning;
namespace TORServices.FormsTor
{
public partial class TextboxFileDialog : UserControl
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component 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.panel1 = new System.Windows.Forms.Panel();
this._Button = new System.Windows.Forms.Button();
this.panel2 = new System.Windows.Forms.Panel();
this._Lnk = new System.Windows.Forms.LinkLabel();
this.panel2.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Dock = System.Windows.Forms.DockStyle.Right;
this.panel1.Location = new System.Drawing.Point(273, 0);
this.panel1.Margin = new System.Windows.Forms.Padding(5);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(11, 33);
this.panel1.TabIndex = 5;
//
// _Button
//
this._Button.Cursor = System.Windows.Forms.Cursors.Hand;
this._Button.Dock = System.Windows.Forms.DockStyle.Right;
this._Button.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
this._Button.Image = global::TORServices.Properties.Resources.search_button32x32;
this._Button.Location = new System.Drawing.Point(284, 0);
this._Button.Margin = new System.Windows.Forms.Padding(5);
this._Button.Name = "_Button";
this._Button.Size = new System.Drawing.Size(34, 33);
this._Button.TabIndex = 4;
this._Button.Click += new System.EventHandler(this._Button_Click);
//
// panel2
//
this.panel2.BackColor = System.Drawing.Color.White;
this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel2.Controls.Add(this._Lnk);
this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel2.Location = new System.Drawing.Point(0, 0);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(273, 33);
this.panel2.TabIndex = 6;
//
// _Lnk
//
this._Lnk.AutoSize = true;
this._Lnk.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
this._Lnk.Location = new System.Drawing.Point(5, 4);
this._Lnk.Name = "_Lnk";
this._Lnk.Size = new System.Drawing.Size(94, 24);
this._Lnk.TabIndex = 6;
this._Lnk.TabStop = true;
this._Lnk.Text = "linkLabel1";
this._Lnk.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this._Lnk_LinkClicked);
//
// TextboxFileDialog
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Controls.Add(this._Button);
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
this.Margin = new System.Windows.Forms.Padding(5);
this.Name = "TextboxFileDialog";
this.Size = new System.Drawing.Size(318, 33);
this.panel2.ResumeLayout(false);
this.panel2.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
public System.Windows.Forms.Button _Button;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.LinkLabel _Lnk;
public TextboxFileDialog()
{
InitializeComponent();
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(222)));
// _Lnk.BackColor = _txt.BackColor;
// _Lnk.Text = _txt.Text;
}
#region _Property
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
public override string Text
{
get
{
return this._Lnk.Text;
}
set
{
this._Lnk.Text = value;
// this.Text = value;
_Lnk.Links.Clear();
_Lnk.Links.Add(0, _Lnk.Text.Length, FullPath);
}
}
[Localizable(true)]
public Image ImageButton
{
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
get
{
return _Button.Image;
}
set
{
_Button.Image = value; Invalidate();
}
}
[
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
RefreshProperties(RefreshProperties.All),
ParenthesizePropertyName(true)
]
public ControlBindingsCollection DataBindings
{
get
{
ControlBindingsCollection bindings = (ControlBindingsCollection)this._Lnk.DataBindings;
if (bindings == null)
{
bindings = new ControlBindingsCollection(this);
}
return bindings;
}
}
private bool oponly = true;
[System.ComponentModel.Description("Set OpenOnly on Text"),
System.ComponentModel.Browsable(true),
System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always),
System.ComponentModel.Category("TOR Setting")]
public bool OpenOnly
{
get { return oponly; }
set { oponly = value; }
}
private string _Path;
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)]
[System.ComponentModel.DefaultValue(@"C:")]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("Open Dialog is dispalyed and on success the contents of the Cell is replaced with the new path.")]
public string Directory
{
get
{
return _Path;
}
set
{
_Path = value;
}
}
private string _SubPath;
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)]
[System.ComponentModel.DefaultValue("")]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("Open Dialog is dispalyed and on success the contents of the Cell is replaced with the new path.")]
public string SubDirectory
{
get
{
return _SubPath;
}
set
{
_SubPath = value;
}
}
private string _Filter;
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)]
[System.ComponentModel.DefaultValue("Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png" +
"|txt files (*.txt)|*.txt" +
"|Document files (*.doc, *.docx, *.xls, *.xlsx, *.ppt, *.pptx, *.pdf )|*.doc, *.docx, *.xls, *.xlsx, *.ppt, *.pptx, *.pdf " +
"|Audio/Vdio files (*.AVI, *.XVID, *.DivX *.3GP *.MKV *.MP4 *.FLV *.MP3 *.DAT)|*.AVI, *.XVID, *.DivX *.3GP *.MKV *.MP4 *.FLV *.MP3 *.DAT" +
"|All files (*.*)|*.*")]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("Open Dialog is dispalyed and on success the contents of the Cell is replaced with the new path.")]
public string Filter
{
get
{
return _Filter;
}
set
{
_Filter = value;
}
}
private bool _showfullpath;
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
[System.ComponentModel.Description("Open Full path directory")]
public bool Showfullpath
{
get { return _showfullpath; }
set
{
_showfullpath = value;
if (_showfullpath == true)
{ Text = FullPath; }
else
{
if (System.IO.File.GetAttributes(FullPath).HasFlag(System.IO.FileAttributes.Directory))
{
Text = System.IO.Path.GetFileName(FullPath);
}
else
{
Text = "";
}
}
}
}
private Color _TextBackColor;
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.Category("TOR Setting")]
public Color TextBackColor
{
get { return _TextBackColor; }
set
{
panel2.BackColor = BackColor;
}
}
private string FullDirectory
{
get
{
string directory = ((string.IsNullOrEmpty(Directory)) ? Application.StartupPath : Directory.Replace("AppPath", Application.StartupPath)) + ((_SubPath == "") ? "" : "\\" + _SubPath);
directory = (!string.IsNullOrEmpty(System.IO.Path.GetPathRoot(directory))) ? directory : Application.StartupPath + "\\" + directory;
directory = (directory.EndsWith("\\")) ? directory : directory + "\\";
if (!System.IO.Directory.Exists(directory)) System.IO.Directory.CreateDirectory(directory);
return directory;
}
}
public string FullPath
{
get
{
// string path = (Showfullpath == true) ? Convert.ToString(this.Text) : FullDirectory + Convert.ToString(this.Text);
string path = FullDirectory + Convert.ToString(this.Text);
return (string.IsNullOrEmpty(path)) ? Application.StartupPath : path;
}
}
#endregion
#region _EventArgs
protected override void OnSizeChanged(EventArgs e)
{
if (this.Height != this.panel2.Height)
this.Height = this.panel2.Height;
_Button.Height = panel2.Height;
_Button.Width = _Button.Height;
base.OnSizeChanged(e);
}
protected override void OnForeColorChanged(EventArgs e)
{
_Lnk.ForeColor = ForeColor;
base.OnForeColorChanged(e);
}
protected override void OnFontChanged(EventArgs e)
{
_Lnk.Font = Font;
panel2.Font = Font;
this.OnSizeChanged(e);
// _Button.Font = Font;
}
public event EventHandler ButtonClick
{
add
{
_Button.Click += value;
}
remove
{
_Button.Click -= value;
}
}
public event EventHandler ButtonDoubleClick
{
add
{
_Button.DoubleClick += value;
}
remove
{
_Button.DoubleClick -= value;
}
}
public event EventHandler TextChanged
{
add
{
_Lnk.TextChanged += value;
}
remove
{
_Lnk.TextChanged -= value;
}
}
public event EventHandler TextBoxDoubleClick
{
add
{
_Lnk.DoubleClick += value;
}
remove
{
_Lnk.DoubleClick -= value;
}
}
#endregion
private void _Lnk_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (_Lnk.Links.Count > 0)
{
// TORServices.PathFileTor.FileTor.OpenFile(FullPath);
if (!System.IO.File.Exists(FullPath))
{
System.Windows.Forms.MessageBox.Show("Can't Find File " + FullPath);
}
else
{
System.Windows.Forms.MessageBox.Show("Can't Open File " + FullPath);
}
}
}
private void _Button_Click(object sender, EventArgs e)
{
using (OpenFileDialog dialog = new OpenFileDialog())
{
dialog.Filter = Filter;
if (dialog.ShowDialog() == DialogResult.OK)
{
using (SaveFileDialog dialog1 = new SaveFileDialog())
{
dialog1.InitialDirectory = FullDirectory;
dialog1.FileName = System.IO.Path.GetFileName(dialog.FileName);
dialog1.Filter = Filter;
if (dialog1.ShowDialog() == DialogResult.OK)
{
// SetPath(dialog.FileName);
this.Text = System.IO.Path.GetFileName(dialog1.FileName);
if (dialog.FileName != dialog1.FileName)
System.IO.File.Copy(dialog.FileName, dialog1.FileName, true);
}
}
}
}
}
}
}
การใช้งานทั้งใน datagridview กับ ลากมาลงใน ฟอร์ม ครับ
ช่วยๆกันใช้ แล้วก็แก้ ให้มัน ดีๆขึ้น นะครับ
|
|
|
|
|
Date :
2019-07-07 12:29:06 |
By :
lamaka.tor |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|