Không có gì tuyệt vời hơn là luyện tập với ví dụ thực tế. Nào cùng nhau thử thách bản thân với một ứng dụng thú vị: Từ điển nói.
Code file Form1.designer.cs
namespace SpeakDictionary
{
partial class Form1
{
private System.ComponentModel.IContainer components = null ;
protected override void Dispose (bool disposing)
{
if (disposing && (components != null ))
{
components.Dispose();
}
base .Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent ()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof (Form1));
this .panel1 = new System.Windows.Forms.Panel();
this .panel2 = new System.Windows.Forms.Panel();
this .txbMeaning = new System.Windows.Forms.TextBox();
this .txbExplaination = new System.Windows.Forms.TextBox();
this .cbWord = new System.Windows.Forms.ComboBox();
this .btnSpeakEnglish = new System.Windows.Forms.Button();
this .button1 = new System.Windows.Forms.Button();
this .button2 = new System.Windows.Forms.Button();
this .panel1.SuspendLayout();
this .panel2.SuspendLayout();
this .SuspendLayout();
this .panel1.Controls.Add(this .btnSpeakEnglish);
this .panel1.Controls.Add(this .cbWord);
this .panel1.Location = new System.Drawing.Point(12 , 12 );
this .panel1.Name = "panel1" ;
this .panel1.Size = new System.Drawing.Size(154 , 349 );
this .panel1.TabIndex = 0 ;
this .panel2.Controls.Add(this .button2);
this .panel2.Controls.Add(this .button1);
this .panel2.Controls.Add(this .txbExplaination);
this .panel2.Controls.Add(this .txbMeaning);
this .panel2.Location = new System.Drawing.Point(172 , 12 );
this .panel2.Name = "panel2" ;
this .panel2.Size = new System.Drawing.Size(391 , 349 );
this .panel2.TabIndex = 1 ;
this .txbMeaning.Location = new System.Drawing.Point(3 , 3 );
this .txbMeaning.Name = "txbMeaning" ;
this .txbMeaning.Size = new System.Drawing.Size(359 , 20 );
this .txbMeaning.TabIndex = 0 ;
this .txbExplaination.Location = new System.Drawing.Point(3 , 29 );
this .txbExplaination.Multiline = true ;
this .txbExplaination.Name = "txbExplaination" ;
this .txbExplaination.Size = new System.Drawing.Size(359 , 317 );
this .txbExplaination.TabIndex = 1 ;
this .cbWord.DropDownStyle = System.Windows.Forms.ComboBoxStyle.Simple;
this .cbWord.FormattingEnabled = true ;
this .cbWord.Location = new System.Drawing.Point(0 , 3 );
this .cbWord.Name = "cbWord" ;
this .cbWord.Size = new System.Drawing.Size(124 , 345 );
this .cbWord.TabIndex = 0 ;
this .cbWord.SelectedIndexChanged += new System.EventHandler(this .cbWord_SelectedIndexChanged);
this .btnSpeakEnglish.BackgroundImage = global::SpeakDictionary.Properties.Resources.Kteam_vector_copy_2;
this .btnSpeakEnglish.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this .btnSpeakEnglish.Location = new System.Drawing.Point(130 , 3 );
this .btnSpeakEnglish.Name = "btnSpeakEnglish" ;
this .btnSpeakEnglish.Size = new System.Drawing.Size(20 , 23 );
this .btnSpeakEnglish.TabIndex = 1 ;
this .btnSpeakEnglish.UseVisualStyleBackColor = true ;
this .btnSpeakEnglish.Click += new System.EventHandler(this .btnSpeakEnglish_Click);
this .button1.BackgroundImage = global::SpeakDictionary.Properties.Resources.Kteam_vector_copy_2;
this .button1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this .button1.Location = new System.Drawing.Point(368 , 3 );
this .button1.Name = "button1" ;
this .button1.Size = new System.Drawing.Size(20 , 23 );
this .button1.TabIndex = 2 ;
this .button1.UseVisualStyleBackColor = true ;
this .button1.Click += new System.EventHandler(this .button1_Click);
this .button2.BackgroundImage = global::SpeakDictionary.Properties.Resources.Kteam_vector_copy_2;
this .button2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this .button2.Location = new System.Drawing.Point(368 , 323 );
this .button2.Name = "button2" ;
this .button2.Size = new System.Drawing.Size(20 , 23 );
this .button2.TabIndex = 3 ;
this .button2.UseVisualStyleBackColor = true ;
this .button2.Click += new System.EventHandler(this .button2_Click);
this .AutoScaleDimensions = new System.Drawing.SizeF(6 F, 13 F);
this .AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this .ClientSize = new System.Drawing.Size(575 , 373 );
this .Controls.Add(this .panel2);
this .Controls.Add(this .panel1);
this .Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon" )));
this .Name = "Form1" ;
this .Text = "Anh - Việt | Dictionary" ;
this .FormClosing += new System.Windows.Forms.FormClosingEventHandler(this .Form1_FormClosing);
this .panel1.ResumeLayout(false );
this .panel2.ResumeLayout(false );
this .panel2.PerformLayout();
this .ResumeLayout(false );
}
#endregion
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button btnSpeakEnglish;
private System.Windows.Forms.ComboBox cbWord;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.TextBox txbExplaination;
private System.Windows.Forms.TextBox txbMeaning;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
}
}
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.Threading.Tasks;
using System.Windows.Forms;
namespace SpeakDictionary
{
public partial class Form1 : Form
{
DictionaryManager dictionary;
SpeakText VietNam;
SpeakText English;
bool isLoading1 = true ;
bool isLoading2 = true ;
public Form1 ()
{
InitializeComponent();
ChangeLoading();
cbWord.DisplayMember = "Key" ;
WebBrowser wbVN = new WebBrowser();
wbVN.Width = 0 ;
wbVN.Height = 0 ;
wbVN.Visible = false ;
wbVN.ScriptErrorsSuppressed = true ;
wbVN.Navigate(Cons.VietNamLink);
wbVN.DocumentCompleted += wbVN_DocumentCompleted;
this .Controls.Add(wbVN);
WebBrowser wbEn = new WebBrowser();
wbEn.Width = 0 ;
wbEn.Height = 0 ;
wbEn.Visible = false ;
wbEn.ScriptErrorsSuppressed = true ;
wbEn.Navigate(Cons.EnglishLink);
wbEn.DocumentCompleted += wbEn_DocumentCompleted;
this .Controls.Add(wbEn);
VietNam = new SpeakText(wbVN);
English = new SpeakText(wbEn);
dictionary = new DictionaryManager();
dictionary.LoadDataToCombobox(cbWord);
}
void wbEn_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
isLoading1 = false ;
ChangeLoading();
}
void wbVN_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
isLoading2 = false ;
ChangeLoading();
}
void ChangeLoading()
{
this .Enabled = !(isLoading1 && isLoading2);
}
private void Form1_FormClosing (object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Bạn có chắc chắn muốn thoát?" , "Thông báo" , MessageBoxButtons.OKCancel) != System.Windows.Forms.DialogResult.OK)
{
e.Cancel = true ;
return ;
}
dictionary.Serialize();
}
private void cbWord_SelectedIndexChanged (object sender, EventArgs e)
{
ComboBox cb = sender as ComboBox;
if (cb.DataSource == null )
return ;
DictionaryData data = cb.SelectedItem as DictionaryData;
txbMeaning.Text = data.Meaning;
txbExplaination.Text = data.Explaination;
}
private void btnSpeakEnglish_Click (object sender, EventArgs e)
{
English.Spreak((cbWord.SelectedItem as DictionaryData).Key);
}
private void button1_Click (object sender, EventArgs e)
{
VietNam.Spreak(txbMeaning.Text);
}
private void button2_Click (object sender, EventArgs e)
{
VietNam.Spreak(txbExplaination.Text);
}
}
}
Code DictionaryData.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpeakDictionary
{
public class DictionaryData
{
private string key;
public string Key
{
get { return key; }
set { key = value ; }
}
private string meaning;
public string Meaning
{
get { return meaning; }
set { meaning = value ; }
}
private string explaination;
public string Explaination
{
get { return explaination; }
set { explaination = value ; }
}
}
}
Code DictionaryItem.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpeakDictionary
{
[Serializable]
public class DictionaryItem
{
private List<DictionaryData> items;
public List<DictionaryData> Items
{
get { return items; }
set { items = value ; }
}
}
}
Code DictionaryManager.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace SpeakDictionary
{
public class DictionaryManager
{
#region properties
private string filePath = "data.xml" ;
private DictionaryItem items;
public DictionaryItem Items
{
get { return items; }
set { items = value ; }
}
#endregion
#region methods
public DictionaryManager ()
{
items = (DictionaryItem)DeserializeFromXML(filePath);
}
public void LoadDataToCombobox (ComboBox combo)
{
combo.DataSource = items.Items;
}
public void Serialize ()
{
SerializeToXML(Items, filePath);
}
private void SerializeToXML (object data, string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
XmlSerializer sr = new XmlSerializer(typeof (DictionaryItem));
sr.Serialize(fs, data);
fs.Close();
}
public object DeserializeFromXML (string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
XmlSerializer sr = new XmlSerializer(typeof (DictionaryItem));
object obj = sr.Deserialize(fs);
fs.Close();
return obj;
}
#endregion
}
}
Code data.xml
<?xml version="1.0"?>
<DictionaryItem xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" >
<Items >
<DictionaryData >
<Key > Hello</Key >
<Meaning > Xin chào</Meaning >
<Explaination > Từ mang ý nghĩa chào hỏi. Có thể dùng mỗi khi gặp một ai đó.</Explaination >
</DictionaryData >
<DictionaryData >
<Key > Good bye</Key >
<Meaning > Chào tạm biệt</Meaning >
<Explaination > Từ mang ý nghĩa chào tạm biệt. Có thể dùng mỗi khi tạm biệt một ai đó.</Explaination >
</DictionaryData >
<DictionaryData >
<Key > How Kteam</Key >
<Meaning > How Kteam</Meaning >
<Explaination > Một dự án chia sẽ kiến thức miễn phí</Explaination >
</DictionaryData >
<DictionaryData >
<Key > Kter</Key >
<Meaning > Thành viên của Kteam</Meaning >
<Explaination > Bất cứ ai mong muốn chia sẽ kiến thức miễn phí. Đều là Kter.</Explaination >
</DictionaryData >
<DictionaryData >
<Key > HowKteam.com</Key >
<Meaning > Website HowKteam.com</Meaning >
<Explaination > Một website giáo dục, chia sẽ kiến thức hoàn toàn miễn phí. Xúc tích, dễ hiểu.</Explaination >
</DictionaryData >
</Items >
</DictionaryItem >
Code Cons.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpeakDictionary
{
public class Cons
{
public static string VietNamLink = "http://responsivevoice.org/text-to-speech-languages/van-ban-de-noi-trong-tieng-viet/" ;
public static string EnglishLink = "http://responsivevoice.org/text-to-speech-languages/us-english-text-to-speech/" ;
}
}
Code SpeakText.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SpeakDictionary
{
public class SpeakText
{
private WebBrowser wbWeb;
public WebBrowser WbWeb
{
get { return wbWeb; }
set { wbWeb = value ; }
}
public SpeakText (WebBrowser wb)
{
this .WbWeb = wb;
}
private void SetText (string data)
{
HtmlElement element = WbWeb.Document.GetElementById("text" );
element.SetAttribute("value" , data);
}
private void Speak ()
{
HtmlElement element = WbWeb.Document.GetElementById("playbutton" );
element.InvokeMember("click" );
}
public void Spreak (string data)
{
SetText(data);
Speak();
}
}
}
Hãy khoe thành quả của bạn ở phần comment nhé.
Đừng quên: “Luyện tập – Thử thách – Không ngại khó ”
Tải xuống
Tài liệu
Nhằm phục vụ mục đích học tập Offline của cộng đồng, Kteam hỗ trợ tính năng lưu trữ nội dung bài học Tạo chức năng đọc chữ từ điển nói C# Winform dưới dạng file PDF trong link bên dưới.
Ngoài ra, bạn cũng có thể tìm thấy các tài liệu được đóng góp từ cộng đồng ở mục TÀI LIỆU trên thư viện Howkteam.com
Đừng quên like và share để ủng hộ Kteam và tác giả nhé!
Thảo luận
Nếu bạn có bất kỳ khó khăn hay thắc mắc gì về khóa học, đừng ngần ngại đặt câu hỏi trong phần BÌNH LUẬN bên dưới hoặc trong mục HỎI & ĐÁP trên thư viện Howkteam.com để nhận được sự hỗ trợ từ cộng đồng.
Cám ơn bài hướng dẫn thật chi tiết
Ad hướng dẫn thêm phần thêm từ mới không add như vậy sẽ hoàn chỉnh hơn
Thanks
:)
Nó hiện lên chứ ResponsiveVoice.JS Text to Speech, không load được trang này.
cái này là mình tự đưa thư viện vào hả a, có thể translte như google dịch và xem phần phiên âm được ko a?
Em thì hoàn hảo xong từ đầu đến cuối rồi, nhưng tại sao nó lại báo lỗi khi nhấn nút nói