Comboboxes and changing items with selections - combobox

I am trying to figure out how to set up my code so that when an option is selected in a combo box, it changes the text of a label as well. From what I understand it involves something called an item listener but we did not discuss anything about that in class. We recently went over inheritance and interfaces but I do not see how that would apply here.

//this event is fired whenever user selects or change a item from combobox
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
//do whatever you want to do in this
}
for android do this
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});

Related

Winform Timer isn't running if statement when criteria is met

Hi I've come across either a weird bug or I'm not understanding something.
To cut a long story short I've had everything I'm wanting to work on my form working fine, I then decided to turn the form into an options menu so it was no longer the first form that appears when the application is launched and is shown after I click a button on a different form with the code
private void ShowOptionsButton_Click(object sender, EventArgs e)
{
formHomePage.Show();
}
And for some reason a timer if statement is no longer working:
private void StartSubCheckT_Tick(object sender, EventArgs e)
{
if (subliminalMessages.Count > 0)
{
MessageBox.Show("list greater than 0 if");
StartSubB.Enabled = true;
}
there are other if statements below but are irrelevant and the point of this is to make a button usable once a list is greater than 0. I've created another test button to display the value and it shows that the sublminalMessages list is greater than 0
private void testbutton3_Click(object sender, EventArgs e)
{
MessageBox.Show(subliminalMessages.Count.ToString());
}
Which outputs at 1 which it should be from some other code that adds a value in at the beginning. But for some reason even with the subliminalmessages.count being greater than 0 the if statement is no longer being called ever since I'm making the form appear being called from another form from the button code above.
The subliminalMessages list is being populated and created on the same form
public List<string> subliminalMessages = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(Settings.Default["Subliminal1"].ToString()))
{
subliminalMessages.Add(Settings.Default["Subliminal1"].ToString());
MessageBox.Show("If worked");
}
}
There is a value in the Setting.Default that is being added
The button and timer are on the same form and the timer in question is enabled.
Does anyone know why?
Thanks
I'll have a stab at giving you an answer. But it's a little swerve from what you're doing now.
From what I understand of your code you are using the timer to enable/disable the StartSubB button. Or maybe just enable it.
Instead of relying on the timer which appears to not work why not use a BindingList<string>. This has an event called ListChanged which you can handle and then enable/disable your button.
Here's a test form I created:
public partial class Form1 : Form
{
BindingList<string> items;
public Form1()
{
InitializeComponent();
button3.Enabled = false;
items = new BindingList<string>();
items.ListChanged += Items_ListChanged;
}
private void Items_ListChanged(object sender, ListChangedEventArgs e)
{
button3.Enabled = items.Count > 0;
}
private void btnAdd_Click(object sender, EventArgs e)
{
items.Add("a");
}
private void btnRemove_Click(object sender, EventArgs e)
{
if (items.Count > 0)
items.RemoveAt(items.Count - 1);
}
}
I have a BindingList<string> called items. This is analagous with your subliminalmessages list but it's a BindingList. This type of list has a ListChanged event that fires when items are added or removed from the list. In the constructor we new up the items list and subscribe to the ListChanged event.
When the Items_ListChanged event fires button3 is enabled or disabled based on whether items.Count > 0 or not.
In btnAdd_Click we just add an item to the list.
In btnRemove_Click we check that there are some items then remove the last one.
If you were to run this you'd see that when we click the Add button, button3 gets enabled. If we click the Remove button we'll see button3 get disabled.
The only limitation of BindingList is that you can't add a range by passing in another List.
If you implement this and your button still doesn't activate then you'll probably need to post some more code. Strip out all the irrelevant stuff and put it in a new project that demonstrates the failure of the condition and either copy the code here or provide a link to download the project. The only reason the if statement should fail is if the list is actually empty.

Winforms, Combobox, Databinding... allow user to type in a value not found in the DataSource

Just like the title says... I have a Winforms application with a databound dropdown. I want the user to have the convenience to pick from a bunch of predefined values, but also the ability to type in his own value
If I just enable databinding and set dropdown type to anything but DropDownList, it allows me to enter anything I want, but does not persist it to the objects...
Seems like a simple problem to solve... help?
I've added an event handler on ComboBox.Leave this code would add the newly typed in string in the combobox to the underlying list(countries) as well as refresh the combobox binding to it.
Limitations
You'd have to handle the addition of new element based on the type of datasource you have.
The List.Contains is case sensitive you might want to keep all the strings in one case. And convert the user entered value to that case before deciding to add it to the datasource.
Here you go, modify the comboBox1_Leave eventhandler according to your datatypes and datasource.
public partial class Form1 : Form
{
private List<string> countries;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
countries = new List<string>();
countries.Add("Australia");
countries.Add("Belgium");
countries.Add("Canada");
comboBox1.DataSource = countries;
}
private void comboBox1_Leave(object sender, EventArgs e)
{
ComboBox combo = (sender as ComboBox);
CurrencyManager cm = (combo.BindingContext[combo.DataSource] as CurrencyManager);
if (!cm.List.Contains(combo.Text))
{
cm.List.Add(combo.Text);
cm.EndCurrentEdit();
cm.Refresh();
cm.Position = cm.Count - 1;
}
}
}

