###前提・実現したいこと
C#でラベルをスクロールするソフトを作成中です。しかし、ラベルをループさせる方法がわかりません。(webサイトから取得してスクロールさせる)
###発生している問題・エラーメッセージ
ラベルをループさせたいが、バグってしまう(動かない)
label1.Left = this.ClientSize.Width - label1.Width;
###該当のソースコード
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Threading.Tasks; 9using System.Windows.Forms; 10using System.IO; 11using System.Net; 12using HtmlAgilityPack; 13 14namespace label 15{ 16 public partial class Form1 : Form 17 { 18 public Form1() 19 { 20 InitializeComponent(); 21 timer1.Start(); 22 timer2.Start(); 23 } 24 25 private void button2_Click_1(object sender, EventArgs e) 26 { 27 label1.Text = ""; 28 29 const string url = "xxxxxxxxxxxxxxxxxxxxxxx";//URLがはいります 30 WebClient web = new WebClient(); 31 web.Encoding = System.Text.Encoding.UTF8; 32 string html = web.DownloadString(url); 33 34 HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); 35 doc.LoadHtml(html); 36 37 HtmlNodeCollection nodes = 38 doc.DocumentNode.SelectNodes(@"/html[1]/body[1]/div[1]"); 39 40 foreach (HtmlNode node in nodes) 41 42 { 43 label1.Text = node.InnerText; 44 } 45 46 timer1.Start(); 47 } 48 49 private void checkBox1_CheckedChanged(object sender, EventArgs e) 50 { 51 if (checkBox1.Checked) 52 { 53 timer2.Start(); 54 } 55 else 56 { 57 timer2.Stop(); 58 } 59 } 60 61 private void timer2_Tick(object sender, EventArgs e) 62 { 63 label1.Text = ""; 64 65 const string url = "xxxxxxxxxxxxxxxxxxxxxxx";//URLがはいります 66 WebClient web = new WebClient(); 67 web.Encoding = System.Text.Encoding.UTF8; 68 string html = web.DownloadString(url); 69 70 HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); 71 doc.LoadHtml(html); 72 73 HtmlNodeCollection nodes = 74 doc.DocumentNode.SelectNodes(@"/html[1]/body[1]/div[1]"); 75 76 foreach (HtmlNode node in nodes) 77 78 { 79 label1.Text = node.InnerText; 80 } 81 } 82 83 private void timer1_Tick(object sender, EventArgs e) 84 { 85 label1.Left -= 2; // 左に動かす 86 if (label1.Left < 0) 87 { 88 // 左がはみ出すので、右端に戻す 89 label1.Left = this.ClientSize.Width - label1.Width; 90 91 } 92 } 93 } 94} 95
###試したこと
this.label1.Location = new Point(10, 56);
###補足情報(言語/FW/ツール等のバージョンなど)
使用ツール:Html Agility Pack Tester
C# VisualStudio2017
回答3件
あなたの回答
tips
プレビュー