public partial class Form1 : Form { private ListBox LstMots; private Label LblPhrase; public Form1() { string phrase = "Essai pour masquer un mot par un label en comptant sur votre aide pour résoudre mon problème"; SuspendLayout(); WindowState = FormWindowState.Maximized; //button_cde pour masquer le mot choisi dans la listbox Button b = new Button(); b.Visible = true; b.AutoSize = true; b.Text = "Masquer"; b.Location = new Point(20, 20); b.Click += BtnMasquerMot_Click; Controls.Add(b); //button_cde pour effacer les masques b = new Button(); b.Visible = true; b.AutoSize = true; b.Text = "Effacer"; b.Location = new Point(20, 20 + b.Height); b.Click += BtnEffacerMasque_Click; Controls.Add(b); //listbox_mot contenant les mots de la phrase string[] words = phrase.Split(new Char[] { ' ' }); LstMots = new ListBox(); LstMots.Visible = true; LstMots.Location = new Point(40 + b.Width, 20); LstMots.Items.AddRange(words); Controls.Add(LstMots); LstMots.SelectedIndexChanged += new EventHandler(LstMots_SelectedIndexChanged); //label_texte affichant la phrase LblPhrase = new Label(); LblPhrase.Visible = true; LblPhrase.BackColor = Color.Transparent; LblPhrase.Font = new Font("Arial", 14); LblPhrase.Text = phrase; LblPhrase.Location = new Point(20, LstMots.Top + LstMots.Height + 20); using (Graphics g = LblPhrase.CreateGraphics()) { SizeF S = g.MeasureString(LblPhrase.Text, LblPhrase.Font); LblPhrase.Height = (int)S.Height+1; LblPhrase.Width = (int)S.Width+1; } LblPhrase.Paint += new PaintEventHandler(LblPhrase_Paint); Controls.Add(LblPhrase); ResumeLayout(true); } void LstMots_SelectedIndexChanged(object sender, EventArgs e) { LblPhrase.Refresh(); } void LblPhrase_Paint(object sender, PaintEventArgs e) { SolidBrush b = new SolidBrush(Color.FromKnownColor(KnownColor.Control)); e.Graphics.FillRectangle(b, new Rectangle(0, 0, LblPhrase.Width, LblPhrase.Height)); Pen p = new Pen(Color.Black); e.Graphics.DrawString(LblPhrase.Text, LblPhrase.Font, new SolidBrush(Color.Black), 0, 0, null); if (LstMots.SelectedItem == null) return; String Mot = LstMots.SelectedItem.ToString(); int nPos = LblPhrase.Text.IndexOf(Mot); CharacterRange[] CharRanges = { new CharacterRange(nPos, Mot.Length) }; StringFormat StrFormat = new StringFormat(StringFormatFlags.FitBlackBox | StringFormatFlags.NoClip | StringFormatFlags.LineLimit); StrFormat.SetMeasurableCharacterRanges(CharRanges); SizeF S = e.Graphics.MeasureString(LblPhrase.Text, LblPhrase.Font); Region[] Rgn = e.Graphics.MeasureCharacterRanges(LblPhrase.Text, LblPhrase.Font, new RectangleF(new PointF(0, 0), S), StrFormat); RectangleF bounds = Rgn[0].GetBounds(e.Graphics); Text = bounds.X + " " + bounds.Y + " " + bounds.Width + " " + bounds.Height; e.Graphics.DrawRectangle(p, bounds.X, bounds.Y, bounds.Width, bounds.Height); } private void BtnMasquerMot_Click(object sender, System.EventArgs e) { LblPhrase.Refresh(); } private void BtnEffacerMasque_Click(object sender, System.EventArgs e) { LstMots.SelectedItem = null; LblPhrase.Refresh(); } }
Public Class Form1 Dim LstMots As ListBox Dim LblPhrase As Label Public Sub New() Dim phrase As String = "Essai pour masquer un mot par un label en comptant sur votre aide pour résoudre mon problème" SuspendLayout() WindowState = FormWindowState.Maximized 'button_cde pour masquer le mot choisi dans la listbox Dim b As New Button() b.Visible = True b.AutoSize = True b.Text = "Masquer" b.Location = New Point(20, 20) b.Click += BtnMasquerMot_Click Controls.Add(b) 'button_cde pour effacer les masques b = New Button() b.Visible = True b.AutoSize = True b.Text = "Effacer" b.Location = New Point(20, 20 + b.Height) b.Click += BtnEffacerMasque_Click Controls.Add(b) 'listbox_mot contenant les mots de la phrase Dim words As String() = phrase.Split(New Char() {" "C}) LstMots = New ListBox() LstMots.Visible = True LstMots.Location = New Point(40 + b.Width, 20) LstMots.Items.AddRange(words) Controls.Add(LstMots) AddHandler LstMots.SelectedIndexChanged, AddressOf LstMots_SelectedIndexChanged 'label_texte affichant la phrase LblPhrase = New Label() LblPhrase.Visible = True LblPhrase.BackColor = Color.Transparent LblPhrase.Font = New Font("Arial", 14) LblPhrase.Text = phrase LblPhrase.Location = New Point(20, LstMots.Top + LstMots.Height + 20) Using g As Graphics = LblPhrase.CreateGraphics() Dim S As SizeF = g.MeasureString(LblPhrase.Text, LblPhrase.Font) LblPhrase.Height = DirectCast(S.Height, Integer) + 1 LblPhrase.Width = DirectCast(S.Width, Integer) + 1 End Using AddHandler LblPhrase.Paint, AddressOf LblPhrase_Paint Controls.Add(LblPhrase) ResumeLayout(True) End Sub Sub LstMots_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) LblPhrase.Refresh() End Sub Sub LblPhrase_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Dim b As New SolidBrush(Color.FromKnownColor(KnownColor.Control)) e.Graphics.FillRectangle(b, New Rectangle(0, 0, LblPhrase.Width, LblPhrase.Height)) Dim p As New Pen(Color.Black) e.Graphics.DrawString(LblPhrase.Text, LblPhrase.Font, New SolidBrush(Color.Black), 0, 0, Nothing) If LstMots.SelectedItem Is Nothing Then Return End If Dim Mot As String = LstMots.SelectedItem.ToString() Dim nPos As Integer = LblPhrase.Text.IndexOf(Mot) Dim CharRanges As CharacterRange() = {New CharacterRange(nPos, Mot.Length)} Dim StrFormat As New StringFormat(StringFormatFlags.FitBlackBox Or StringFormatFlags.NoClip Or StringFormatFlags.LineLimit) StrFormat.SetMeasurableCharacterRanges(CharRanges) Dim S As SizeF = e.Graphics.MeasureString(LblPhrase.Text, LblPhrase.Font) Dim Rgn As Region() = e.Graphics.MeasureCharacterRanges(LblPhrase.Text, LblPhrase.Font, New RectangleF(New PointF(0, 0), S), StrFormat) Dim bounds As RectangleF = Rgn(0).GetBounds(e.Graphics) Text = bounds.X + " " + bounds.Y + " " + bounds.Width + " " + bounds.Height e.Graphics.DrawRectangle(p, bounds.X, bounds.Y, bounds.Width, bounds.Height) End Sub Private Sub BtnMasquerMot_Click(ByVal sender As Object, ByVal e As System.EventArgs) LblPhrase.Refresh() End Sub Private Sub BtnEffacerMasque_Click(ByVal sender As Object, ByVal e As System.EventArgs) LstMots.SelectedItem = Nothing LblPhrase.Refresh() End Sub End Class
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionSub LblPhrase_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) If LstMots.SelectedItem Is Nothing Then Return 'Dim b As New SolidBrush(Color.FromKnownColor(KnownColor.Control)) 'e.Graphics.FillRectangle(b, New Rectangle(0, 0, LblPhrase.Width, LblPhrase.Height)) Dim p As New Pen(Color.Black) 'e.Graphics.DrawString(LblPhrase.Text, _ ' LblPhrase.Font, _ ' New SolidBrush(Color.Black), _ ' 0, _ ' 0, _ ' Nothing) 'If LstMots.SelectedItem Is Nothing Then 'Return 'End If Dim Mot As String = LstMots.SelectedItem.ToString() Dim nPos As Integer = LblPhrase.Text.IndexOf(Mot) Dim CharRanges As CharacterRange() = {New CharacterRange(nPos, Mot.Length)} Dim StrFormat As New StringFormat(StringFormatFlags.FitBlackBox _ Or StringFormatFlags.NoClip _ Or StringFormatFlags.LineLimit) StrFormat.SetMeasurableCharacterRanges(CharRanges) Dim S As SizeF = e.Graphics.MeasureString(LblPhrase.Text, LblPhrase.Font) Dim Rgn As Region() = e.Graphics.MeasureCharacterRanges(LblPhrase.Text, _ LblPhrase.Font, _ New RectangleF(New PointF(0, 0), S), _ StrFormat) Dim bounds As RectangleF = Rgn(0).GetBounds(e.Graphics) Me.Text = bounds.X.ToString + " " + bounds.Y.ToString + " " + bounds.Width.ToString + " " + bounds.Height.ToString e.Graphics.DrawRectangle(p, bounds.X, bounds.Y, bounds.Width, bounds.Height) End Sub
Using g As Graphics = LblPhrase.CreateGraphics() Dim S As SizeF = g.MeasureString(LblPhrase.Text, LblPhrase.Font) 'LblPhrase.Height = DirectCast(S.Height, Integer) + 1 'LblPhrase.Width = DirectCast(S.Width, Integer) + 1 LblPhrase.Height = CInt(S.Height) + 1 LblPhrase.Width = CInt(S.Width) + 1 End Using 'LblPhrase.AutoSize = True
Private Sub debuter_analyse() 'Inhibition des sub pour eviter leurs appels lors des form8.ListView.Items(x).Checked = True/false RemoveHandler Form8.ListView_themes.ItemChecked, AddressOf Form8.ListView_themes_ItemChecked ..... 'Remplissage de ListView_themes For index_theme = 0 To array_theme.Length - 1 Form8.ListView_themes.Items.Add(array_theme(index_theme)) Form8.ListView_themes.Items(index_theme).Checked = True Next .... 'Activation des sub form8.ListView.ItemChecked AddHandler Form8.ListView_themes.ItemChecked, AddressOf Form8.ListView_themes_ItemChecked End Sub