Stuck on circular dependency on windows forms C++ - c++-cli

I believe that the problem is that my code is stuck on circular dependency. I'm trying to send value from one form to other. In form1 I declare Form2 and open it and therefore I can't declare Form1 from Form2 and I don't know what to do now.
Code:
private: System::Void paieškaToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e)
{
PaieskaGUI^ paieska = gcnew PaieskaGUI(); //declaring the other form
paieska->Show(); //opening it when the button is pressed
}
So above is the form1 where when I press the button and it opens form2.
And in the code below which shows form2 when I press the button it declares form1 and sends the value to it
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
String^ mas = textBox1->Text;
Pagrindinis^ pgrr = gcnew Pagrindinis();
pgrr->SomeText = mas;
this->Hide();
}
I'm new at programming.

If I understand correctly you are looking to pass a value back to the original form and not create a new instance of one. If that is the case, maybe consider setting the Parent on form2 before you show it so that you can get the first form from the Parent property and then set the relevant value on it after casting it to the appropriate type.
In the code you are also hiding the second form after the button is clicked, do you intend for the form to just be a dialog to get some value and return it to the parent? If so, maybe you want to consider using ShowDialog instead which would prevent interaction with the parent until returning and then you could use the instance of the second form from the calling method to access properties on that form. The example on MSDN for the Form.ShowDialog method shows how you can get a value from a second form to the first form.

Related

How to receive property change events in an AbstractInformationControl

I am working on an editor plugin and now implementing support for code hovers.
I have extended AbstractInformationControl (and implemented IInformationControlExtension2) to create a new control for showing the hover info.
It works almost fine, but I am unable to receive property change events in my information control. What I try is like this, but the event handler does not fire:
public class MyHoverInfoControl extends AbstractInformationControl implements IInformationControlExtension2 {
public MyHoverInfoControl(Shell parentShell, String string) {
super(parentShell, string);
create();
Activator.getDefault().getPreferenceStore().addPropertyChangeListener(new IPropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent event) {
// TODO Auto-generated method stub
}
});
}
The control itself contains a StyledText. I have also tried to add a KeyListener to this StyledText to see if key events are received, and it only seemed to work if I click into the control (it is not enough to hover over a text to show the control). But property change event does not seem to be received even after the click.
Is it possible to receive property change events for an AbstractInformationControl subclass?

Inherit UserControl and hooking up to basic property events

I'm making a custom TextBox for UWP to simplify Win2D outlined text solution, for that I created a UserControl that contains only a canvas on which I'll draw the text.
Of course I need some properties, like text, outline thickness and color, etc...
I also need some properties that are already exposed by the inherited UserControl like Foreground, FontSize, FontFamily...
So far so good, it seems like I won't need to implement each one of those common properties.
The problem is that I can't find a way to hook up an event when one of those properties changes, as I have to call the Canvas.Invalidate() method to redraw it when the format changes.
Looks like I have to hide all those properties and create new Dependency Properties to call Canvas.Invalidate().
There is no way to do it faster?
Nevermind, the answer was behind the corner.
In the constructor, you can call
RegisterPropertyChangedCallback(DependencyProperty dp, DependencyPropertyChangedCallback callback);
For example:
public OutlinedText()
{
InitializeComponent();
RegisterPropertyChangedCallback(FontFamilyProperty, OnPropertyChanged);
RegisterPropertyChangedCallback(FontSizeProperty, OnPropertyChanged);
}
private void OnPropertyChanged(DependencyObject sender, DependencyProperty dp)
{
OutlinedText instance = sender as OutlinedText;
if (instance != null)
{
//Caching the value into CanvasTextFormat for faster drawn execution
if (dp == FontFamilyProperty)
instance.TextFormat.FontFamily = instance.FontFamily.Source;
else if (dp == FontSizeProperty)
instance.TextFormat.FontSize = (Single)instance.FontSize;
instance.needsResourceRecreation = true;
instance.canvas.Invalidate();
}
}