Silverlight ObservableCollection dependency properties are not updated in design mode

I use a dependency property of type ObservableCollection<> in my control. I want to automatically change content display in my control while editing XAML code. I write such a code:
public class RowInfoCollection : ObservableCollection<RowInfo>
{
}
//... here my control class
public static readonly DependencyProperty RowDataProperty =
DependencyProperty.Register("RowData", typeof(RowInfoCollection), typeof(VisiGrid),
new PropertyMetadata(new RowInfoCollection(), RowDataChangedCallback));
public RowInfoCollection RowData
{
get
{
return (RowInfoCollection)base.GetValue(RowDataProperty);
}
set
{
base.SetValue(RowDataProperty, value);
}
}
private static void RowDataChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
VisiGrid vg = d as VisiGrid;
vg.rowData = (RowInfoCollection)e.NewValue;
vg.rowData.CollectionChanged += vg.rowData_CollectionChanged;
// Some application specific logic skipped
}
void rowData_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
rowData = RowData;
switch (e.Action)
{
// Skipped
}
}
When in run time, everything works ok. But when I change the property value in VS2010 designer there are some problems. When I completely remove value or insert it instead of empty VS' designer successfully refreshes display of my control. But when I add or remove collection items one at a time there's no change. When I build project the display updates (in fact, the designer reloads).
When I looked under debugger I saw that when in design mode it successfully calls my callback function when the collection is totally removed or inserted but does not fire CollectionChanges event when it is changed partially.
How should I force VS designer to track collection changes and fire events?
Thank you.

How do i make a picturebox selectable?

I am making a very basic map editor. I'm halfway through it and one problem i hit is how to delete an object.
I would like to press delete but there appears to be no keydown event for pictureboxes and it will seem like i will have it only on my listbox.
What is the best solution for deleting an object in my editor?
You'll want the PictureBox to participate in the tabbing order and show that it has the focus. That takes a bit of minor surgery. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. Implement the KeyDown event.
using System;
using System.Drawing;
using System.Windows.Forms;
class SelectablePictureBox : PictureBox {
public SelectablePictureBox() {
this.SetStyle(ControlStyles.Selectable, true);
this.TabStop = true;
}
protected override void OnMouseDown(MouseEventArgs e) {
this.Focus();
base.OnMouseDown(e);
}
protected override void OnEnter(EventArgs e) {
this.Invalidate();
base.OnEnter(e);
}
protected override void OnLeave(EventArgs e) {
this.Invalidate();
base.OnLeave(e);
}
protected override void OnPaint(PaintEventArgs pe) {
base.OnPaint(pe);
if (this.Focused) {
var rc = this.ClientRectangle;
rc.Inflate(-2, -2);
ControlPaint.DrawFocusRectangle(pe.Graphics, rc);
}
}
}
i think this is the best methode:
http://felix.pastebin.com/Q0YbMt22
... 8 years after ...
An alternative to Hans Passant's code, which doesn't require you to create a new class just so your PictureBox is in the tab order, is to set TabStop to true, and call SetStyle() directly on the PictureBox, optimally after InitializeComponent() is called.
TabStop is public, so it's easily set, but SetStyle() is protected, so reflection comes to the rescue!
myPictureBox.TabStop = true;
typeof(PictureBox)
.GetMethod("SetStyle", BindingFlags.Instance | BindingFlags.NonPublic)
.Invoke(myPictureBox, new object[] { ControlStyles.Selectable, true });
This of course, doesn't do anything like getting the focus when the PictureBox is clicked, so you have to do that in its various events as you see fit.

Using IEditableObject In Silverlight

I have a object that implements the IEditableObject interface exposed on a viewmodel bound to a Silverlight page.
How/Where do I call the BeginEdit, CancelEdit and EndEdit methods? How can I constrain only objects implementing this interface to my page?
I am NOT using DataGrid or DataForm controls. I am using Label, TextBox and DescriptionViewer controls to display the data for editing.
I know this is an old thread (but for the sake of future use...)
I do it this way:
whenever the current item (for instance of a CollectionViewSource) changes this is done:
void View_CurrentChanged(object sender, EventArgs e)
{
if (culturesView.Source != null)
{
((IEditableObject)SelectedRecord).BeginEdit();
RaisePropertyChanged("SelectedRecord");
}
}
Whenever i want to save (the current item) i do this:
private void Save()
{
((IEditableObject)SelectedRecord).EndEdit();
//do the actual saving to the dbms here ....
}
Whenever i want to cancel (current changes) i do this:
private void Cancel()
{
((IEditableObject)SelectedRecord).CancelEdit();
//allthough we have canceled the editing we have to re-enable the edit mode (because
//the user may want to edit the selected record again)
((IEditableObject)SelectedRecord).BeginEdit();
}
Hope it helps someone in the future!

Resources