Add item to ListBox (ListView) by user - c#

Can i make ListView or ListBox editable by user?
For example: user can add a new item at list (without any buttons).
Can i do that? Maybe give some simple example.
P.S. It is about WPF.

In winforms this is simple with a ComboBox. The Text is added if it is new when the user presses Enter:
comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.Simple;
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
if (!comboBox1.Items.Contains(comboBox1.Text))
comboBox1.Items.Add(comboBox1.Text);
}
There was no WPF tag at first, but the same should be possible in WPF as well..
(Make it editable, set the dropdown to visible and catch the enter key..can't provide code atm)
Update: After a rather quick check it seems WPF can't do it out of the box. I'm (somewhat) surprised that a useful control (an editable listbox) that has bee with Windows since the 90s (at least) is no longer there. But maybe I'm wrong..

There is no suitable method to do this trick without buttons. Try DataGridView instead http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx

Related

UWP CustomRenderer for Checkbox: Pointer over Checkbox changes style?

I'm working with Xamarin.Forms and I made a CustomRenderer for Checkbox in UWP. When I set all the Checkboxes of my items in the ListView to true by clicking the button "Alle", the Checkboxes are displayed correctly with the check inside the box:
However, if I hover my mouse over the Checkboxes, they immediately change their appearence (the check disappears but it's still selected). In the following screenshot, I moved my cursor over the 3rd - 7th Checkboxes:
This is my overridden OnElementChanged method in the CustomRenderer:
protected override void OnElementChanged(ElementChangedEventArgs<EvaCheckbox> e)
{
base.OnElementChanged(e);
var model = e.NewElement;
if (model == null)
{
return;
}
nativeCheckbox = new CheckBox();
CheckboxPropertyChanged(model, null);
model.PropertyChanged += OnElementPropertyChanged;
nativeCheckbox.Checked += (object sender, Windows.UI.Xaml.RoutedEventArgs eargs) =>
{
model.IsChecked = (bool)nativeCheckbox.IsChecked;
};
nativeCheckbox.Unchecked += (object sender, Windows.UI.Xaml.RoutedEventArgs eargs) =>
{
model.IsChecked = (bool)nativeCheckbox.IsChecked;
};
SetNativeControl(nativeCheckbox);
}
I tried to override the PointerEntered event of nativeCheckbox. It works, for example if I set the model.IsChecked to true on this event, it will be set to true:
nativeCheckbox.PointerEntered += (s, args) =>
{
model.IsChecked = true;
};
But I don't know how to (if even at this place) prevent the checkbox from changing it's appearance when moving the cursor above the Checkbox. Just leaving the triggered event with empty code like this won't change anything about the described behaviour:
nativeCheckbox.PointerEntered += (s, args) => { };
How can I prevent the Checkbox from changing it's appearance when I move my cursor over it?
Update:
I've created a sample project for this issue. You can find the repository here: https://github.com/Zure1/CustomCheckbox
It has the exact same described behavior. In the following screenshot I pressed the button "All" on the bottom of the screen and then the checkboxes look like correct with a check inside of them:
After moving the mouse cursor over the bottom 3 checkboxes, their change their appearance:
Information: I'm debugging on my desktop (Windows 10). I don't know if this issue exists on WinPhone. Just in case you're wondering why my checkboxes are red: My system color in Windows is red.
This is a tricky one as I have been struggling with this issue for a while, I'll try my best to answer this.
TL;DR: It's caused by ViewCell.
The issue comes down to Xamarin Forms ListView and ViewCell.
I haven't been able to track down the cause yet for many months and the way I get around this issue is by refreshing the ListView every time a change happens forcing a redraw of the entire ListView which can really impact performance.
My educated guess on what the cause could be is the rendering code for the ViewCell is missing something.
As for your particular issue, I have created a CheckBoxCell which you can use to display a list of checkboxes with a title. I forked your project and made the changes.
This will display something similar to what you are trying to achieve and doesn't have rendering issues so will be a good starting point. You are able to customize this to display images and the like but you'll have to do that in the platform-specific layout code.
Please note that I have only created the code for UWP and that should be enough to get you going for the other platforms.
I hope this helps somewhat.

Nested DataGridView Control

I am trying to create a nested DataGridView control where there will be two levels of nesting that are open at all times. It will look similar to the picture on this page: http://www.codeproject.com/Articles/12657/GridView-inline-Master-Detail-record-display. The difference will be that each subnesting will always be open and it is not necessary to have a way for the user to open/close each nesting. This control will only be used for displaying data, so there will be no need to modify the data directly from this control (even though the user will not modify the data directly it can still be changed).
If this can not be done with DataGridView, is there any other control that would allow for this.
If not does anyone know another way to do this. I can, but they would be tedious to implement. One way would be to add multiple DataGridView controls in sequence (2N DataGridControls for N categories). The other would add it all manually with static controls.
I don't know if this will give the answer, but it might make u some door.
//button to call function that looks for DatagridView control
private void button2_Click(object sender, EventArgs e)
{
scanDG(this);
}
private void scanDG(Control parent)
{
foreach (Control ctrl in parent.Controls)
{
if (ctrl.GetType().Name == "DataGridView")
{//If current Control is Datagridview then set Readonly to true
((DataGridView)ctrl).ReadOnly = true;
}
//If a control can contain control scan it and look for Datagridview control
if (ctrl.HasChildren) scanDG(ctrl);
}
}

TextBox that Gets the value of another TextBox VISUAL C#

Is It possible that if I create two TextBoxes.
When the first TextBox is modified from input, the second text box is set to be read only and its value will update depending on what you had written in the first text box.
It's like when I am posting here in stackoverflow there is also a read only area that follows what I'm typing (The preview window). :)) Thanks!!!
If it's win-form application, it's so simple. try this :
private void txtFirstTextBox_TextChanged(object sender, EventArgs e) {
if (string.IsNullOrEmpty(txtFirstTextBox.Text)) {
txtSecondTextBox.Clear();
return;
}
txtSecondTextBox.Text = txtFirstTextBox.Text;
}
hope this help.
I should note: This is a solution if you're using WPF for your UI.
Yes that's easily possible if you have, for example the first textbox:
<TextBox x:Name="FirstBox"/>
You can bind to this text box's content via:
<TextBox x:Name"SecondBox" Text="{Binding ElementName="FirstBox", Path="Text", UpdateSourceTrigger=PropertyChanged}" IsEnabled="False"/>
And when the first text box changes, the second one should follow suit. This is all handled automatically for you via binding, it connects to the Text property on the TextBox named "FirstBox". This second TextBox is disabled by setting the IsEnabled property to "False"
Since there is already a WPF Solution and you didn't specify which you are using, I'll go ahead and post a WinForms solution.
Luckily, this is relatively simple in WinForms as well. You simply wire a TextChanged event handler for the first text box which updates the text of the second:
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = ((TextBox)sender).Text;
}

