i am new to programming and i am trying to do a drag and drop here , i can drag and drop now but the custom cursor to drag and drop is ugly , how do i drag the element that i am draging as the cursor? i search online and found some mentioning about adorner but i don't even understand the codes . Is there any simple or simplified and better way to do this?
I have this code here that i can drag and drop ( i dynamically created textbox and label in a for loop , i retrieve the text and append it to label from a database :
TextBox tbox = new TextBox();
tbox.Width = 250;
tbox.Height = 50;
tbox.AllowDrop = true;
tbox.FontSize = 24;
tbox.BorderThickness = new Thickness(2);
tbox.BorderBrush = Brushes.BlanchedAlmond;
tbox.PreviewDrop += new DragEventHandler(tbox_Drop);
if (lstQuestion[i].Answer.Trim().Length > 0)
{
wrapPanel2.Children.Add(tbox);
answers.Add(lbl.Content.ToString());
MatchWords.Add(question.Content.ToString(), lbl.Content.ToString());
}
Dictionary<string, string> shuffled = Shuffle(MatchWords);
foreach (KeyValuePair<string, string> s in shuffled)
{
Label lbl = new Label();
lbl.Content = s.Value;
lbl.Width = 100;
lbl.Height = 50;
lbl.FontSize = 24;
lbl.DragEnter += new DragEventHandler(lbl_DragEnter);
// lbl.MouseMove += new MouseEventHandler(lbl_MouseMove);
lbl.MouseDown +=new MouseButtonEventHandler(lbl_MouseDown);
dockPanel1.Children.Add(lbl);
}
I am dragging labels(drag target) into textbox( drop target ) , which event should i use and how i write the events to set the drag cursor to the labels that i am dragging?
Here are the events i have used atm :
private void tbox_Drop(object sender, DragEventArgs e)
{
MessageBox.Show("Are you sure ? Wrong don't blame me ");
(sender as TextBox).Text = string.Empty;
}
private void lbl_DragEnter(object sender, DragEventArgs e)
{
if (sender == e.Source)
{
e.Effects = DragDropEffects.None;
}
}
Any solutions or help is appreciated , I've seen adorner , its way too complicated for me to understand to implement it . Looking for a simple and simplified way to do this .
You can use opensource like FluidKit
http://fluidkit.codeplex.com/
Tutorials for the same
http://blog.pixelingene.com/2006/11/drag-drop-with-attached-properties
http://blog.pixelingene.com/2006/11/drag-drop-with-attached-properties-part-2
http://blog.pixelingene.com/2006/12/drag-drop-with-attached-properties-part-3
http://blog.pixelingene.com/2006/12/drag-drop-with-attached-properties-part-4
http://blog.pixelingene.com/2007/07/making-the-scrollbar-work-with-dragdropmanager
http://blog.pixelingene.com/2007/11/improved-dragdropmanager-source-code
Related
My problem is in the picture:
How can i position automatically the next control (textbox in this sample), without a TableLayoutPanel?
Do you mean you want the TextBox move left/right based on the width of Label?
private void button2_Click(object sender, EventArgs e) {
int gap1 = textBox1.Left - label1.Right;
label1.AutoSize = true;
label1.Text = "long long long long long long long long";
textBox1.Left = label1.Right + gap1;
int gap2 = textBox1.Left - label1.Right;
label2.AutoSize = true;
label2.Text = "s";
textBox2.Left = label2.Right + gap2;
}
You firstly record the gap between the TextBox and Label, then set the AutoSize to true, followed by setting the new content of Label, finally you can move the TextBox accordingly.
Before:
After:
If you need to align multiple TextBox, or the width of TextBox as well, it would be more complicated, but you can follow the similar logic.
However, you have to write your own code but not doable in the Design view, as the Anchor of control is to the parent container but not the sibling control. Well, in Xcode on Mac you could do this, but AFAIK Visual Studio does not have this feature out of the box.
Here's a simple example of using a counter to track the number of controls created and compute the proper Y position:
private int counter = 0;
private void button1_Click(object sender, EventArgs e)
{
counter++;
int y = counter * 25;
Label lbl = new Label();
lbl.Text = "Label " + counter.ToString();
lbl.Location = new Point(5, y);
TextBox tb = new TextBox();
tb.Location = new Point(lbl.Bounds.Right + 5, y);
this.Controls.Add(lbl);
this.Controls.Add(tb);
}
i dynamic populate a dock panel with answers from database and another dock panel with questions from the database as well. the answers will be populated as Labels and i trying to do a drag and drop with labels to textblock . Yes i can drag and drop , but the thing is i want to drag the label too . For example if the Label content is Hello , i want the hello to be dragged over with the word " hello " as well , for now , when i drag it , it doesn't drag the word as well , but when i drop it into a textbox , the word " hello " is dropped . I want to drag the animation or word as well together with the cursor .
this is my code :
private void PopulateQuestion(int activityID, int taskID)
{
IList<Model.question> lstQuestion = qn.GetRecords(taskID, activityID);
StackPanel sp = new StackPanel();
StackPanel stp = new StackPanel();
foreach (Model.question qhm in lstQuestion)
{
StackPanel sp1 = new StackPanel() { Orientation = Orientation.Horizontal }; // Question
TextBlock tb = new TextBlock();
tb.Text = qhm.QuestionContent;
tb.FontWeight = FontWeights.Bold;
tb.FontSize = 24;
sp1.Children.Add(tb);
StackPanel sp2 = new StackPanel() { Orientation = Orientation.Horizontal }; // Answer
Label tb1 = new Label();
tb1.Content = qhm.Answer;
tb1.FontWeight = FontWeights.Bold;
tb1.FontSize = 24;
tb1.MouseLeftButtonDown += tb1_Click;
sp2.Children.Add(tb1);
TextBox tbox = new TextBox();
tbox.Width = 100;
tbox.FontSize = 24;
tbox.AllowDrop = true;
tbox.FontWeight = FontWeights.Bold;
if (qhm.Answer.Trim().Length > 0 )
{
sp1.Children.Add(tbox);
}
sp.Children.Add(sp1);
stp.Children.Add(sp2);
}
dockQuestion.Children.Add(sp);
dockAnswer.Children.Add(stp);
}
private void tb1_Click(object sender, RoutedEventArgs e)
{
Label lbl = (Label)sender;
DataObject dataObj = new DataObject(lbl.Content);
DragDrop.DoDragDrop(lbl, dataObj, DragDropEffects.All);
lbl.IsEnabled = false;
lbl.Foreground = (SolidColorBrush)new BrushConverter().ConvertFromString("#FFFB3B46"); // Red
}
You can follow the strategy outlined in the link below, which essentially creates a new window and causes the window position to be updated with the mouse cursor.
http://blogs.msdn.com/b/jaimer/archive/2007/07/12/drag-drop-in-wpf-explained-end-to-end.aspx
So the main points from the page are that you decorate the cursor using the Adorner.
You can use the this.DragSource.GiveFeedback and other events on the DragSource event handlers to set up the Adorner.
Once you have the event handler, that gives you the opportunity to do something.
//Here we create our adorner..
_adorner = new DragAdorner(DragScope, (UIElement)this.dragElement, true, 0.5);
_layer = AdornerLayer.GetAdornerLayer(DragScope as Visual);
_layer.Add(_adorner);
So you can create your own Adorner by subclassing it. You can find more info on creating a custom adorner here:
http://msdn.microsoft.com/en-us/library/ms743737.aspx
take a look at this
http://blogs.msdn.com/b/adamroot/archive/2008/02/19/shell-style-drag-and-drop-in-net-wpf-and-winforms.aspx
the default wpf drag & drop's animation is ugly, if you want be show some text or image while dragging,you need
do something more.
I'm trying to create components on the fly, so, I know how to make this, but, how can I access this component on the fly?
For example:
public Form1
{
Label label1 = new Label();
label1.AutoSize = true;
label1.Location = new System.Drawing.Point(e.X, e.Y);
label1.Name = string.Format("label{0}", labelsCount.ToString());
label1.Size = new System.Drawing.Size(35, 13);
label1.TabIndex = 2;
label1.Text = string.Format("Label -> {0}", labelsCount.ToString());
label1.Click += new System.EventHandler(this.label1_Click);
this.Controls.Add(label1);
label1.BringToFront();
label1.Show();
labelsCount++;
}
When I click on the label, I want to get the label's information (like position, text and name)
How I can do this? Or, what is the best way to do this?
And, to access the component based on position of the panel, inside of form, how I can do this?
Sender of event is your lablel. Simply cast sender object to Label type:
void label1_Click(object sender, EventArgs e)
{
Label label = (Label)sender;
// use
// label.Name
// label.Location
}
I am creating a winform based Desktop application and I am using Datagridview to populate the data.
I am using checkbox in one of the header column. The Table is quiet big to fit into the screen and am using scroll bar to move in the horizontal and vertical direction.
The checkbox need to move as the scroll bar moves. However, the problem is it remains static.
Any idea how to anchor it, so that when the scroll bar moves, the checkbox moves accordingly.
Thanks
EDIT :
Auto generated Designer Code :
this.checkheader.AutoSize = true;
this.checkheader.BackColor = System.Drawing.Color.White;
this.checkheader.FlatAppearance.BorderColor = System.Drawing.Color.White;
this.checkheader.Location = new System.Drawing.Point(49, 96);
this.checkheader.Name = "checkheader";
this.checkheader.Size = new System.Drawing.Size(15, 14);
this.checkheader.TabIndex = 21;
this.checkheader.UseVisualStyleBackColor = false;
this.checkheader.CheckedChanged += new System.EventHandler(this.checkboxHeader_CheckedChanged);
//
You could use the datagridview 'scroll'-event to do something like this:
private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
{
if (e.ScrollOrientation.Equals(ScrollOrientation.HorizontalScroll))
{
checkBox1.Location = new Point(checkBox1.Location.X - (e.NewValue - e.OldValue), checkBox1.Location.Y);
}
if (checkBox1.Location.X < dataGridView1.Location.X + 40)
{
checkBox1.Visible = false;
}
else
{
checkBox1.Visible = true;
}
}
Better late than never ?
I have the following code where a click event will dynamically create additional Canvas to the WrapPanel, and each Canvas contains a TextBox and a Button. Once the Button on one Canvas is click, TextBox.Text and Button.Content change from "Foo" to "Jesus".
The below code works, but it's not ideal. Because each property Change ("Foo" to "Jesus), I have to run a loop. I have to run two loops just to change the text on the TextBox and Button. Is there a direct way to change the Properties other then a Loop? My actually application contains 30+ controls in a Canvas, I don't want to run 30+ loops each time just to change some text.
List<Canvas> cvList = new List<Canvas>();
List<TextBox> tbList = new List<TextBox>();
List<Button> FooList = new List<Button>();
WrapPanel wp = new WrapPanel();
private void createbtn1_Click(object sender, RoutedEventArgs e)
{
Canvas cv = new Canvas();
StackPanel sp = new StackPanel();
TextBox tb = new TextBox();
Button Foo = new Button();
sp.Orientation = Orientation.Vertical;
sp.Children.Add(tb);
sp.Children.Add(Foo);
cv.Children.Add(sp);
wp.Children.Add(cv);
cvList.Add(cv);
tbList.Add(tb);
FooList.Add(Foo);
cv.Width = 100;
cv.Height = 100;
tb.Text = "#" + (cvList.IndexOf(cv)+1);
tb.Width = 50;
tb.Height = 30;
Foo.Content = "Foo";
Foo.Click += destroy_Click;
}
private void Foo_Click(object sender, RoutedEventArgs e)
{
Button b = sender as Button;
var bIndex = FooList.IndexOf(b);
foreach (TextBox t in tbList)
{
if (tbList.IndexOf(t) == bIndex)
{
t.Text = "Jesus";
}
}
foreach (Button f in FooList)
{
if (FooList.IndexOf(t) == bIndex)
{
t.Content = "Jesus";
}
}
}
Just access the text boxes by index and set the content of the button directly:
if(bIndex < tbList.Count && bIndex != -1)
tbList[bIndex].Text = "Jesus";
if(b != null && bIndex != -1)
b.Content = "Jesus";
why can't you just get the item at the index and set that items text:
tbList[bindex].Text="Jesus";
As for setting the buttons content, you already have the button from the click event, so just use that:
b.Content = "Jesus";
You current code just loops through each item in the list and gets the index of the item and sees if it is the index you want. Accessing by the indexer of the list directly will give you what you want.
You will probably want to do some error checking, but that is not currently done in your existing code either.
Some info on using indexers from MSDN