How to find the id of dragged control - c#

I am implementing drag and drop functionality among lables. I want to find the ID of a dragged control like label, button, etc so that I can assign a text to it.
I am not sure how to get that data through the events. Any suggestion will help.

you can use Drag_Enter and DragOVer Events, to achieve the goal DragEnter Doucmentation
and DragOver Documentation
alternativily you can check following tutorials
Code project : Drag and Drop in Windows Forms
Code Project : Drag and Drop UI in WinForms

check out System.Windows.Forms.DragEventArgs e
void MyControl_DragDrop(object sender, DragEventArgs e)
{
var controlBeingDrag = (Label)sender; // cast from object
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop, false);
}
the control sending drag event is object sender

Assuming you've done your research to figure out how to do drag and drop of controls on your form (and your question is really only limited to your question title: How to find the id of dragged control) most standard WinForm events provide a parameter (object sender) which represents the control used to invoke the event. You should be able to get its ID as you would normally.
Apparently it is less obvious how to consistently get the ID from a WinForm control. Luckily, Brian McMaster has a (fairly old meaning only possibly relevant) MSDN blog post for doing just that. In .NET 3.5 I'd probably use this old post as the start to an extension method for control objects.
If your question is broader than that then you may benefit from #Ravi's links, but on SO we generally expect that you do your own research. Please be sure to do so before asking questions.

Related

Copy Controls from a list to another without the same pointer reference

Today I would like to create a copy/paste function for a software I develop.
I have a Panel that contains Controls and I want to copy/paste.
I have a selection tool that permit the user to select different Controls and add this Controls to a List. I have called it "SelectionActuelle".
Then, when the user clicks on "Copy". I would like to add every controls that SelectionActuelle contains into a new List called "PressePapier".
But when I do, it copies the same pointer reference, and I dont want.
I throught that add a Control to another List should copy it and create a new instance but it doesn't.
I tried this example HERE but it doesn't work.
What I have now is only 6 lines (it doesn't work !!) to try to make a copy of the List :
private void bt_copier_Click(object sender, EventArgs e)
{
PressePapier.Clear();
foreach(Control ctr in SelectionActuelle)
{
PressePapier.Add(ctr);
}
bt_coller.Enabled = true;
}
How can I simply copy Control to make my Copy/Paste tool ? So is it possible (I think yes but we never know) ?
Have a good day,
Julien
Thanks to you, I found out that my structure was not great and I changed it to a simpler structure and more reutilisable.
I will apply this :
You should foreach the selected controls and check it's type, just generate a new instance of that control with it's properties.
But Dr. Coconut, my problem was that .NET does not appreciate some expressions in the code even if I have adapted it... I don't know why. Question of framework version ?

How to limit focus to a Control in WPF

I Have a Page Containing some controls, this page will be loaded into a ContentControl in a Window.
Now, How can i limit the focus cycle in my Page? I don't want to pass the focus to out of Page after pressing TAB in last item of my page.
As a brief, How Can I determine the next focus control and change it
It's always difficult to fully understand what a question author wants when they don't bother to provide you with more than a few hastily typed lines. However, as far as I understand your problem, it seems that you have some problem with focusing in your WPF Application.
The first thing to do is to direct you to the Focus Overview page on MSDN, where you can find out about the different focus types used in WPF. In particular, please pay attention to the Navigating Focus Programmatically section which discusses the TraversalRequest Class that can help developers to move focus programmatically.
You should also pay careful attention to the KeyboardNavigation section that discusses the KeyboardNavigation Class. This class contains some properties that enable you to define how the Tab key works in various scenarios, so this may be what you're after. It is used like this (from the last linked page on MSDN):
KeyboardNavigation.SetTabNavigation(navigationMenu, KeyboardNavigationMode.Cycle);
For future reference, you will get quicker and more accurate answers if you provide clear questions that include all of your requirements at the time of posting.
Have you tried looking at the FocusLost event - you might possibly be able to just refocus the page control with an event handler.
private void Page_LostFocus(object sender, System.Windows.RoutedEventArgs e)
{
((UIElement)sender).Focus();
}

How do I dynamically evaluate an event?

Please forgive me if this should be phrased differently in a programming context - I have no idea what to search for other than exactly what I used to title my question.
Context - I am attempting to build a word search game involving a 4x4 grid of random letters. I'm currently using a Label_Click event to change the background color of the label containing a given letter. In the future there will be many other actions than just this (evaluating whether an array of clicks forms a legitimate word, for instance), but I'm fundamentally stuck with this problem:
// pseudo-code
if (lblA1_wasClicked || lblB1_wasClicked || ... lblD4_wasClicked)
{
whichever_wasClicked... //perform actions
}
Perhaps I need to learn about creating an event handler for this situation? I have never created a custom event handler, I just know what they are in theory. I am kind of stuck and at this point it's tough to know what to search for to keep learning. I know only enough to be dangerous, so please go easy on me if the answer is as straight-forward as my question feels. :)
If you are in the ui, you can go to the properties of each label and set their Click handler to the same method. so if you have a method with a signature like this.
public void label_Click(object sender, EventArgs e)
{
}
Then assign that to each label and then use that to process each one. This is a naive example, and there are much better methods that use established patterns for creating windows forms applications. Check out this question or search for MVP pattern windows forms to get more info on it!

Maintain Control focus across post backs using PostBackOptions.TrackFocus

Maintaining focus across post backs is an apparently difficult task. Searching Google, you will find a ton of people that desire the same thing, but all hook it up differently, and mostly, custom-ly. I would like to avoid a custom implementation, especially if there's a way it's supported by .NET. Only after some very deep searching, did I come across PostBackOptions.TrackFocus, mentioned quietly in another stack overflow post. According to MSDN:
Gets or sets a value indicating whether the postback event should return the page to the current scroll position and return focus to the current control."
Holy crap, this is supported by .NET 4? AWESOME. But we have a ton of custom controls, how does .NET know how to set the focus on a control? I have no idea. Looking a the MSDN documentation for System.Web.UI.Control, there's an interesting method:
public virtual void Focus()
"Use the Focus method to set the initial focus of the Web page to the
control. The page will be opened in the browser with the control
selected."
Alright, clearly overridable. But what is the recommended method of doing so? It returns void. No examples. Unable to find any examples of people overriding this method in their implementations. However, after overriding it and doing nothing more than throwing an exception, it becomes evident that this is not how ASP.NET gets focus on a control that had focus before the post back: it never gets called.
After a ton of debugging using Firebug, I have found that enabling PostBackOptions.TrackFocus works! Sometimes. It is apparent that the focus of a control is only maintained when the control calls the __doPostBack JavaScript method. Other controls that launch a PostBack (when pressing enter inside the control), call WebForm_OnSubmit(), which doesn't update the ASP hidden field __LASTFOCUS. __doPostBack calls WebForm_OnSubmit() after setting the hidden fields.
This is where I'm currently stuck. It's looks as if I need to get everything to call __doPostBack, no matter what. There's very, very little documentation on the use of TrackFocus. So does anyone have any tips from here?
I've been maintaining focus accross postbacks using the method in this article:
(ie: store focus in __LASTFOCUS hidden field on field enter event clientside for all controls)
http://www.codeproject.com/KB/aspnet/MainatinFocusASPNET.aspx
If you've gotten as far as having __LASTFOCUS show up on the page, this should get you most of the rest of the way...
Note: It'd be nice to find a way to keep the extra javascript from bloating __VIEWSTATE for example.
It was working pretty well for me until I figured out that some of my pages included the hidden __LASTFOCUS field and some of my pages didn't. (That's what prompted me to search around and find your question) Now I'm just trying to figure out how to make sure __LASTFOCUS always shows up on every page I want to keep track of focus on... (Looks like I'll have to open a separate question about it)
Here is what I just did. Assuming you have a handler in your code behind that takes care of the event and has a signature like this:
protected void myEventHandler(object sender, EventArgs e)
You can use this line of code to restore focus back to the sending object:
ScriptManager.RegisterStartupScript((WebControl) sender, sender.GetType(), "RestoreFocusMethod", "document.getElementById(\"" + ((WebControl) sender).ClientID + "\").focus();", true);
just using the Focus() method of the sending control will reposition the page (if you are scrolled down a bit), but this works beautifully. And if you have specific handlers for your control, you can just use the control itself rather than casting the sender to a WebControl, like this:
protected void CityListDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
...
ScriptManager.RegisterStartupScript(CityListDropDown, CityListDropDown.GetType(), "CityDropDownRefocus", "document.getElementById(\"" + CityListDropDown.ClientID + "\").focus();", true);
}

