Designer messes up location and size of a Custom UserControl child container that is design enabled - user-controls

When I put this control on to a form, change it's size and location, save it and close the form. After opening it, location and size are not the same, but in ".Designer.cs" it's exactly how I set it.
I can't find a solution to this problem, not even someone mentioning it.
This is a simple example of a custom control I am using:
[Designer(typeof(myControlDesigner1))]
public partial class UserControl1 : UserControl
{
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[TypeConverter(typeof(Panel))]
[MergableProperty(false)]
public System.Windows.Forms.Panel Panel
{
get
{
return pnlWorkingArea;
}
set
{
pnlWorkingArea = value;
}
}
public UserControl1()
{
InitializeComponent();
}
}
public class myControlDesigner1 : ControlDesigner
{
public override void Initialize(IComponent component)
{
base.Initialize(component);
UserControl1 bc = component as UserControl1;
EnableDesignMode(bc.Panel, "MyPanel");
}
}

Yes I can reproduce your issue now, that is because the panel is inside usercontrol, they are added as a whole to the Form, that means the location of the panel is relative to usercontrol, so if you set the location of the panel is (x, y), then when you reopen the form, the actual location of the panel will be (usercontrol.location.X+x, usercontrol.location.Y+y).
You can find there is not any problem if you set the location of the usercontrol in the form is (0, 0), please have a try.
If you do not want to set the location of the usercontrol is (0, 0), as an alternative solution, you can add the following code in Form_Load event, so the location will be where you set it when you run the form:
private void Form1_Load(object sender, EventArgs e)
{
this.userControl11.Panel.Location = new Point(userControl11.Panel.Location.X - userControl11.Location.X, userControl11.Panel.Location.Y - userControl11.Location.Y);
}

Related

How to move an Object to a specific location with a Button in Unity?

I'm new to Unity and am currently using Unity2D. What I want to do is that when a button is pressed an object moves and goes to a specific location. Currently I'm using:
public class MoveCard : MonoBehaviour
{
public float speed = 20f;
public Rigidbody2D rb;
public Vector2 pos = new Vector2(5,1);
void Update(){
}
void CardMovement(){
rb.MovePosition(pos * speed * Time.deltaTime);
}
}
I want the function CardMovement to initiate when I press a button, but it's not showing up in the Button Inspector and I don't know how to reference a Rigidbody2D in a button. How do I do this?
In the inspector in the buttons 'OnClickEvent' menu, click the plus button, drop in the object that contains 'MoveCard' component. In the drop box on the right find the method 'CardMovement'. Just a note in your current code you have it set up so if you press the button it will move only one frame's worth of movement. You could change to something like this:
public class MoveCard : MonoBehaviour
{
public float speed = 20f;
public Rigidbody2D rb;
public Vector2 pos = new Vector2(5,1);
public bool moving = false;
void Update(){
if (moving) {
rb.MovePosition(pos * speed * Time.deltaTime);
}
}
void CardMovement(){
moving = true;
}
}
edit (still not a bad idea):
//..
void FixedUpdate()
{
if (moving) {
rb.MovePosition(pos * Time.fixedDeltaTime);
}
}
//..
edit edit: set isKinematic to true on the rigidbody
The function your button is calling should be public.
public void CardMovement(){
rb.MovePosition(pos * speed * Time.deltaTime);
}
}
After you change it, assign again the object that has the script and try.
what you have to do is make a function in a script and apply that script to a game object. after making that create the button and click on the add function button to the button and drag the gameobject into the open section. afterwards chose the function from the listed scripts you added and have it call the move object script or whatever you called it.

MvvmCross and UIButton.Selected UISegmentedControl Bindings, iOS

