ListBox HorizontalScrollbar doen't work WinForms - c#

I have a ListBox in winforms , C# application.
i need to add to the ListBox CheckBoxes in the code , and i need that HorizontalScrollbar will appear when there is a lot of CheckBoxes and the ListBox can't show them all.
I have placed the ListBox on the Window and set it's HorizontalScrollbar to be true. Also i have set the maximum height and width for the ListBox.But when i run the application , i can't see all the CheckBoxes...
can anyone help with that?
this the code for adding the checkboxes :
listBox1.HorizontalScrollbar = true;
this.groupBox_ChooseTCPipConfg.Controls.Add(listBox1);
CheckBox chk1 = new CheckBox();
chk1.Text = "chk1";
chk1.Location = new Point(2, 2);
listBox1.Controls.Add(chk1);
CheckBox chk2 = new CheckBox();
chk2.Text = "chk2";
chk2.Location = new Point(2,22);
listBox1.Controls.Add(chk2);
CheckBox chk3 = new CheckBox();
chk3.Text = "chk3";
chk3.Location = new Point(2, 42);
listBox1.Controls.Add(chk3);
CheckBox chk4 = new CheckBox();
chk4.Text = "chk4";
chk4.Location = new Point(2, 62);
listBox1.Controls.Add(chk4);
CheckBox chk5 = new CheckBox();
chk5.Text = "chk5";
chk5.Location = new Point(2, 82);
listBox1.Controls.Add(chk5);
CheckBox chk6 = new CheckBox();
chk6.Text = "chk6";
chk6.Location = new Point(2, 102);
listBox1.Controls.Add(chk1);
CheckBox chk7 = new CheckBox();
chk7.Text = "chk7";
chk7.Location = new Point(2, 122);
listBox1.Controls.Add(chk7);
CheckBox chk8 = new CheckBox();
chk8.Text = "chk8";
chk8.Location = new Point(2, 142);
listBox1.Controls.Add(chk8);
CheckBox chk9 = new CheckBox();
chk9.Text = "chk9";
chk9.Location = new Point(2, 162);
listBox1.Controls.Add(chk9);
CheckBox chk10 = new CheckBox();
chk10.Text = "chk10";
chk10.Location = new Point(2,202);
listBox1.Controls.Add(chk10);
when i run the application , i can only see the first 7 checkboxes....

You dont' have ListBox scroll because you didn't add any items to it. You just place the CheckBox controls inside. Scroll appears oly if you will have to many items.
For such purposes use FlowLayoutPanel control.
Here is how properties should be set for FlowLayoutPanel

Try setting ListBox.HorizontalExtent
Do not forget to read Remarks on above msdn reference.

Related

How to set dynamic text for textblock from code behind?

I am trying to read an "ini" file and to create multiple radiobuttons depending on the number of the lines of this file. The code used is the following:
private void CreateRadioButtons(string filePath)
{
StackPanel mainStackPanel = new StackPanel();
mainStackPanel.Orientation = Orientation.Vertical;
mainStackPanel.HorizontalAlignment = HorizontalAlignment.Stretch;
mainStackPanel.VerticalAlignment = VerticalAlignment.Stretch;
var lines = File.ReadAllLines(filePath).Where(l => !l.StartsWith(";"));
WrapPanel wrapPanel = new WrapPanel();
wrapPanel.Margin = new Thickness(30);
ScrollViewer scrollViewer = new ScrollViewer();
Style style = new Style(typeof(RadioButton));
style.Setters.Add(new Setter(RadioButton.BackgroundProperty, Brushes.Yellow));
style.Setters.Add(new Setter(RadioButton.BorderThicknessProperty, new Thickness(2)));
ControlTemplate controlTemplate = new ControlTemplate(typeof(RadioButton));
FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border));
border.SetValue(Border.BorderThicknessProperty, new TemplateBindingExtension(BorderThicknessProperty));
border.SetValue(Border.BorderBrushProperty, new TemplateBindingExtension(BorderBrushProperty));
border.SetValue(Border.BackgroundProperty, Brushes.Transparent);
border.SetValue(Border.CornerRadiusProperty, new CornerRadius(20));
border.SetValue(BorderBrushProperty, new SolidColorBrush(Colors.Blue));
border.SetValue(Border.BackgroundProperty, Brushes.Transparent);
FrameworkElementFactory image = new FrameworkElementFactory(typeof(Image));
image.SetValue(Image.SourceProperty, new BitmapImage(new Uri("pack://application:,,,/Resources/image.jpg")));
FrameworkElementFactory textBlock = new FrameworkElementFactory(typeof(TextBlock));
textBlock.Name = "TextBlock";
textBlock.SetValue(TextBlock.TextProperty, "Text");
textBlock.SetValue(TextBlock.MarginProperty, new Thickness(5));
FrameworkElementFactory secondaryStack = new FrameworkElementFactory(typeof(StackPanel));
secondaryStack.SetValue(StackPanel.OrientationProperty, Orientation.Vertical);
secondaryStack.SetValue(StackPanel.MarginProperty, new Thickness(5, 10, 5, 0));
secondaryStack.AppendChild(image);
secondaryStack.AppendChild(textBlock);
border.AppendChild(secondaryStack);
controlTemplate.VisualTree = border;
style.Setters.Add(new Setter(Control.TemplateProperty, controlTemplate));
Trigger trigger = new Trigger() { Property = RadioButton.IsCheckedProperty, Value = false };
trigger.Setters.Add(new Setter(UIElement.OpacityProperty, 0.5));
style.Triggers.Add(trigger);
FrameworkElementFactory label = new FrameworkElementFactory(typeof(Label));
foreach (var line in lines)
{
RadioButton radioButton = new RadioButton();
radioButton.Style = style;
radioButton.Width = 135;
radioButton.Height = 125;
radioButton.Margin = new Thickness(15, 0, 0, 15);
radioButton.Content = border;
wrapPanel.Children.Add(radioButton);
}
This code is functioning at the moment. The problem comes when i try to implement textblocks with dynamic text: I just need the text inside the radiobutton to be equal to the string line. I have tried multiple ways but in the end I only encounter exceptions about windowspresentation (which I think mean that the elements are presented in the wrong way). How can I make textBlock dynamic?