Generic method to get a control name when moused over?

I am writing an application that I am working on a skinning feature to it. What I want to do is allow a user to create an XML document that my code will parse through and set properties of the controls on a form. This is the easy part. Where I am stuck is giving the user a way to find out the control names/types with minimal coding/documentation effort.
My idea was to have a tool tip that when they moused over a control, it got the name of the control and type to show. Anyone know of a way to do this? I was thinking something like how Spy++ can find control, but I want to get the .NET properties also.
If someone has another idea I'm open ears.
Thanks Much.
Figured it out. The issue was because the mouse location wasn't relative to the client location. Thus the code below will resolve this issue. I put it in a polling thread that I already had going, but it should work with a timer or other event. Didn't work in MouseMove for some reason though. Thanks everyone for the help.
Point p = this.PointToClient(MousePosition);
Control x = this.GetChildAtPoint(p);
if (x != null)
{
MessageBox.Show(x.GetType().ToString() + " - " + x.Name);
}
You'll need to set up some ToolTip objects (one for each field), looping through all of the controls in your form to obtain the text for each tooltip.
http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.aspx
Are you willing to register for each controls mouse over event? If so, the sender would be the type, etc...maybe I'm missing something here? Furthermore you can get the control out of the visual tree in WPF/SL, why not port over to WPF?
You could recursively install a OnMouseOver eventhandler. Then if an OnMouseOver event occures the control is in the sender argument of the event handler.
Why wouldn't this work? I'm putting it in the base form, don't really see what it wouldn't work next to GetChildAtPoint being buggy.
protected override void OnMouseMove(MouseEventArgs e)
{
Control c = this.GetChildAtPoint(e.Location);
if (c != null)
{
MessageBox.Show(String.Format("Your control name is: {0} and type is {1}.", c.Name, c.GetType().ToString()));
}
base.OnMouseMove(e);
}

Categories