Bonsoir à tous,
J'ai besoin de forcer la sélection de ++ éléments d'une listBox lors du chargement de la page, je ne sais pas comment forcer cette sélection.
merci de m'aider.
Version imprimable
Bonsoir à tous,
J'ai besoin de forcer la sélection de ++ éléments d'une listBox lors du chargement de la page, je ne sais pas comment forcer cette sélection.
merci de m'aider.
Listbox.SelectedItem, ou listbox.SelectedIndex
merci de m'avoir répondu, j'ai testé Listbox.SelectedItem mais ça permet juste de déterminer quel élément a été sélectionné par l'utilisateur, ce que j'en ai besoin c de savoir comment sélectionner un élement à partir du code C# sans un click sur l'élément.
merci de m'aider.
Tu peux assigner une valeur aux deux propriétés qu'a donné EquinoxeDotNet.
Pour sélectionner le n ème élément de ta ListBox, tu fais :et pour sélectionner un élément en particulier :Code:maListBox.SelectedIndex = n;
Code:maListBox.SelectedItem = monItem;
J'ai assigné une valeur à selectedItem mais ça me renvoie l'erreur suivante :
SelectedItem ne permet pas de sélectionner mais juste de renvoyer l'élément sélectionné par l'utilisateur.Citation:
La propriété ou l'indexeur 'System.Web.UI.WebControls.ListControl.SelectedItem' ne peut pas être assigné -- il est en lecture seule
Etrange, j'ai testé en windows form, asp et wpf, je n'ai pasd'erreur.
On peut voir ton code?
Mes tests :
Une listebox, avec 3 éléments et un bouton. Au clic, je sélectionne le dernier :
Winform :
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 namespace WindowsFormsApplication1 { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.listBox1 = new System.Windows.Forms.ListBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // listBox1 // this.listBox1.FormattingEnabled = true; this.listBox1.Items.AddRange(new object[] { "TOTO", "TITI", "TATA"}); this.listBox1.Location = new System.Drawing.Point(13, 13); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(120, 95); this.listBox1.TabIndex = 0; // // button1 // this.button1.Location = new System.Drawing.Point(13, 115); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 1; this.button1.Text = "button1"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.Add(this.button1); this.Controls.Add(this.listBox1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion private System.Windows.Forms.ListBox listBox1; private System.Windows.Forms.Button button1; } }
WPF :Code:
1
2
3
4
5 private void button1_Click(object sender, EventArgs e) { listBox1.SelectedIndex = 2; }
etCode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14 <Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <Grid> <ListBox Height="100" Margin="12,12,0,0" Name="listBox1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120"> <ListBoxItem>TOTO</ListBoxItem> <ListBoxItem>TITI</ListBoxItem> <ListBoxItem>TATA</ListBoxItem> </ListBox> <Button Margin="12,118,0,121" Name="button1" HorizontalAlignment="Left" Width="75" Click="button1_Click">Button</Button> </Grid> </Window>
asp :Code:
1
2
3
4
5 private void button1_Click(object sender, RoutedEventArgs e) { listBox1.SelectedIndex = 2; }
etCode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:ListBox ID="ListBox1" runat="server"> <asp:ListItem>TOTO</asp:ListItem> <asp:ListItem>TITI</asp:ListItem> <asp:ListItem>TATA</asp:ListItem> </asp:ListBox> </div> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> </form> </body> </html>
Code:
1
2
3
4
5 protected void Button1_Click(object sender, EventArgs e) { ListBox1.SelectedIndex = 2; }