Calling a class from another form - forms

I am trying to call to class from form2 in C# for, example below is code from "Form2".
private void button17_Click(object sender, EventArgs e)
{
Form1 frontmain = new Form1();
frontmain.buttonchange();
this.Hide();
}
This will not excute the class in "Form1" called "buttonchange." Below is the code for the "buttonchange" class:
public void buttonchange()
{
button1.Text = workshop1;
button2.Text = workshop2;
button3.Text = workshop3;
button4.Text = workshop4;
button5.Text = workshop5;
{
I assume it has something to do with privacy settings between form1 and form2 however I have never found the solution. I have always worked around it somehow. Does anyone know what the issue is here?

I would set up events on form2 and set up form1 to capture the form1 events.
Here is a link to setting up events:
http://msdn.microsoft.com/en-us/library/awbftdfh.aspx

You're creating a new instance of Form1 and calling code upon that, which is probably not what you actually intend to do.
If you're trying to refer to an existing instance of Form1, then you'll need to refer to that (rather than create a new instance).
If an instance of Form1 spawns the form that your click handler is in, you can refer to it like this:
Form1 frontmain = this.Owner as Form1;
If an instance of Form1 doesn't spawn your second form with the click handler, then you will need to take an event-based approach (which is a better approach anyway, as it eliminates dependencies).

Related

Get SalesFormLetter class from SalesEditLines form formRun using PreHandler AX7

I need to make some changes on closeOk of SalesEditLines form. As I know, I am not able to change the standard methods, so I need to create an event handler for closeOk.
[PreHandlerFor(formStr(SalesEditLines), formMethodStr(SalesEditLines, closeOk))]
public static void SalesEditLines_Pre_closeOk(XppPrePostArgs args)
{
FormRun sender = args.getThis() as FormRun;
Object callerObject = sender.args().caller();
}
The question is - how can i access a SalesFormLetter through SalesEditLines form formRun using PreHandler?
You can see the following line in init method of SalesEditLines form
salesFormLetter = element.args().caller();
So your callerObject is an instance of SalesFormLetter class, you need just cast it to proper type.
Please check the following link:
https://learn.microsoft.com/en-us/dynamicsax-2012/developer/expression-operators-is-and-as-for-inheritance

Instantiating a Partial Class

So I have two classes in my project, one where it says
namespace WindowsApplication13
{
partial class Form1
and one where is says:
namespace WindowsApplication13
{
public partial class Form1 : Form
{
public Form1()
{
string text1, text2, text3, text4;
InitializeComponent();
text1 = textBox1.Text;
text2 = textBox2.Text;
text3 = textBox3.Text;
text4 = textBox4.Text;
}
}
}
}
So how do I then instansiate the latter in my Class1?
I have tried
Form1 : Form f1 = new Form1 : Form();
and
Form1 f1 = new Form1();
and
Form f1 = new Form();
But none of them works since when I try to get a variable from the Form1 : Form, I just get the error "WindowsApplication13.Form1" does not contain a definition for "text1".
Sorry if this post is messy or bad done. I can explain more in comments if needed. Thanks!
Edit: Sorry I know the syntax if Form f1 = new Form, I just forgot while writing this!
I assume this is meant to be C#? If so, you'd declare a variable and instantiate the class like this:
Form1 form = new Form1();
The Form1 : Form is part of the class declaration to show that it inherits from Form. You don't need it as part of the variable declaration.
(We can't tell what's wrong with regards to text1 - you haven't told us anything about how it's declared or how you're trying to use it.)
The correct syntax to declare a variable is
Form1 varName = new Form1();
Form1 is the name of your class; the fact that it's partial doesn't make any difference.
If you want to access a control in the form, you'll need to make it public first. (or, better yet, hide it behind a public property that exposes the functionality you need)

GWT Request Factory and Editor Framework Exception

When attempting to edit a new (proxy) entity using RequestFactoryEditorDriver.edit() I am getting the following error: "Exception caught: Attempting to edit an EntityProxy previously edited by another RequestContext". I am fairly sure that this is a result of my misunderstanding of the request factory/editor framework architecture. Here is the editor code that I think pertains to this problem:
public class OrgMaintenanceWidget extends Composite implements Editor<IOrgProxy> {
... other fields ...
private IOrgEditorDriver _orgEditorDriver;
interface IOrgEditorDriver extends RequestFactoryEditorDriver<IOrgProxy, OrgMaintenanceWidget> {}
public OrgMaintenanceWidget(final IClientFactory clientFactory) {
... widget initialization ...
_orgEditorDriver = GWT.create(IOrgEditorDriver.class);
_orgEditorDriver.initialize(_clientFactory.getRequestFactory().getEventBus(),
_clientFactory.getRequestFactory(), this);
}
#UiHandler("newButton")
public void onNewButtonClick(final ClickEvent clickEvent) {
_org = _clientFactory.getCache().getOrgCache().newOrg();
_orgEditorDriver.edit(_org, _clientFactory.getRequestFactory().orgRequestContext());
}
...
}
It's the "_orgEditorDriver.edit()" line that causes the exception. The "newOrg()" method is:
public IOrgProxy newOrg() {
return _clientFactory.getRequestFactory().orgRequestContext().create(IOrgProxy.class);
}
The RequestFactory is simply:
public interface IRequestFactory extends RequestFactory {
IOrgRequestContext orgRequestContext();
}
I am sure that I'm missing something fundamental about editing a new entity. When I edit an existing entity everything is fine ... the UI components are populated automatically, and flushing the editor back to the entity works very nicely. Here's the code that initiates editing for an existing entity:
#UiHandler("newButton")
public void onNewButtonClick(final ClickEvent clickEvent) {
_org = _clientFactory.getCache().getOrgCache().newOrg();
_orgEditorDriver.edit(_org, _clientFactory.getRequestFactory().orgRequestContext());
}
Any help would be greatly appreciated, and I'll try to publish any lessons learned.
This code:
_clientFactory.getRequestFactory().orgRequestContext().create(IOrgProxy.class);
Means:
Create new orgRequestContext()
Create new IOrgProxy using this context
Edit new IOrgProxy using this context, because as docs say: "Returns a new mutable proxy that this request can carry to the server, perhaps to be persisted.", it means that the proxy is edited by this request.
This code:
_orgEditorDriver.edit(_org, _clientFactory.getRequestFactory().orgRequestContext());
Means:
Again, create new orgRequestContext() (because each invocation of getRequestFactory().orgRequestContext() provides new instance of orgRequestContext()
"Start driving the Editor and its sub-editors with data." as docs say. But as a part of it, use passed orgRequestContext() to edit passed IOrgProxy instance, so that the proxy is editable.
Because the proxy was already edited while created by other RequestContext, you get the exception, because there is fundamental rule in RequestFactory, that proxy can be edited only by one RequestContext.
See also this thread.
I think you can't create an object with one RequestContext and then edit it with another one.
So you can solve this in two ways:
Persist the created object with the RequestContext you used when you created the object. The save method should return the persisted object and this persisted object can be passed to the editor with a fresh new RequestContext
Somewhere save the RequestContext you used for creating the object and pass it to the edit function of your Driver
Solution two could look something like this:
#UiHandler("newButton")
public void onNewButtonClick(final ClickEvent clickEvent) {
IOrgRequestContext ctx = _clientFactory.getRequestFactory().orgRequestContext();
_org = ctx.create(IOrgProxy.class);
_orgEditorDriver.edit(_org,ctx );
}

MVVM Toolkit light Messenger chained Messages

this might be complicated to explain but I give it a try...
I would like to use the Messenger to navigate to a new Page and also create a new Object (or pass one). How is this possible or am I on the wrong path?
Basically:
Click on "add new person" button which should bring up the PersonView and also should hold a new instance of a person object.
Click on "add person" button which should bring up the same PersonView page and should receives the object which is selected.
Message 1 = open Uri / Message 2 send exisiting or new object.
So far I have MainPageViewModel which sends
Messenger.Default.Send<Uri>(...)...
And MainPage.cs which Registers Messenger.Default.Register<Uri>(...)and executes
Frame.Navigate(...targetUri)....
I tryed to Send a message to the PersonViewModel right after Frame.Navigate... but this runs out of sync... so the page was not loaded to receive the PersonMessage,...
So any tips, tricks, licks, approaches would be greate...
Thanks...
Hope this helps, basicaly it is a simple Singleton class that gets the navigation frame the page that contains, after that you are able to use it in your viewmodel and navigate, and get notified when the page changes, so with this you control in a better way the navigation
and send messages, and get aware about your page status.
public class NavigationFrameController {
private static NavigationFrameController _instance;
private MainPage _root;
public Frame NavFrame { get; set;}
private static object keyLock = new Object();
NavigationFrameController() {
_root = (MainPage)Application.Current.RootVisual;
NavFrame = _root.ContentFrame;
NavFrame.Navigated += new NavigatedEventHandler(ContentFrame_Navigated);
NavFrame.NavigationFailed += new NavigationFailedEventHandler(ContentFrame_NavigationFailed);
}
public static NavigationFrameController Instance {
get {
if (_instance == null)
lock (keyLock) {
_instance = new NavigationFrameController();
}
return _instance;
}
}
public void NavigateTo(Uri uri) {
NavFrame.Source = uri;
}
private void ContentFrame_Navigated(object sender, NavigationEventArgs e) {
//send your message
// get attached to this event and get notified
}
private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e) {
}
I had the same problem - essentially where you receive the message to open a new window also create the viewmodel and add it to the view as datacontext. When you instantiate your viewmodel pass in the existing object or null etc... then in your viewmodel you can handle whether its a new or existing object.
If you are using dependency injection then call a resolve from the codebehind where you process the 'add person' messsage etc..
I think what you should do is keep the first message for navigation, and add to it the information about the object (person) you want to send. you can add a parameter to the query string, say "add=true" and then you can create the object normally in the view model, or the ID of the object to edit, and in this case, the viewmodel can retrieve the object itself and edit it.
To achieve this, in the code behind of the PersonView (associated with the PersonViewModel) has to send a message upon navigation (OnNavigatedTo) to its ViewModel containing the received info from the query string.

how to parametrize an import in a View?

I am looking for some help and I hope that some good soul out there will be able to give me a hint :)
I am building a new application by using MVVM Light. In this application, when a View is created, it instantiates the corresponding ViewModel by using the MEF import.
Here is some code:
public partial class ContractEditorView : Window
{
public ContractEditorView ()
{
InitializeComponent();
CompositionInitializer.SatisfyImports(this);
}
[Import(ViewModelTypes.ContractEditorViewModel)]
public object ViewModel
{
set
{
DataContext = value;
}
}
}
And here is the export for the ViewModel:
[PartCreationPolicy(CreationPolicy.NonShared)]
[Export(ViewModelTypes.ContractEditorViewModel)]
public class ContractEditorViewModel: ViewModelBase
{
public ContractEditorViewModel()
{
_contract = new Models.Contract();
}
}
Now, this works if I want to open a new window in order to create a new contract... or in other words, it is perfect if I don't need to pass the ID of an existing contract.
However let's suppose I want to use the same View in order to edit an existing contract. In this case I would add a new constructor to the same View, which accepts either a model ID or a model object.
"Unfortunately" the ViewModel is created always in the same way:
[Import(ViewModelTypes.ContractEditorViewModel)]
public object ViewModel
{
set
{
DataContext = value;
}
}
As far as I know, this invokes the standard/no-parameters constructor of the corresponding ViewModel at composition-time.
So what I would like to know is how to differentiate this behavior? How can I call a specific constructor during composition time? Or how can I pass some parameters during the Import?
I really apologize if this question sounds silly, but I have only recently started to use MEF!
Thanks in advance,
Cheers,
Gianluca.
You CAN do this. Check out the Messenger implementation in MVVM-Light. You can pass a NotificationMessage(Of Integer) to send the right ID to the view model. The view model has to register for that type of message, and load it when a message is sent.
MEF Imports by default only have a parameterless constructor.