How can I set an icon for a ListViewSubItem?

In a ListView you can have icons on each item.
When viewing in Details-mode, the icon is shown in the left-most column.
Can I show an icon in some other column?
The ListView control does not support images in sub-items natively. The easiest thing to do is switch to a DataGridView and use a DataGridViewImageColumn. If that is not possible, then you'll need to draw the icons yourself using the custom draw support in the ListView control. To do this set ListView.OwnerDraw = true and handle the ListView.DrawSubItem and ListView.DrawColumnHeader events.
private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
// Only interested in 2nd column.
if (e.Header != this.columnHeader2)
{
e.DrawDefault = true;
return;
}
e.DrawBackground();
var imageRect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height);
e.Graphics.DrawImage(SystemIcons.Information.ToBitmap(), imageRect);
}
private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
e.DrawDefault = true;
}
Use P/Invoke and send LVM_SETITEM message to the listview (you should set LVS_EX_SUBITEMIMAGES style on control creation or via LVM_SETEXTENDEDLISTVIEWSTYLE), specify the subitem index and the corresponding image index. You will need to do it for every list item you insert.
ObjectListView is an open source wrapper around a .NET Winforms ListView. It supports images on subitems using the p/invoke strategy that that #ligget78 mentioned. It also solves many other common problems with a ListView.
It allows you to make very nice looking ListViews with a minimum effort:
(source: sourceforge.net)
Inherit from ListView and draw your own icons.
public class MyListView : ListView
{
protected override void OnDrawSubItem(System.Windows.Forms.DrawListViewSubItemEventArgs e)
{
base.OnDrawSubItem(e);
}
}
The icon is shown in the "first" column, and this is also the basis for the keyboard prefix search. One possible solution could be to reorder the columns by setting the DisplayIndex of the first column to something else.
listView1.Columns[0].DisplayIndex = 1;
This of course only works if you need an icon in only one column.
There's no .NET support for this.
Have a look at this project.
Take a loot at this:
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/d25b4ffa-2ea4-43cd-a3ae-8dd0387197ae/
In addition to the accepted answer, you should handle the DrawItem event as well, or it will not work.

Redraw Toolstrip based on selections

I have been asked to write c# winforms app that will give users the ability to select options from a checkbox list and have it automatically redraw/repaint a toolstrip with the selected items.
I am new to winforms so I am not sure how to approach it. Should I be using the BackgroundWorker Process? Invalidate()?
Just alittle confused.
Any assistence of pointing in the right direction would be appreciated.
You probably don't want a BackgroundWorker as that's run on a non-UI thread and would cause problems when you try to modify the toolstrip (you can only work with the UI on the thread the UI was created on). Handle the CheckedChanged events on the checkboxes and then add or remove items from the toolstrip. The repainting should be automatic.
You need to keep tooltips for all options some where (if Tag property of checkboxes is free the put it there). Then when an option is selected or deselected, you need to update tooltips.
Let's suppose you are adding all the checkboxes in a IList. then things will work as follows:
private IList<CheckBox> options= new List<CheckBox>();
private void UpdateTTip()
{
toolTip1.RemoveAll();
foreach (CheckBox c in options)
{
if (c.Checked)
toolTip1.SetToolTip(c, c.Tag.ToString());
}
}
Now you need to call this on checkedchanged event of options check boxes:
private void chk_CheckedChanged(object sender, EventArgs e)
{
UpdateTTip();
}
A toolstrip contains controls by itself - it does not just "paint" buttons you can press. In order to have the toolstrip display different buttons depending on different conditions, you can:
Clear the toolstrip items and re-create the ones that are needed in the current context in code when items are checked in the list you mentioned
Add all the items and design time (with property Visible = false) and only set the necessary ones to Visible = true upon selection in your check listbox
No need to do any painting :-)

Categories