C# winform checkbox still checked

In a button click function, I set checkbox.check = false;
but when I click button again, checkbox still in checked
here is my code:
// Header全選加上CheckBox
System.Drawing.Rectangle rect = dataGridView1.GetCellDisplayRectangle(0, -1, true);
rect.X = rect.Location.X + rect.Width / 4 - 9;
rect.Y = rect.Location.Y + (rect.Height / 2 - 9);
SolidBrush solidBrush = new SolidBrush(Color.White);
System.Windows.Forms.CheckBox checkBox = new System.Windows.Forms.CheckBox();
checkBox.Name = "checkBoxHeader";
checkBox.Size = new Size(18, 18);
checkBox.Location = rect.Location;
//checkBox.AutoCheck = false;
//MessageBox.Show(checkBox.Checked.ToString());
checkBox.Checked = false;
checkBox.Invalidate();
checkBox.Update();
dataGridView1.RefreshEdit();
checkBox.CheckedChanged += new EventHandler(checkBox_CheckedChanged);
dataGridView1.Controls.Add(checkBox);
I set MessageBox to show checkbox value, but the value is "False"
How can I set checkbox is unchecked when I click botton?
Thanks a lot
In the button click method you instantiate a new checkbox and the state of it is always set to false. You have to instantiate the checkbox and setting the properties of it in the constructor of your form or you can drag a checkbox onto the form when using the designer.
Example
private Checkbox _checkbox;
public Form1()
{
InitializeComponents();
_checkbox = new CheckBox();
//set properties
_checkbox.Checked = true;
//other properties you want to set
//add checkbox to your form for example
datagridview1.Controls.Add(_checkbox);
}
Then in your button click method just write:
_checkbox.Checked = false;
Thanks for answering, I solve this question.
I set code as
foreach (Control ckb in dataGridView1.Controls)
{
if (ckb is System.Windows.Forms.CheckBox)
{
((System.Windows.Forms.CheckBox)ckb).Checked = false;
}
}
and the checkbox is unchecked when I click button again

How to add scroollbar to combo box items in c#

I am working on silverlight using c#. I have to display the combo items in a scrollbar.
My attempt to do this is:
TextBlock txtblkName = generateTextBlock();
ComboBox cb = new ComboBox();
ScrollViewer scrollViewer = new ScrollViewer();
cb.Width = 45;
cb.Height = 20;
foreach (String item in param.Component.Attributes.Items)
cb.ItemsSource = param.Component.Attributes.Items;
scrollViewer.Content = cb;
scrollViewer.HorizontalAlignment = HorizontalAlignment.Center;
scrollViewer.VerticalAlignment = VerticalAlignment.Center;
scrollViewer.ScrollToVerticalOffset(3);
cb.SelectionChanged += (o, e) =>
{
txtblkName.Text = cb.SelectedValue.ToString() + " " + param.Unit;
};
cb.SelectedIndex = param.Component.Attributes.Selected != -1 ? param.Component.Attributes.Selected : 0;
Grid.SetColumn(scrollViewer, 1);
childGrid.Children.Add(scrollViewer);
which results in scroll over combo box .Like this:
Not on it's item displayed by scrollbar.
Could some one please help me to create scrollbar only on items displayed not on all combo box?
You dont need ScrollViewer here, If you need Scrollbar for your comboboxItems set this property MaxDropDownHeight to some value
ComboBox cb = new ComboBox();
List<string> items = new List<string>();
items.Add("1");
items.Add("2");
items.Add("3");
items.Add("5");
items.Add("7");
items.Add("8");
cb.ItemsSource = items;
cb.MaxDropDownHeight = 20;
childGrid.Children.Add(cb);

