Form inheritance should be abstract - forms

Here my code
GeneralLesson.cs: it's just a .cs file, no .Designer.cs or .resx file
public /*abstract*/ class GeneralLesson : Form
{
public GeneralLesson()
{
this.StartPosition = SS.FrmStartPos;//If I want to change it later, I just change it here and all the children will get the change
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
if (e.CloseReason == CloseReason.WindowsShutDown) return;
SS.LearningDay = 0; //SS is my static class that hold the static variables.
SS.FrmMain.Show();
}
}
SentLesson.cs: This is actually a windows form, with .Designer.cs and .resx file
public partial class _Sent_Lesson : GeneralLesson
{
public _Sent_Lesson()
{
InitializeComponent();
richTextBox1.Text = "under construction";
}
}
So it actually serves my purpose well. my SentLesson window inherits the constructor and OnFormClosing from GeneralLesson. But the problem is I can't design my SentLesson window any more, it shows as the picture below:
Maybe I do it wrong. But I don't want to create GeneralLesson as a Window because I don't design any control on it, I just want to override some function like OnFormClosing or the Constructor. Is there any way I can do this without making GeneralLesson as a Window.
Thanks for reading.

I finally got the solution. Even though the abstract parent form still works logically, but the children form can't be designed any further with Visual Studio designer. So I have to inherit forms the Visual Studio way.
As you can see, Visual Studio provides us Inherited Form to create Children form. So I create GeneralLesson.cs as Windows Form, and SentLesson.cs as Inherited Form. So SentLesson inherits constructor and OnFormClosing from GeneralLesson and the designer of SentLesson no longer shows error.
If you want the Children form can access some controls from the Parent form, say Parent's "Button A". Just set the Parent's "Button A" 's Modifiers in properties window from Private to Protected. Then you can do anything with Button A in the Children form.
Thank for your reading.

Related

Nested Variables in Unity Inspector

I was wondering if anyone knew how to make nested variables inside the unity inspector with a script, kind of like this:
Doing so does require knowledge of UnityEditor and not only (as you say ... nested variables) can give you many other control options in the inspector. To do this, I created a sample code called MenuManager. As you can see this code:
public class MenuManager : MonoBehaviour
{
public bool variable1;
public float nestedVariable;
//...
}
Unity itself does not provide any attributes such as [Range] or [Header] for such a request, and to do this you need to define a CustomEditor for the class but Before to do that, you need to create a folder similar to the photo with name of Editor and put it in the Assets folder. Then create another script with name of MenuEditor (for example here ..) and put it in a Editor folder.
Now open the MenuEditor code. Inherit it from the Editor class. Editor class is base class for editing inspector and more. It will give you many override methods with access to the features inside the unity editor. and make sure it has two Attributes Custom Editor as well as CanEditMultipleObjects.
[CustomEditor(typeof(MenuManager))]
[CanEditMultipleObjects]
public class MenuEditor : Editor
{
//..
}
This code gives you access to the MenuManager script. According to the following code, I coded a Nested variable to the first one.
[CustomEditor(typeof(MenuManager))]
[CanEditMultipleObjects]
public class MenuEditor : Editor
{
public override void OnInspectorGUI()
{
var myMenu = target as MenuManager; // target is script reference that we want to manipulate it
myMenu.variable1 = EditorGUILayout.Toggle("Variable 1", myMenu.variable1); // show first variable on inspector
GUI.enabled = myMenu.variable1; // access to second variable depend of first
myMenu.nestedVariable =EditorGUILayout.Toggle("Nested Variable", myMenu.nestedVariable);
GUI.enabled = true;
}
}
After finishing the work, you can access the nested variable only by setting first one to true.
Remember after doing this you can access many other features just inside MenuEditor class but if you find this difficult, I suggest you use Odin Inspecter. I hope you have reached your answer. comment under answer if you need more information.

Proper way to do dialogs using Prism and UWP

