I am working on a Windows Forms Application project (it is an assignment). And this is how the form should look like.
I need to write a method that when clicking on the 'Add' button textboxes are erased, so the new value which user adds could be accepted.
A very simple example of what you are asking for is:
private void ClearText()
{
Control[] aryControls = { txtBirthday, txtFirstName, txtLastName, txtID };
foreach (Control ctrl in aryControls)
{
ctrl.Text = "";
}
}
Related
i am new to wpf and i need to open up a pop up a new window on grid row click which contains lots of data and controls on it.i am confused with the correct approach. i am using mvvm pattern.should i make a window control or user control or something else. and how to open that pop up inside a function. please help with example
If I need to display a new Window in my MVVM-Application I use the following approach:
At first I have an interface with a method to show the new dialog:
internal interface IDialogManager
{
void DisplayData(object data);
}
And an implementation like:
internal class DialogManager : IDialogManager
{
public void DisplayData(object data)
{
LotOfDataViewModel lotOfDataViewModel = new LotOfDataViewModel(data);
LotOfDataView lotOfDataView = new LotOfDataView
{
DataContext = lotOfDataViewModel
};
lotOfDataView.ShowDialog();
}
}
LotOfDataViewModel and LotOfDataView are the new Dialog where you want to show your data.
In your actual ViewModel you introduce a new property like:
private IDialogManager dialogManager;
private IDialogManager DialogManager
{
get { return dialogManager ?? (dialogManager = new DialogManager()); }
}
And the you can show your large data with:
DialogManager.DisplayData(myData);
It looks like the AutoCompleteTextField is firing a suggestion selection event on load. I test this with a ListModel. You can recreate this issue with the code below from a barebone hello world project.
public void start() {
if (current != null) {
current.show();
return;
}
Form hi = new Form("Hi World");
ListModel<String> suggestionsModel = new DefaultListModel<String>();
suggestionsModel.addItem("Apple");
suggestionsModel.addItem("Banana");
suggestionsModel.addItem("Chocolate");
suggestionsModel.addItem("Elk");
suggestionsModel.addItem("Fish");
AutoCompleteTextField search = new AutoCompleteTextField(suggestionsModel);
suggestionsModel.addSelectionListener(new SelectionListener() {
#Override
public void selectionChanged(int oldSelected, int newSelected) {
System.out.println("SUGGESTION SELECTED"+suggestionsModel.getSelectedIndex());
}
});
hi.add(search);
hi.show();
}
If you run the code, you can see that the "SUGGESTION SELECTED0" gets printed twice confirming that the selection is firing on load and selecting the first suggestion by default.
This is causing me issues. I am searching for an object and then if found, I am displaying its attributes to the user. Per this issue, the attributes for the first object is getting displayed by default on load.
This is the behavior of the data change listener which is "over eager", we won't change it as there are some edge cases that depend on this behavior (e.g. paste, instant edit type etc.).
Working with this is pretty easy:
if(currentValue != lasValue) {
....
}
I think, that something has broken in cn1 painting model. Could someone review if this is a bug or am I doing something incorrectly?
I would like to archive the following:
On a form is a label, which text is refreshed with UITimer with 1 second interval. For example:
To indicate some activity on form is used form.setGlassPane(..) to paint a shadow above the form. The problem is, that on label text update label is repainted, but glasspane is not repainted, that is the shadow is not painted on the label:
Test code:
final Form form = new Form("Welcome", new BoxLayout(BoxLayout.Y_AXIS));
final Label label = new Label("..");
Button button = new Button("Show Shade");
form.addComponent(label);
form.addComponent(button);
button.addActionListener((e) -> {
form.setGlassPane(new Painter() {
public void paint(Graphics g, Rectangle rect) {
int wasAlpha = g.getAlpha();
g.setAlpha(50);
g.setColor(0x101010);
g.fillRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
g.setAlpha(wasAlpha);
}
});
});
new UITimer(() -> {
label.setText(new Date().toString());
}).schedule(1000, true, form);
form.show();
This seems to be due to a fix for issue 1680 a while back. I reopened the issue, we'll need to figure out why this failed.
I have a prism/wpf/mef solution that contains an AvalonDock. I created a RegionAdapterBase<Pane> class that handles creating and removing the Panes from AvalonDock.
Heres the problem I'm running into:
I click a button in my menu and a view is registered with a region and displayed in my DocumentPane
I click the close button in AvalonDock to close the tab and remove the view
I click the same menu button to add it back again
I receive the error:
"Specified element is already the
logical child of another element.
Disconnect it first."
So... this tells me that something is lingering that I need to remove, but I cannot figure out where it is. Heres some code from my RegionAdapter:
private void OnViewsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e, IRegion region, Pane regionTarget)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (object item in e.NewItems)
{
UIElement view = item as UIElement;
if (view is ITabViewInfo)
{
if (view != null)
{
DockableContent newContentPane = new DockableContent()
{
Content = item,
Title = ((ITabViewInfo)view).TabViewTitle,
Icon = new Image()
{
Source = new BitmapImage(((ITabViewInfo)view).TabViewIcon)
}.Source,
IsCloseable = ((ITabViewInfo)view).IsCloseable,
HideOnClose = ((ITabViewInfo)view).IsHideOnClose
};
newContentPane.Closed += (contentPaneSender, args) =>
{
Debug.WriteLine("Removing view from region", "Prism");
region.Remove(item);
};
regionTarget.Items.Add(newContentPane);
newContentPane.Activate();
}
}
}
} else if (e.Action == NotifyCollectionChangedAction.Remove) {
regionTarget.Items.Clear();
}
}
From my debug lines, the DocumentPane and region views are properly being destroyed... when I click to add the item back to the view, I get the above error message on the line that does:
Content = item,
Heres the code from my module that runs when the menu button is pressed:
if (_regionManager.Regions["MainRegion"].Views.Any(m => m.GetType() == typeof(Views.ClassicFrontierView)))
{
Debug.WriteLine(_regionManager.Regions["MainRegion"].Views.Count());
}
else
{
Debug.WriteLine("Adding view to region", "Prism");
_regionManager.RegisterViewWithRegion("MainRegion", typeof(Views.ClassicFrontierView));
}
Any idea what I'm missing?
Do you create a new View each time or you trying to show existing View several times? If second is correct I would try this:
else if (e.Action == NotifyCollectionChangedAction.Remove) {
foreach (DockableContent content in regionTarget.Items)
content.Content = null;
regionTarget.Items.Clear();
}
Instead of handling the Closed event (which may have lost a reference to the underlying view), I handle the Closing event.
This worked, however, when I tried to re-open the tab, it was displaying the same instance. After reading this In Composite WPF (Prism), what is the difference between IRegion.Add and IRegionManager.RegisterViewWithRegion? I changed this:
_regionManager.RegisterViewWithRegion("MainRegion", typeof(Views.ClassicFrontierView));
to this:
_regionManager.Regions["MainRegion"].Add(new Classic.Views.ClassicFrontierView());
I still have to do some research with Prism / avalondock to make sure there will be no memory leaks, but as of now it appears to be working.
You likely need to remove it from regionTarget as well.
You can use Snoop to see what hasn't been removed from the Visual Tree and then attempt to find which container you need to remove your element from. Other possibilities are things like an unfrozen Icon image, etc.
Can you think of a scenario where IEditableObject would be still usefull in an MVVM-based WPF application? If so, do you have an example that demonstrates this.
I have used IEditableObject in one of my applications. For example if you have a dialog for editing something, you could implement IEditableObject on your ViewModel. You call BeginEdit() when the dialog opens, EndEdit() when the user clicks OK, and CancelEdit() when the user clicks cancel.
IEditableObject is a good interface anytime you want to be able to roll back changes.
I've got an implementation of IEditableObject in my application so that I can keep from updating my data model if what the user has entered is invalid, and roll back to the original values if the user abandons changes.
Within a type being displayed in a DataGrid. This was needed since when I am making use of tabs and a DataGrid is being stored within that tab switching the tabs needed to force a commit so to speak within the DataGrid if a cell was active; rolling back the changes since they were not committed. T
There is a behavior being applied to the DataGrid to achieve this functionality but the IEditableObject portion is below.
private IDatabaseConnection _copy;
void IEditableObject.BeginEdit()
{
if (this._copy == null)
this._copy = _container.Resolve<IDatabaseConnection>();
_copy.Database = this.Database;
_copy.DisplayName = this.DisplayName;
_copy.HostName = this.HostName;
_copy.Username = this.Username;
_copy.Password = this.Password;
}
void IEditableObject.CancelEdit()
{
this.Database = _copy.Database;
this.DisplayName = _copy.DisplayName;
this.HostName = _copy.HostName;
this.Username = _copy.Username;
this.Password = _copy.Password;
}
void IEditableObject.EndEdit()
{
_copy.Database = String.Empty;
_copy.DisplayName = String.Empty;
_copy.HostName = String.Empty;
_copy.Username = String.Empty;
_copy.Password = String.Empty;
}