Meilleur auteur de réponses
Liens hypertext dans un webbrowser

Question
-
Bonjour je progra
mme en c# un éditeur html et la je voudrais donné la possibilité à mon utilisateur de sélection une partie dans mon webbrowser et transformer ce "bout" en un lien hypertexte
ex : aller vers google ". quand l'utilisateur clique sur le bouton liens il tombe sur une forme et il peux introduire dans une combobox le nom du site ex : www.google.be .
Le problème c'est que je ne vois pas comment je pourrais "joindre les deux " ... donc le liens "www.google.be" et "aller sur google
merci d'avance de votre aide
pour récuperer la séléction j'ai trouver ce code :<br/><br/><br/><br/> IHTMLDocument2 htmlDocument = HTMLEditor.Document.DomDocument as IHTMLDocument2; IHTMLSelectionObject currentSelection= htmlDocument.selection; if (currentSelection!=null) { IHTMLTxtRange range= currentSelection.createRange() as IHTMLTxtRange; if (range != null) { //MessageBox.Show(range.text); montext = range.text; } }
MERCII d'avance
Réponses
-
Bonjour,
Cela me semble compliqué
Pourquoi ne pas ajouter "http://" à ton ComboBoxURL.SelectedItem() ?
WebBrowser.Navigate("http://" + ComboBoxURL.SelectedItem());
fred on youTube
PHP MySQL Web Site Creator
fred- Marqué comme réponse SarahChou mercredi 25 janvier 2012 10:31
Toutes les réponses
-
De ce que je sais, c’est très facile remplacer un URL avec un link, par exemple, via javascript (voir aussi ce lien), mais ce que vous voulez faire est assez diffèrent.
Le code pour obtenir la sélection dans le WebBroser est correct. L’interface IHTMLDocument2 vous offre accès au texte sélectionné via selection qui vous donne une référence vers un objet IHTMLSelectionObject.
Puis, il faut vérifier que le type de cet objet est Text pour s’assurer qu’une sélection a été faite et que l’utilisateur n’a pas sélectionné un control. Dans l’affirmative, on utilise la méthode createRange pour obtenir une référence a l’objet TextRange correspondant.
Finalement, on utilise la propriété text de ce dernier objet pour décider selon sa valeur le lien qu’on veut placer et puis on remplace la valeur « texte » de la propriété text avec « <a href=“lien“> texte <\a> ».
Je n’ai pas testé l’algorithme, mais svp testez-le et n’hésitez pas à revenir avec des questions s’il ne fonctionne pas.
Si vous voulez voir un autre exemple d’utilisation de DHTML, consultez http://msdn.microsoft.com/en-us/library/aa290341(VS.71).aspx.
Les liens sont en anglais, mais vous pouvez utiliser Microsoft Translator pour les traduire.
Cordialement,
Cipri
Ciprian DUDUIALA, MSFT  
•Nous vous prions de considérer que dans le cadre de ce forum on n’offre pas de support technique et aucune garantie de la part de Microsoft ne peut être offerte.
-
Bonjour Cipri...
J'ai suivi tes conseils mais un tit problème qui pour moi me semble insurmontable est non résolu.
pour résumé :
Je veux pouvoir créer un lien HTML
Donc j'ai 2 forme , l'une se nomme HTMLEditor et l'autre Link
dans la forme HTMLEditor il y a un webBrowser et une toolStrip contenant le bouton link
Quand je selectionne un bout de texte ex : "aller sur google" et que clique sur le bouton liens je tombe sur la deuxième forme Link dans celle-ci je demande à l'utilisateur d'entrer un URL.
Donc en matière de donner j'ai le text sélectionné et l'url
Mon objectif est faire une sorte d’événement clique sur le bout de texte sélectionné pour que quand on clique dessus on navigue vers l'url.
Je me suis tiré les cheveux :colere2: dessus tout le week sans solution .. please help !!!
Voila mon code dans la forme Link :public partial class Link : Form { private bool _accepted = false; public Link() { InitializeComponent(); LoadUrls(); linkEdit.TextChanged += new EventHandler(linkEdit_TextChanged); } void linkEdit_TextChanged(object sender, EventArgs e) { label1.Text = URL; //MessageBox.Show("Variable URL"+URL); } public string URL { get { return comboBox1.Text + linkEdit.Text.Trim(); } } public string URI { get { return linkEdit.Text.Trim(); } } public bool Accepted { get { return _accepted; } } private void LinkDialog_Load(object sender, EventArgs e) { label1.Text = URL; BeginInvoke((MethodInvoker)delegate { linkEdit.Focus(); }); } private void LoadUrls() { string glob = Properties.Settings.Default.LinkDialogURLs; string[] urls = glob.Split(null); if (urls != null) { foreach (string url in urls) { linkEdit.Items.Add(url); } } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { label1.Text = URL; } private void OKbutton_Click_1(object sender, EventArgs e) { string url = linkEdit.Text; string glob = Properties.Settings.Default.LinkDialogURLs; if (glob == null) glob = ""; if (!glob.Contains(url)) { if (glob.Length > 0) glob += "\n"; glob += url; } Properties.Settings.Default.LinkDialogURLs = glob; Properties.Settings.Default.Save(); _accepted = true; Close(); //MessageBox.Show("url dans le click ok" + url ); } private void button2_Click(object sender, EventArgs e) { _accepted = false; Close(); }
et dans form HTMLEditor :private void link_Click(object sender, EventArgs e) { IHTMLDocument2 htmlDocument = HTMLEditor.Document.DomDocument as IHTMLDocument2; IHTMLSelectionObject currentSelection = htmlDocument.selection; if (currentSelection != null) { IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange; if (range != null) { MessageBox.Show("dans ma form1 "+range.text); montext = range.text; } } SelectLink(); } public void SelectLink() { using (Link dlg = new Link()) { dlg.ShowDialog(this.ParentForm); if (!dlg.Accepted) return; string link = dlg.URI; if (link == null || link.Length == 0) { MessageBox.Show(this.ParentForm, "Invalid URL"); return; } InsertLink(dlg.URL); MessageBox.Show("liiiinnnk " + link); HTMLEditor.Navigate(link); HTMLEditor.Document.ExecCommand("CreateLink", false, link); } } public void InsertLink(string url) { HTMLEditor.Document.ExecCommand("CreateLink", false, url); }
Merciiiii d'avance pour votre aide !! -
Bonjour Sarah,
Pourquoi dans la page HTML tu ne fais pas directement le lien entre "Aller sur Google" et www.google.be comme ceci:
<a ref="http://www.google.be">Aller sur Google </a>
fred on youTube
PHP MySQL Web Site Creator
fred -
-
quel est le type de fichier qui contient "aller vers google" que tu vas afficher dans ton WebBrowser ?
Est-ce un fichier texte ou HTML ?
Peut-tu nous donner un exemple de ce fichier ?
fred on youTube
PHP MySQL Web Site Creator
fred- Modifié fred75 lundi 23 janvier 2012 16:24
-
-
J'ai du mal à comprendre
Comment mets-tu "aller vers Google" directement dans ton WebBrowser ?
fred on youTube
PHP MySQL Web Site Creator
fred -
-
-
Bien sur ...
Donc il y a 2 form HTMLEditor et Link
dans HTMLEditor ;
private void link_Click(object sender, EventArgs e) { IHTMLDocument2 htmlDocument = HTMLEditor.Document.DomDocument as IHTMLDocument2; IHTMLSelectionObject currentSelection = htmlDocument.selection; if (currentSelection != null) { IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange; if (range != null) { MessageBox.Show("dans ma form1 "+range.text); montext = range.text; } } SelectLink(); } public void SelectLink() { using (Link dlg = new Link()) { dlg.ShowDialog(this.ParentForm); if (!dlg.Accepted) return; string link = dlg.URI; if (link == null || link.Length == 0) { MessageBox.Show(this.ParentForm, "Invalid URL"); return; } InsertLink(dlg.URL); MessageBox.Show("liiiinnnk " + link); HTMLEditor.Navigate(link); HTMLEditor.Document.ExecCommand("CreateLink", false, link); } } public void InsertLink(string url) { HTMLEditor.Document.ExecCommand("CreateLink", false, url); }
et dans la formLInk ;
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 editeurHtml { public partial class Link : Form { public bool _accepted = false; public Link() { InitializeComponent(); LoadUrls(); linkEdit.TextChanged += new EventHandler(linkEdit_TextChanged); } void linkEdit_TextChanged(object sender, EventArgs e) { label1.Text = URL; //MessageBox.Show("Variable URL"+URL); } public string URL { get { return comboBox1.Text + linkEdit.Text.Trim(); } } public string URI { get { return linkEdit.Text.Trim(); } } public bool Accepted { get { return _accepted; } } private void LinkDialog_Load(object sender, EventArgs e) { label1.Text = URL; BeginInvoke((MethodInvoker)delegate { linkEdit.Focus(); }); } private void LoadUrls() { string glob = Properties.Settings.Default.LinkDialogURLs; string[] urls = glob.Split(null); if (urls != null) { foreach (string url in urls) { linkEdit.Items.Add(url); } } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { label1.Text = URL; } private void OKbutton_Click_1(object sender, EventArgs e) { string url = linkEdit.Text; string glob = Properties.Settings.Default.LinkDialogURLs; if (glob == null) glob = ""; if (!glob.Contains(url)) { if (glob.Length > 0) glob += "\n"; glob += url; } Properties.Settings.Default.LinkDialogURLs = glob; Properties.Settings.Default.Save(); _accepted = true; Close(); //MessageBox.Show("url dans le click ok" + url ); } private void button2_Click(object sender, EventArgs e) { _accepted = false; Close(); } private void Link_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { } } }
-
Bonjour,
Cela me semble compliqué
Pourquoi ne pas ajouter "http://" à ton ComboBoxURL.SelectedItem() ?
WebBrowser.Navigate("http://" + ComboBoxURL.SelectedItem());
fred on youTube
PHP MySQL Web Site Creator
fred- Marqué comme réponse SarahChou mercredi 25 janvier 2012 10:31
-
-