OnPropertyChanged is being called twice for a bindable property

In Xamarin Forms, I created a bindable property like so:
public static readonly BindableProperty SelectedItemProperty = BindableProperty.Create("SelectedItem", typeof(MyItem), typeof(MyGrid), default(MyItem));
public MyItem SelectedItem
{
get { return (MyItem)GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
Here's my constructor:
public MyView()
{
InitializeComponent();
PropertyChanged += OnPropertyChanged;
}
protected void OnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
{
if (propertyChangedEventArgs.PropertyName == "SelectedItem")
{
// called twice
}
}
Can somebody explain why property changed event is firing twice? If I create a changed handler in the definition of the bindable property, then the handler is called once.
public static readonly BindableProperty SelectedItemProperty = BindableProperty.Create("SelectedItem", typeof(MyItem), typeof(MyGrid), default(MyItem), null, SelectedItemChanged);
I have noticed the issue exists only in code-behind. If I set the property directly in XAML, property changed event fires once.
We don't really have enough information to answer this question with certainty, but I can speculate.
Without seeing your SetValue method, my assumption would be that it lacks a short circuit, e.g. "don't do anything if the new value and the old value are the same".
Then my second assumption would be that the control that is being bound to this property is setting it (after being bound). This can happen with list-type controls when SelectedItem is bound.
The resulting chain of events might be something like:
Code sets property
PropertyChanged event is fired
Binding sets the value on a control
Control reports it's value has been changed, and two-way binding sets the value on the ViewModel again
The lack of a short circuit causes the PropertyChanged event to be raised again
The binding sets the value on the control again (to the same value as before)
The control does not report a change, because it's property is short-circuited properly
My guess is that if you were to short circuit your setter (by checking against the existing value and bailing out if they are the same) this behavior would stop.

Listbox.SelectedItems.Add not causing SelectionChanged event

I have a custom user control extending the Listbox class. Inside of it I am overriding OnSelectionChanged to add/remove Adorners to any selected/unselected items. This all works when I select an item using the mouse, but when I programmatically add items to the listbox using
myListBox.SelectedItems.Add(newItem) // newItem is already a member of myListBox.Items
It does not execute the OnSelectionChanged code.
Update: Unless I'm crazy (which is always possible) it seems there is a difference in behaviour between calling this from the parent object
myListBox.SelectedItems.Add(newItem)
and this method inside my extended listbox class
Public Sub AddSelectedItem(newItem as Object)
Me.SelectedItems.Add(newItem)
End Sub
For some reason the second option is triggering the event while the first one isn't.
you need to add this line of code first
myListBox.Items.Add(newItem)
The solution here is that calling SelectedItems.Add() from inside an extension of ListBox
public class MyListBox : ListBox
{
public void AddSelectedItems(object newSelectedItem)
{
// works
this.SelectedItems.Add(newSelectedItem);
}
}
will trigger the OnSelectionChanged event.
Calling it like this from the window will not trigger the event
private sub SomeWindowMethod()
{
// does not work
this.MyListBoxInstance.SelectedItems.Add(newSelectedItem);
}

How can you add a menu in a main form from a dll?

I have a main form that reference to a dll. Inside the dll is also a form that will add additional menu in the main form that calls it. How can I do that? Please show sample codes. thanks.
It is unclear the interaction between those forms but I think you have to pass a main form reference to the other form allowing it to modify main form controls.
Something like this:
MainForm:
Form2 form2=new Form2(this);
Form2:
private MainForm mainForm;
public Form2(MainForm mainForm)
{
this.mainForm=mainForm;
}
private void DoSomething()
{
this.mainForm.Controls. ....
}
updated
You can also use reflection or dynamic type.
look here:
http://www.codeproject.com/KB/cs/csharpreflection.aspx
or here for use of dynamic:
http://msdn.microsoft.com/en-us/library/dd264736.aspx
those example are in c# but it is easy to convert in vb.net with a tool like this:
http://converter.telerik.com/