Drag and Drop in GUI - oop

Is it possible to create an object in a GUI whose position I can define by the cursor position (drag when clicked) by setting its 'Position' property to whatever the cursor position is? What function should I be using?

You can use the SELECTMOVERESIZE function to turn on moving and resizing for your GUI object. Then you can just click and drag the object with the mouse. It's as simple as this:
set(hObject,'ButtonDownFcn','selectmoveresize');
What's not so simple is if your GUI object is a uicontrol object, in which case you will have to disable the object by setting the 'Enable' property to 'off' or 'inactive' in order to have the 'ButtonDownFcn' function execute instead of the 'Callback' function. This is true even if you haven't defined a callback for the object.
You will also probably need to add a means to your GUI to turn the moving and resizing of the object on and off, perhaps an extra button or a menu item you can select. To show how you could do this with a push button, here's a simple example that creates a figure with an editable text box and a push button that turns on and off the ability to move and resize the editable text box:
function GUI_example
hFigure = figure('Position',[100 100 200 200],... %# Create a figure
'MenuBar','none',...
'ToolBar','none');
hEdit = uicontrol('Style','edit',... %# Create a multi-line
'Parent',hFigure,... %# editable text box
'Position',[10 30 180 160],...
'Max',2,...
'String',{'(type here)'});
hButton = uicontrol('Style','pushbutton',... %# Create a push button
'Parent',hFigure,...
'Position',[50 5 100 20],...
'String','Turn moving on',...
'Callback',#button_callback);
function button_callback(hSource,eventData) %# Nested button callback
if strcmp(get(hSource,'String'),'Turn moving on')
set(hSource,'String','Turn moving off'); %# Change button text
set(hEdit,'Enable','inactive',... %# Disable the callback
'ButtonDownFcn','selectmoveresize',... %# Turn on moving, etc.
'Selected','on'); %# Display as selected
else
set(hSource,'String','Turn moving on'); %# Change button text
set(hEdit,'Enable','on',... %# Re-enable the callback
'ButtonDownFcn','',... %# Turn off moving, etc.
'Selected','off'); %# Display as unselected
end
end
end
Note: although the documentation lists the 'Selected' property as read-only, I was able to modify it without a problem. It must be a typo in the documentation.

You can create an invisible axes in your GUI, and plot whatever objects you want in there. Then, you can use DRAGGABLE from the File Exchange to allow for dragging objects all over the place.

Related

VB NET What is the good way to find a position of an object inside a scrollable panel?

I have an object inside a scrollable panel, and I want to get the position of the object. Everything is okay until I have create many object inside the panel until the panel needed to be scrolled. Then I notice that the X Position of the object changed after scrolling the panel. Take a look at example image below
X Position inside a scrollable panel
I'm using Position.X to get a X Position of the object, but as you can see in the picture above. It does me no good.
Is there a way to get the actual position even if the panel is scrolled ?
UPDATE:
I have One Panel that have Auto Scroll as True , and on Button inside a Panel, let's just say the button name is Button1 and the Panel is Panel1
The Code i use to get the location of the Button1 is :
Dim msg = Button1.Location.X
msgbox(msg)
You're getting the screen position of the object, I don't know how because you didn't post your code. What you need to do is add the scroll offset, eg.
Position.X + scrollableControl.AutoScrollPosition.X

Changing MDI background affects child form background [duplicate]

How can I change the BACKGROUND color of the MDI FORM in C#?
I changed it using the background color property but the color is not changed.
What should I do to perform this task?
The actual BackGround colour of the MDI control is based on the colour in the Windows current Theme. You have to physically set the MdiClient control's background inside the WinForm.
// #1
foreach (Control control in this.Controls)
{
// #2
MdiClient client = control as MdiClient;
if (!(client == null))
{
// #3
client.BackColor = GetYourColour();
// 4#
break;
}
}
Edit - Added comments:
We need to loop through the controls in the MdiParent form to find the MdiClient control that gets added when you set the Form to be an MdiParent. Foreach is just a simple iteration of a type through a collection.
We need to find the MdiClient control within the form, so to do this we cast the current control within the loop using the 'as' keyword. Using the 'as' keyword means that if the cast is invalid then the variable being set will be null. Therefore we check to see if 'client' is null. If it is, the current control in the loop is not the MdiClient control. As soon as the variable 'client' is not null, then the control we've got hold of is the MdiClient and we can set its background colour.
Set the backcolour to anything you want. Just replace "GetYourColour()" with whatever colour you want, i.e. Color.White, Color.Blue, Colour.FromArgb(etc)...
As there is only ever 1 MdiClient, there's no point continuing the loop as it's just a waste of processing time. Therefore we call 'break' to exit the loop.
Let me know if you want anything else explaining.
Write this in your load method of your MDI form.
Controls.OfType<MdiClient>().FirstOrDefault().BackColor = Color.Purple;

Custom context menu XAML for WP8

I try to implement a custom ContextMenu in a LongListSelector.
I'm not using the ContextMenu from Microsoft.Phone.Controls.Toolkit, it's basically the same as in the Rowi App:
(source: hiddenpineapple.com)
Approach 1
My list item toggles a VisualState on hold and an overlay is shown with controls in it.
The problem
I can't find a way to go back to the default state when the user clicks outside of the list item (as in the default ContextMenu).
Approach 2
I've implemented a custom template for the toolkit ContextMenu which looks exactly the same. I had to move its margin top to -itemHeight, as by default it is below the item.
The problem
The problem with this solution is, that it automatically closes itself when opening and I couldn't figure out how to avoid this.
Another problem was that it didn't work well with TiltEffect.IsTiltEnabled from the Toolkit (visual problems).
I need your help
Any suggestions on how to get this working?
Answer
Thanks to Cheese, now I know how to properly close the menu when the user clicks outside.
His suggestion was to get the coordinates of a Tap event on the current page, and check if it's inside the menu. When not, close the menu.
So I added a Tap listener to the page when the menu opens, and removed it when the menu closes. From the page listener I got the event coordinates and could check if it's inside the control which holds the menu (same size and position). I received the position of the control with Point leftUpperPoint = control.TransformToVisual(page).Transform(new Point(0, 0)) and the rightLowerPoint by adding the ActualWidth and ActualHeight.
But then I realized:
Why should I even calculate if the tap is inside the menu? I always want to close the menu when the user taps anywhere on the screen. If it's outside, yes. If it's on a menu button, yes.
Another modification I made was to listen for MouseLeftButtonDown instead of Tap as it also triggers when the user swipes.
So I removed this code and came up with the following:
private void ToggleMenu(object sender, System.Windows.Input.GestureEventArgs e)
{
PhoneApplicationFrame frame = ((PhoneApplicationFrame)Application.Current.RootVisual);
VisualState state = this.States.CurrentState;
if (state == null || state.Name == "DefaultState")
{
frame.MouseLeftButtonDown += MouseDownDelegate;
this.State = "MenuState";
}
else
{
frame.MouseLeftButtonDown -= MouseDownDelegate;
this.State = "DefaultState";
}
}
private void MouseDownDelegate(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
ToggleMenu(sender, null);
}
This works perfectly!
Thanks to Cheese for the hint.
Something like this by #denniscode http://dotnet.dzone.com/articles/rowi-show-tap-menu
Approach 1 problem
The best solution would be:
Get the menus coordinates, when user makes a tap - you check are tap coordinates on menu or not, if not - dissmiss - simple.
Approach 2 problem
I guess you had some button in a corner and when you tapped on it - nothing happened? And when you dissmissed the Tilt all worked. It seems that tilt works faster than a click, so, tilt changes the button coordinates, and device thiks you have missed/or dragged off
You can use what #ScottIsAFool suggested and maybe create another Dependency Property on your TapMenu control of type UIElement named CloseWhenTappedElement and automatically listen for Tap events inside your control once set. For example
<Grid x:Name="TapArea"/>
<TapMenu CloseWhenTappedElement="{Binding ElementName=TapArea"}/>