In a cross platform Xamarin app built with the MvvmCross framework I'm using a ToggleButton Widget in an Android .axml layout. I've bound the Checked property to a View Model property using a converter using the following binding syntax:
Checked MarketBuySellViewModel.Direction, Converter=DirectionBool, ConverterParameter='Sell'
Everything works well. On the iOS side, it appears you can use UIButton as a ToggleButton by using the Selected property. This implies that the following binding should achieve what I want on iOS:
set.Bind (SellButton).For(b => b.Selected).To (vm => vm.MarketBuySellViewModel.Direction).WithConversion("DirectionBool", "Sell");
I don't get any binding errors in the application output but the binding itself doesn't seem to work. Clicking the button doesn't set the Direction property and setting the direction to a different value does not set the Selected property on the UIButton.
Do I need to create a Custom Binding or am I simply setting up the binding incorrectly?
I also tried using a UISegmentedControl to achieve the same effect. Is binding to this control supported at all in MvvmCross? I don't see any reference to it in the source code. Does this mean I need to create custom bindings for it too?
For the UIButton, I don't believe there's any included Selected binding built into MvvmCross. Because of this - and because Selected doesn't have a simple paired event SelectedChanged, then I believe Selected binding should work one-way (from ViewModel to View) but not two-way.
There is a binding for the On of a UISwitch control and that's the control I've seen used most in these situations.
If you wanted to add a custom 2-way binding for Selected then I guess you'd have to do this using the ValueChanged event (but would need to check that is correct).
To do so, you'd just build a target binding something like:
public class MvxUIButtonSelectedTargetBinding : MvxPropertyInfoTargetBinding<UIButton>
{
public MvxUIButtonSelectedTargetBinding(object target, PropertyInfo targetPropertyInfo)
: base(target, targetPropertyInfo)
{
var view = View;
view.ValueChanged += HandleValueChanged;
}
private void HandleValueChanged(object sender, System.EventArgs e)
{
var view = View;
if (view == null)
return;
FireValueChanged(view.Selected);
}
public override MvxBindingMode DefaultMode
{
get { return MvxBindingMode.TwoWay; }
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (isDisposing)
{
var view = View;
if (view != null)
{
view.ValueChanged -= HandleValueChanged;
}
}
}
}
and this could be registered in Setup in protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry) using something like:
registry.RegisterPropertyInfoBindingFactory(typeof(MvxUIButtonSelectedTargetBinding), typeof(UIButton),
"Selected");
Similarly, I don't believe anyone has added a two way UISegmentedControl binding yet - but would happily see one added.
Building a two way UISegmentedControl binding would be quite straight-forward - you'd just have to bind to the pair SelectedSegment and ValueChanged - with code similar to above.
Alternatively, you could switch to using a custom MySegmentedControl which had a nicer Value`ValueChanged` pair which would automatically work without a custom binding - e.g.:
public class MySegmentedControl : UISegmentedControl
{
// add more constructors if required
public int Value
{
get { return base.SelectedSegment; }
set { base.SelectedSegment = value; }
}
}
If any or all of these custom bindings are needed, then the Mvx project is happy to get these bindings added as issues or pull requests along with test/demo UIs in the https://github.com/slodge/MvvmCross-Tutorials/blob/master/ApiExamples/ApiExamples.Touch/Views/FirstView.cs project
Could be helpful to someone else, so i'm sharing my experience. I needed a two way binding for UISegmentedControl.SelectedSegment property to a ViewModel. The one way biding (ViewModel => View) works by default. I couldn't able to properly utilize the solution proposed by Stuart - to subclass the UISegmentedControl. I tried to ensure that the linker does not rip off the new custom control code, but this didn't help me a bit. So a perfectly viable solution is the one with MvxPropertyInfoTargetBinding. Here is the code working ok for me:
public class MvxUISegmentedControlSelectedSegmentTargetBinding : MvxPropertyInfoTargetBinding<UISegmentedControl>
{
public MvxUISegmentedControlSelectedSegmentTargetBinding(object target, PropertyInfo targetPropertyInfo)
: base(target, targetPropertyInfo)
{
this.View.ValueChanged += HandleValueChanged;
}
private void HandleValueChanged(object sender, System.EventArgs e)
{
var view = this.View;
if (view == null)
{
return;
}
FireValueChanged(view.SelectedSegment);
}
public override MvxBindingMode DefaultMode
{
get { return MvxBindingMode.TwoWay; }
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (isDisposing)
{
var view = this.View;
if (view != null)
{
view.ValueChanged -= HandleValueChanged;
}
}
}
}
public class Setup : MvxTouchSetup
{
...
protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
registry.RegisterPropertyInfoBindingFactory(typeof(MvxUISegmentedControlSelectedSegmentTargetBinding), typeof(UISegmentedControl), "SelectedSegment");
}
}

Common menu item for all scenes in Java FX