I'm not sure if I'm thinking about this in the correct way. I have a list of objects, and I would like the user to be able to edit and view the properties of a specified object. My initial thought is great, I'll pop up a dialog that has textboxes and let the user edit to their heart's content until they press either Ok or Cancel.
I'm on UWP, and using Prism for all of my MVVM needs. It's taken me a while, but I understand creating Views and their associated ViewModels, commands, etc. So far, I think I've done a good job keeping the view logic and business logic separated.
I've searched, but haven't found how to show a dialog in a way that follows MVVM principles. The two things that seem to be coming up the most are to use Interaction requests (which don't appear to exist using Prism on UWP), and creating a custom Content Dialog and showing it by calling ShowAsync in a method in the parent view's associated ViewModel (which seems to be counter to MVVM principles).
So, how do I either show a dialog that is defined using XAML and has an associated ViewModel (preferable since it is similar to what I'm familiar with), or another way I can tackle this problem?
Using MVVM the proper place for opening a dialog is in the ViewModel.
Usually I do something like this in your scenario:
Create an interface for showing dialogs:
public interface IWindowService
{
void OpenViewModelInWindow(ViewModelBase vm, string title, bool resizeable = true);
void CloseViewModelInWindow(ViewModelBase vm);
}
Implement this interface in UI layer:
public class WindowService : IWindowService
{
private List<Window> _windows = new List<Window>();
public void OpenViewModelInWindow(ViewModelBase vm, string title, bool resizeable = true)
{
var window = new Window
{
Title = title,
Content = vm,
Owner = Application.Current.MainWindow,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
ShowInTaskbar = false,
SizeToContent = SizeToContent.WidthAndHeight,
ResizeMode = resizeable ? ResizeMode.CanResize : ResizeMode.NoResize
};
_windows.Add(window);
window.ShowDialog();
}
public void CloseViewModelInWindow(ViewModelBase vm)
{
_windows.Single(w => w.Content == vm).Close();
}
}
In your App.xaml you need to define DataTemplates so that when you set the Content property of the window the corresponding View created in the window.
<DataTemplate DataType="{x:Type viewModel:AViewModel}">
<views:AUserControl />
</DataTemplate>
Then you can use the IWindowService from the ViewModel, you should inject it by constructor injection.
This way you are not referencing framework specific classes directly from the ViewModel. ViewModel has a reference only to an IWindowService. This has a benefit also when you want ot write unit test for a viewmodel. You can mock this service so that when the unit test is running it should not open a dialog.

Attaching Properties view to a custom XML Editor

I have created a custom editor in eclipse plugin which shows a XML in Tree-Table(TreeViewer) format with couple of its attributes. For showing remaining attributes I am trying to tie it up with "Properties View", but not really able to make progress on it.
I went through similar question on SO like
How to handle property sheet from customized editor in eclipse plugin development? where it talk about make your viewer contribute to workbench selection and implementing an IPropertySource on object which is selected in editor.
In my case I am directly setting an document object in treeviewer input like below.
IFileEditorInput editorInput = (IFileEditorInput) getEditorInput();
IFile inputIFile = editorInput.getFile();
File f = new File(inputIFile.getLocation().toString());
try {
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(f);
}
catch (SAXException | IOException | ParserConfigurationException e) {
e.printStackTrace();
}
//setting root element of doc as input
treeViewer.setInput(doc.getDocumentElement());
Now on what object should I implement an IPropertySource interface to contribute properties?
Let me know if I am going in right direction or missing something or doing it completely wrong.
Hope this make sense !!
When your selection provider fires a selection changed event the properties page will look at the new selection. If you are using a tree viewer as the provider the selection will be the current object from your tree content provider.
The properties view will try to get an IPropertySourceProvider from the selection. Your object can implement IPropertySourceProvider directly, or provide it view the IAdaptable interface or by using IAdapterFactory.
Once the view has the IPropertySourceProvier it will call the getPropertySource method. Your code must return an IPropertySource object - it is up to you to write this class.

Use eclipse GMF to create read only diagram

I followed the file system example http://gmfsamples.tuxfamily.org/wiki/doku.php?id=gmf_tutorial1
what I wanted to do is not using the generated editor with its palette.
I created a new plugin with one view and I wanted to create a diagram programatically inside this view to show for instance 2 objects connected with link
I came across this answer GMF display diagram example
but it didn't help me a lot.
in createPartControl of my view I did
#Override
public void createPartControl(Composite parent) {
DiagramGraphicalViewer viewer = new DiagramGraphicalViewer();
viewer.createControl(parent);
RootEditPart root = EditPartService.getInstance().createRootEditPart(diagram);
viewer.setRootEditPart(root);
viewer.setEditPartFactory(new EcoreEditPartProvider());
viewer.getControl().setBackground(ColorConstants.listBackground);
viewer.setContents(diagram);
}
as in the answer but I didn't know how to get that "diagram" variable
The easiest would be use the same GraphicalViewer for your view and the same diagram as well. Just get your DiagramEditPart from the viewer and call disableEditMode() on it. (Do the appropriate type casting if necessary).

Sort ItemsControl Prism v2.2 Region without a bound collection?

Is there a way to sort the views that get added to a ItemsControl region? The views being added are registered with the container and added to the region in each unique module.
Some pseudo code...
Shell:
<Window>
<ItemsControl Prism:RegionManager.Region="ItemsRegion"/>
</Window>
Modules: This is the initialization code in the modules.
protected override void RegisterViewsAndServices()
{
CommonContainerLifetimeManager.Register<IView, ItemView1>();
Container.RegisterType<IViewModel, ItemViewModel1>("ItemViewModel1");
}
public override void AdditionalInitialization()
{
var itemView1 = Container.Resolve<ItemView1>();
RegionManager.Regions["ItemsRegion"].Add(itemView1);
}
With this approach it is showing the added views in the shell's itemscontrol in the order the modules are loaded. Based on the role of the logged on user different modules are loaded. Is there a way, without having to add a collection inbetween, to sort the itemscontrol.items on a property of the view's viewmodel for example? Is there a way to force the modules to be loaded in a certain order? I am currently using a module catalog.
Thanks
Andy
So I found the answer to this question... At least I found the answer in Prism v4.
You add a ViewSortHint class attribute to the View's code behind. Prism will find this attribute and sort the views based upon the string you enter in the ViewSortHint parameter.
[ViewSortHint("01")]
public partial class SortedButton : UserControl
{
public SortedButton()
{
InitializeComponent();
}
}
Hope this helps someone...
Andy