NSMatrix of buttons and Bind issue

This is my situation:
a NSMatrix (in radio mode) that contains 4 buttons
An object with properties "top","left","right","bottom" and the relative objectController.
each button has state bind to one of the objectController key (top,left,right,bottom).
Radio mode makes me sure that only one button at once has state = on, my problem is that when i select a button the object property chained to the objcet controller goes to 1, but the previous one selected (now with state = off) doesn't go to 0 (the buttons view works correctly and only 1 button is active at time).
How can i obtain a on/off effect also on the bind object?
Instead of binding each button, you should bind the selectedIndex binding of the NSMatrix itself to a property in your model.
You'll need to change the way you store the selected edge in your code by using a single property:
typedef enum {
TopEdge = 0,
RightEdge,
BottomEdge,
LeftEdge
} Edge;
#property Edge currentEdge;
This will allow you to keep track of the currently selected index.

NSMenu --> Adding SubMenu --> receive select event

I need to create a Menu for a selected row of a table,
I am able to create by overriding
-(NSMenu*)menuForEvent:(NSEvent*)evt
Method, of a table, This particular Menu has two sub-menu, i am able to make the Sub-Menu
but facing Following Problem
1 -- To Add a Sub-Menu in a MenuItem, i need to get Sub-Menu from a different class/Interface, i am calling following method
pSubMenu = [CommonUIUtil GetCommonUIMenu:pSubMenu
ActionId:self
Selector:#selector(MyMenuAction)];
Where prototype of this function is as follow
+(NSMenu *)GetCommonStatusMenu:(NSMenu *)pMenu ActionId:(NSObject*)aDelegate Selector:(SEL)selector
Implementation as below,
// pStrArray is Array of String to have the Menu Title
for(int idx =0;idx<[pStrArray count];idx++)
{
pTempStr = [pStrArray objectAtIndex:idx];
pImage = [CommUIController CommonGetImage:[CommonUIUtil GetImageByStatus:pTempStr]];
[pImage setSize:NSMakeSize(20,20)];
pMenuItem =[[NSMenuItem alloc]init];
[pMenuItem setTitle:pTempStr];
// this should set the selector
[pMenuItem setAction:selector];
// Setting the target
[pMenuItem setTarget:aDelegate];
[pMenuItem setImage:pImage];
[pMenuItem setEnabled:YES];
[pMenu addItem:pMenuItem];
[pMenuItem release];
}
return pMenu;
I am able to see the Image, String on this Sub Menu, but the problem i am facing is this menu is not at all Enable, can anyone guide me , where i am making the mistake,
This function will return the Menu and Menu i will add as below,
pMenuItem = [pCTTableMenu itemWithTitle:#"Status"];
//status menu is the menu returned from the above function,
[pMainMenu setSubmenu:pStatusMenu forItem:pMenuItem];
Thanks in Advance :)
looks like, i am not passing the selector method properly, in fact i don't know how to pass function pointer in Cocoa, probably i am mixing Cocoa/Objective C and Normal C both :), corrected, it, in the method just creating the view and in the main class/interface assigning target and action