Changing ListBox selection is not moving changes from BindingSource to DataSet - visual-studio-2005

The answer to this question may turn out to be, "Don't use typed DataSets without using the Binding Navigator."
I am curious, however, about the behavior I'm seeing.
So, I created a form where every control was dragged from the data sources explorer.
I deleted the Binding Navigator because it is ugly and inappropriate for this particular form.
I added a ListBox and set the DataSource to the BindingSource.
Notice that the ListBox is not bound, it is just filling itself from the BindingSource.
By some magic that I wasn't counting on, moving around in the ListBox is navigating the BindingSource and all other controls are updating accordingly.
I can make changes to the bound controls and explicitly call EndEdit on the BindingSource and then update the DataSource through the Table Adapter. Works great.
When I make changes in the bound controls and click a new option in the ListBox, I want to be able to check for changes and prompt to save or reset if there are any.
Here is the strange part that I haven't been able to figure out.
No matter what event I attach to, DataSet.HasChanges doesn't return true until the second ListBox change.
I've searched and tried dozens of suggestions, most of them ridiculous, but a few that seemed promising.
No luck.
Edit: It isn't the second click that is significant, it is when you click back on the original (edited) item.

Since asking the question, I've learned a bit more about BindingSources, DataSets and TableAdapters.
Here is what works:
private void MyListBox_Click(object sender, EventArgs e)
{
this.myBindingSource.EndEdit();
if (myDataSet.HasChanges())
{
if (MessageBox.Show("Save changes?", "Before moving on", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
myTableAdapter.Update(myDataSet.myDataTable);
}
else
{
myDataSet.RejectChanges();
}
}
}

Related

Codename one - Long Press Event to Ignore normal Press

I have been following this guide and have the working example till the very end of the demo (very insightful tutorial, 10/10 would recommend):
https://codenameone.teachable.com/courses/java-for-mobile-devices-introducing-codename-one/lectures/2647773
Now I wanted to expand this to a more practical todo list by adding a Floating Action Button to add new items, like that:
fab = FloatingActionButton.createFAB(FontImage.MATERIAL_ADD);
fab.bindFabToContainer(current.getContentPane(), Component.RIGHT,
Component.BOTTOM);
fab.addActionListener(e -> { // show dialog for adding new item });
This by itself works fine. Now comes the tricky part. When using a long press event on any of the items, two things should happen:
the visual of the long pressed item should change to indicate it is being selected (not in terms of a Checkbox or ToggleButton "select" but a highlight.
the FAB should change its icon and actionlistener to delete the highlighted item.
The long press event is achieved in overriding the longPointPress method from the Checkbox class:
#Override
public void longPointerPress(int x, int y){
mainForm.longPressEvent(this);
// no event parameter for e.consume();
}
To my questions:
Q1: When I use the action listener from the ToggleButton, both the "normal" click event as well as the long Press event are fired. I need to distinguish between the two though.
The longPointerPress method does not have the event in the param list, hence I cannot consume the event after being done with my long Press Event activity. How can I prevent the normal action listener from firing?
Q2: For the "highlight effect", I would like the item to have a margin to all sides, overall shrinking the element by that amount. In other words, without increasing the previous total size. By just adding a margin though, the item gets bigger.
How could a shrink a given element with a margin to all sides, but preserve the original size?
Q3: A FAB only has the option to "setIcon", not "setMaterialIcon". Hence, I am currently recreating the FAB every time it changes, as I dont want to hustle with the involved styles. Is there a better way than this?
//this is the unwanted function, as I dont want to set the style myself
fab.setIcon(FontImage.createMaterial(icon, s));
//Delete FAB pressed, change to Add FAB
fab.remove();
fab = FloatingActionButton.createFAB(FontImage.MATERIAL_ADD);
fab.bindFabToContainer(current.getContentPane(), Component.RIGHT, Component.BOTTOM);
Action event is always invoked on pointer release regardless of whether there was a long press event fired as we don't "know" you processed the longPress event. You would need to create a flag such as:
private boolean handledInLongPress;
public void longPress(int x, int y) {
// do your stuff
handledInLongPress = true;
}
private void handleActionEvent(ActionEvent ev) {
// I'm using this as a placeholder for your event code
// block the event from propagating and undo anything it might
// have triggered
ev.consume();
}
I would recommend using setUIID() on the elements and define a "delete*" set of UIID's. You can define smaller padding and fonts to create the effect of shrinking but that might be tricky if you have icons here as well. You can scale down said icons and keep the original for restoring in a client property.
FAB makes a lot of assumptions so re-creating it (or using two instances) is probably a better approach than trying to set the icon. There is no way to change the icon of the FAB at runtime in the current implementation.

Xamarin.Forms Entry Focus when Unfocused

I need it so that when the Entry loses focus, the focus returns to the Entry (therefore locking the focus on an Entry).
I did the following code:
myEntry.Unfocused += (object sender, FocusEventArgs e) => {
if (!e.IsFocused)
{
((Entry)sender).Focus();
}
};
It works, but the keyboard stops working - I can't write anything.
Is this a bug? Can someone help me?
I found that this is a bug xamarin.forms 2.0.0.0. Updated and solved the problem.
I know on android if you click on an Entry and give it focus, the keyboard will appear, and then if you click another element, the Entry will lose focus and the keyboard will go away. So by refocussing on the Entry when it loses focus, you might be forcing the keyboard to be minimized. This is a total guess though.
What you might be able to do is set the most parent element's InputTransparent property to true and then only set the Entry.InputTransparent property to false so that the user is unable to click anything but the Entry.
If that does not work, then you could also try adding a transparent ContentView that covers everything except the Entry and set the ContentView.InputTransparent to true which would have the same effect.

wxWidgets event focus textcontrol

I have another wxWidgets question regarding events and focus.
I have already looked at the tutorials and this old question here but I am still running into problems C++ Event (Focus) Handling
Basically I have a dialog with two wxTextCtrl elements and a Button.
What I would like to achieve is, that when I click on button it needs to tell me which of the two elements previously had the focus.
In the constructor of my Dialog I created all the elements and then connected them to the eventhandler like this: Ttop->Connect(TOP,wxEVT_KILL_FOCUS,(wxObjectEventFunction)&UI_ADDENTRY::hasfocus);
Tbottom->Connect(BOTTOM,wxEVT_KILL_FOCUS,(wxObjectEventFunction)&UI_ADDENTRY::hasfocus);
then there is the eventhandler that safes the id into focus
void UI_ADDENTRY::hasfocus(wxFocusEvent& event){
focus= event.GetId();
event.Skip();}
however when i try to access focus in the Button function it always tells me: 0 instead of TOP or BOTTOM / the ids that I gave the textcontrols
void UI_ADDENTRY::OnRecord(wxCommandEvent &event){
wxString tmp;
tmp << this->focus;
wxMessageBox(tmp);}
What am I doing wrong? is there another way of finding out which of the two textbox has been in focus last?
Thank you
The most fool proof way is to catch EVT_SET_FOCUS in your text controls and remember the last one that received it. This is not more difficult than what you are doing but should work without problems.
FWIW EVT_KILL_FOCUS can't, unfortunately, be consistently implemented on all platforms, in particular GTK+ doesn't give any information about the window focus is being lost to.
In think u mean event.GetWindow().GetId(). Though I'm not sure how ur casting from int to string.

WP7 controls: When to set VisualState after recovering from Tombstone?

My question is simple: WHEN (on what event?) can I be sure that a control has fully loaded and has its states and templates also?
Why am I asking:
I'm trying to restore the state of my own WP7 control after recovering from tombstone. This control looks like a calendar in a weekly view. In this calendar you can select many items displayed as colored Rectangles.
If I select any of them, and then go to tombstone and come back to the page, it seems like my control forgot which Rectangles were selected. In fact, it did NOT forget the data itself, but the Rectangles forgot their selected state.
After recovering from tombstone, I try to select the Rectangles by setting their VisualState to "Selected" (which works in any other scenario). I found out, that it fails, because VisualStateManager can't find the "Selected" state.
I know this is tricky, because when coming back from tombstone the controls do not build exactly as in any "normal" case. (for example Bindings and Templates do not apply in the same order) But up until now I could always trust, that when FrameworkElement.Loaded fired, I had my controls ready. Now it seems like VisualState is not. (I tried to set the state from Loaded event handler, but results are the same, VisualStateManager.GoToState returns with false.)
What more can I do?
This is a tricky one! I have also experienced issues where UI events fire before the UI itself is fully constructed, see this blog post for an example. My general approach to this is to handle the LayoutUpdated event, which fires each time the visual tree is updated. You will find that this event fires multiple times, both before and after the Loaded event.
When the Layoutupdated event fires, you can check whether the visual state change has worked, if so, no longer handle the event. If not, keep trying!
Within your loaded event, try the following:
// try to set the state
if (VisualStateManager.GoToState(myControl, "myState") == false)
{
// if failed, wait for the next LayoutUpdated event
EventHandler updateHandler = null;
updateHandler = (s, e2) =>
{
if (VisualStateManager.GoToState(myControl, "myState") == false)
{
myControl.LayoutUpdated -= updateHandler;
}
};
myControl.LayoutUpdated += updateHandler;
}

WP7 dynamic pivot

I'm having trouble implementing a dynamic pivot control.
What I would like to do, is create an image gallery using the pivot, where you could change the image by swiping.
This works natively with the pivot control by binding it's itemsource on my observable collection to display one image on each pivotitem.
The thing is that this solution takes a lot of memory when my gallery contains more than 10 pictures, as it creates an equal number of pivotitems.
What I tried is initializing my collection with 3 items, the currently displayed one, the previous and the next one, and when the user swipes, I updated my list.
I tried this using the "SelectionChanged" event and also with the gesture listener.. without success so far.
Has anyone ever tried to do this? And if yes, how can I implement it?
I do something similar, but don't use a Pivot control. Download the Silverlight toolkit (http://silverlight.codeplex.com/) and use a GestureListener to catch the swipe action:
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener DragCompleted="OnDrag"/>
</toolkit:GestureService.GestureListener>
Then in code, you have an event handler that catches the swipe event, and updates the image accordingly. Here is the event handler that I use to change my page:
private void OnDrag(object sender, DragCompletedGestureEventArgs e)
{
if (e.Direction == System.Windows.Controls.Orientation.Horizontal)
{
if (e.HorizontalChange < 0)
{
TurnPagesForward(1);
}
else
{
TurnPagesBackward(1);
}
}
}
If you update the items in your list your binding will be lost, so this will probably not work very well.
What might work is the following (but I have not tested it):
Start with an initial collection with five items.
Make sure you display the third item in the list so that the visible item is in the middle of the list.
When changing the visible page the SelectionChanged event is raised.
If you move to the right the selected index is changed to the fourth element. In this case you remove the first element in the list and add a new fifth element.
If you move to the left you do the opposite and remove the last element in the list.
This should ensure that you always can move back and forth while not removing or adding any visible items.
I am not sure if this will work, but it is worth a try. :-)
Don't do this. Lots of people have tried and it doesn't scale to large numbers of images.
Although it would be really nice if it was possible to use the pivot control to create a simple image navigation control it just isn't practical. You'll need to go the more complicated route of doing this yourself with a different method. (Lots of people have tried different ways. Go with what's best for your needs/experience/preferences.)

Resources