I want to develop a multiple scenes Java FX application. But I want to have the common menu across all scenes. I think using FXML I can create a menu in a scene. But Can I have the same menu across all the scenes even after I navigated to other screen?
If so how is it. Else let me know any alternative for it.
Yes this is possible. I'm using this mechanism in my own application.
What I do first is make an FXML with the menu bar and an AnchorPane who contains the content. This FXML is loaded when the application starts.
I use a Context class (based on the answer of Sergey in this question: Multiple FXML with Controllers, share object) which contains a method ShowContentPane(String url) method:
public void showContentPane(String sURL){
try {
getContentPane().getChildren().clear();
URL url = getClass().getResource(sURL);
//this method returns the AnchorPane pContent
AnchorPane n = (AnchorPane) FXMLLoader.load(url, ResourceBundle.getBundle("src.bundles.bundle", getLocale()));
AnchorPane.setTopAnchor(n, 0.0);
AnchorPane.setBottomAnchor(n, 0.0);
AnchorPane.setLeftAnchor(n, 0.0);
AnchorPane.setRightAnchor(n, 0.0);
getContentPane().getChildren().add(n);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
So what basically happens is:
When the program starts, set content pane in the Context:
#Override
public void initialize(URL url, ResourceBundle rb) {
Context.getInstance().setContentPane(pContent); //pContent is the name of the AnchorPane containing the content
...
}
When a button or menuitem is chosen, I load the FXML in the content Pane:
#FXML
private void handle_FarmerListButton(ActionEvent event) {
Context.getInstance().showContentPane("/GUI/user/ListUser.fxml");
}
Hope this helps :)

Lwuit touch screen strange behaviour

I am making an application using LWUIT.
There is a form
There is a list embedded on the form.
The list has 5 elements.
Initially, when I first load the app, if I choose the 1st element, 2nd gets chosen; when I choose the second the 3rd gets chose and and so on (Weird!)
I am not able to click any button on the screen either
next what I do is, shift to a different from using arrow keys (of the keyboard... I am running the app on a simulator btw)
Then I come back to the first form and now everything works as expected(no weird behaviour).
What could be the issue?
I am using Sun Java Micro Edition SDK 3.0 (default touch screen for testing)
My code is:
List dummy = new List();
dummy.addItem("wewerwer");
dummy.addItem("wewerdswer");
dummy.addItem("wewqweerwer");
dummy.addItem("dscxwewerwer");
dummy.addItem("jhgwewerwer");
mainListForm.setLayout(new BorderLayout());
mainListForm.addComponent(BorderLayout.CENTER,dummy);
mainListForm.show();
What could possible be going wrong here?
UPDATE 1
I think there is a bug here. I have attached the complete code below along with the screen shot
import javax.microedition.midlet.*;
import com.sun.lwuit.*;
import com.sun.lwuit.events.*;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
public class Demo extends MIDlet implements ActionListener {
private Form mForm;
List abc;
public void startApp() {
Display.init(this);
try {
Resources r = Resources.open("/Test.res");
UIManager.getInstance().setThemeProps(r.getTheme(
r.getThemeResourceNames()[0])
);
} catch (Exception e){
System.out.println(e.toString());
}
if (mForm == null) {
Button click = new Button("Press me!");
click.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("I have been pressed");
}
});
abc = new List();
abc.addItem("Str1");
abc.addItem("Str2");
abc.addItem("Str3");
abc.addItem("Str4");
abc.addItem("Str5");
abc.addItem("Str6");
Form f = new Form("Hello, LWUIT!");
abc.addActionListener(this);
f.addComponent(abc);
Command exitCommand = new Command("Exit");
f.addCommand(exitCommand);
f.addCommandListener(this);
f.addComponent(click);
f.show();
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void actionPerformed(ActionEvent ae) {
System.out.println(abc.getSelectedIndex());
}
}
So now when I click on 'Str1' of the list Str2 gets selected and so on.
IDE: Netbeans
Emulator: Default Touch screen phone
On the action event set the list to active again after the event by invoking setHandlesInput(true)
OK....so this is how you resolve it.
After the form is displayed remove the list from the form and again add it to the form and then repaint the form.
Earlier Code
1) form.addComponenet(BorderLayout.center,list);
2) form.show();
Word Around for the problem
1)form.addComponenet(BorderLayout.center,list);
2)form.show();
3)form.setScrollable(false);
I know its kind of strange, but this way the list index selection works smooth for touch screen phones.

How to use QLPreviewController in a non-modal way? Why does my code not work?

I have QLPreviewController up and running but I'm using PresentModalViewController() to show the QLPreviewController directly. For reasons beyond explanation, I would like to have my own UIViewController which will create its own view and within that view I would like to use the QLPreviewController. Should be easy I thought, but the code below just does nothing. The QLPreviewControllers ViewDidAppear never gets called. (In my example below, PreviewController inherits from QLPreviewController and encapsulates delegate, preview item and source).
Can somebody explain what is wrong with the code below (besides the fact that it is pointless :-))?
Oh, yeah: in my test scenario, I present the controller below modally. It shows up but witout the preview.
public class OuterPreviewController : UIViewController
{
public OuterPreviewController (QLPreviewControllerDataSource oDataSource) : base()
{
this.oDataSource = oDataSource;
}
private PreviewController oPreviewController;
private QLPreviewControllerDataSource oDataSource;
public override void LoadView ()
{
this.View = new UIView();
this.View.Frame = new RectangleF(0, 0, 500, 500);
this.View.BackgroundColor = UIColor.Red;
}
public override void ViewDidAppear (bool animated)
{
// Code execution comes her. No errors, no issues.
base.ViewDidAppear (animated);
this.oPreviewController = new PreviewController();
this.oPreviewController.DataSource = this.oDataSource;
// Preview controller's view is added but it never shows up.
this.View.AddSubview(this.oPreviewController.View);
this.oPreviewController.View.Frame = this.View.Frame;
this.oPreviewController.View.Center = this.View.Center;
}
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
return true;
}
}
Found a solution by coincidence today: all ReloadData() on the preview controller and magically it will show its contents.
This allows to add a QLPreviewController to an existing view as a subview and embed a preview. It also gets you rid of the toolbar which contains the open in menu.

Resources