NavigateurAndCo/NavigateurWeb/Form1.cs

145 lines
3.8 KiB
C#

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;
using System.IO;
namespace NavigateurWeb
{
public partial class Form1 : Form
{
ContextMenuStrip fav = new ContextMenuStrip();
public Form1()
{
InitializeComponent();
if(File.Exists("fav.txt"))
{
ReadFav();
}
fav.ItemClicked += new ToolStripItemClickedEventHandler(this.fav_ItemClicked);
}
public void loadUrl()
{
String address = txtBoxAddress.Text;
if (String.IsNullOrEmpty(address)) return;
if (address.Equals("about:blank")) return;
if (!address.StartsWith("http://") &&
!address.StartsWith("https://"))
{
address = "http://" + address;
}
try
{
webBrowser1.Navigate(new Uri(address));
}
catch (System.UriFormatException)
{
return;
}
}
private void pbGo_Click(object sender, EventArgs e)
{
loadUrl();
}
private void pbRefresh_Click(object sender, EventArgs e)
{
txtBoxAddress.Text = Settings1.Default.home;
loadUrl();
}
private void pbBack_Click(object sender, EventArgs e)
{
webBrowser1.GoBack();
txtBoxAddress.Text = webBrowser1.Url.ToString();
}
private void pbUp_Click(object sender, EventArgs e)
{
webBrowser1.GoForward();
txtBoxAddress.Text = webBrowser1.Url.ToString();
}
private void pbHome_Click(object sender, EventArgs e)
{
webBrowser1.Refresh();
txtBoxAddress.Text = webBrowser1.Url.ToString();
}
private void txtBoxAddress_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
loadUrl();
}
private void pbFav_Click(object sender, EventArgs e)
{
fav.Show(MousePosition);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
WriteFav();
MailSettings.Default.Save();
Settings1.Default.Save();
}
public void WriteFav()
{
using (StreamWriter sw = new StreamWriter("fav.txt"))
{
foreach(ToolStripMenuItem favText in fav.Items)
{
sw.WriteLine(favText.ToString());
}
}
}
public void ReadFav()
{
fav.Items.Clear();
using (StreamReader sr = new StreamReader("fav.txt"))
{
string favText;
while ((favText = sr.ReadLine()) != null)
{
fav.Items.Add(favText.ToString());
}
}
}
private void pbAddFav_Click(object sender, EventArgs e)
{
fav.Items.Add(txtBoxAddress.Text);
}
private void fav_ItemClicked(Object sender, ToolStripItemClickedEventArgs e)
{
txtBoxAddress.Text = e.ClickedItem.Text;
loadUrl();
}
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
txtBoxAddress.Text = webBrowser1.Url.ToString() ;
}
private void pbSendMail_Click(object sender, EventArgs e)
{
MailTo mail = new MailTo();
mail.ShowDialog();
}
}
}