Set Listviewitem's width

I have a Listview and it has one column. The view is set to List and I can see each Listviewitem but I can't select the item's row, I have to select the item's text. Is it possible to make it so that the Listviewitem's width is the same size as the Listview itself so that the user can click anywhere on the Listviewitem to select the item?
I tried searching but could only find how to change the column width and how to fix it in XAML, but this is for a WinForm.
Edit - As requested, this is the code that is generated by the Visual Studio designer. It is the only code involved with the ListviewItem.
System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem("1");
System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem("2");
System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem("3");
System.Windows.Forms.ListViewItem listViewItem4 = new System.Windows.Forms.ListViewItem("4");
System.Windows.Forms.ListViewItem listViewItem5 = new System.Windows.Forms.ListViewItem("5");
System.Windows.Forms.ListViewItem listViewItem6 = new System.Windows.Forms.ListViewItem("6");
System.Windows.Forms.ListViewItem listViewItem7 = new System.Windows.Forms.ListViewItem("7");
this.listView1 = new System.Windows.Forms.ListView();
this.listView1.Activation = System.Windows.Forms.ItemActivation.OneClick;
this.listView1.AllowDrop = true;
this.listView1.AutoArrange = false;
this.listView1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.listView1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.listView1.ForeColor = System.Drawing.SystemColors.MenuHighlight;
this.listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
listViewItem1.StateImageIndex = 0;
this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
listViewItem1,
listViewItem2,
listViewItem3,
listViewItem4,
listViewItem5,
listViewItem6,
listViewItem7});
this.listView1.Location = new System.Drawing.Point(105, 129);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(157, 475);
this.listView1.TabIndex = 4;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.List;
A simple solution will be using a ListBox instead of List View
In case of simple text, it is better to use ListBox.
Update:
Simplest hack for getting this functionality in ListView will be adding dummy spaces at the end of each item string to fill.
Better ListView and Better ListView Express (free) support this. It behaves like a ListBox by default (Details view, no columns):
The item auto sizing can be triggered by setting AutoSizeItemsInDetailsView property to true:
betterListView.AutoSizeItemsInDetailsView = true;
Set listView1.FullRowSelect = true

Placing controls inside a panel in C#

private void newThumbNail(int docType, string fileName)
{
thmbNail[thmbNailCnt] = new GroupBox();
thmbNail[thmbNailCnt].Parent = panel1;
thmbNail[thmbNailCnt].Visible = true;
thmbNail[thmbNailCnt].Location = new Point(2, 5);
thmbNail[thmbNailCnt].Size = new Size(222, 50);
picBox[thmbNailCnt] = new PictureBox();
picBox[thmbNailCnt].Parent = thmbNail[thmbNailCnt];
picBox[thmbNailCnt].Visible = true;
picBox[thmbNailCnt].Location = new Point(6, 13);
picBox[thmbNailCnt].Size = new Size(31, 31);
//picBox[thmbNailCnt].Image = new Bitmap("images/excel.png");
texBox[thmbNailCnt] = new TextBox();
texBox[thmbNailCnt].Parent = thmbNail[thmbNailCnt];
texBox[thmbNailCnt].Visible = true;
texBox[thmbNailCnt].Location = new Point(53, 24);
texBox[thmbNailCnt].Size = new Size(163, 20);
texBox[thmbNailCnt].Text = fileName;
texBox[thmbNailCnt].Enabled = false;
Controls.Add(thmbNail[thmbNailCnt]);
Controls.Add(picBox[thmbNailCnt]);
Controls.Add(texBox[thmbNailCnt]);
}
this is a function that dynamically adds a groupBox with some controls int it inside a panel. Unfortunately it does not appears inside the panel. The panel was created before hand using the c# design tools. It is placed directly on top of the windows form at 15,52 having a size of 279,489. Help please.
It seems that you are adding these controls to the form controls collection.
Instead you should use the panel controls collection like:
panel1.Controls.Add(thmbNail[thmbNailCnt]);
panel1.Controls.Add(picBox[thmbNailCnt]);
panel1.Controls.Add(texBox[thmbNailCnt]);
Try Panel.Controls.Add(thmbNail[thmbNailCnt])
Also a tip to make your code faster and easier to read:
// Not modified to use Panel.Controls.Add()
GroupBox box = new GroouBox();
thmbNail[thmbNailCnt] = box;
box.Parent = panel1;
box.Visible = true;
box.Location = new Point(2, 5);
box.Size = new Size(222, 50);

Categories