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.
Related
I have the problem that the text inside my panel gets cut of strangely. The panel is located inside a textbox. But even if I replace the textbox by a flowlayoutpanel, I have the same issue.
Code:
List<string> list = datenbank.FerienAuswahl(monat, jahr);
int i = 0;
//Create Panel
try
{
//Fill Panel
do
{
Label panel = new Label();
panel.Name = "panel" + i;
panel.Height = 30;
panel.Width = 400;
panel.AutoSize = false;
panel.TextAlign = ContentAlignment.MiddleCenter;
panel.ForeColor = Color.Black;
panel.Text = list[i];
Label ferien = new Label();
panel.Controls.Add(ferien);
tbFerien.Controls.Add(panel);
i++;
} while (i < list.Count);
}
catch { }
Result:
I have already tried to change the width of the panel. But as result I only get a messed up alignment of the text.
The only settings of the textbox I have changed are these:
Multiline: True
TextAlign: Center
Size: 359; 125
Does Someone know what else I could try ?
These lines worry me:
Label panel = new Label();
Label ferien = new Label();
panel.Controls.Add(ferien);
tbFerien.Controls.Add(panel);
It seems to me you are adding one label to another. That's not good. Use a Panel or TableLayoutPanel instead of the actual panel and make sure you have your positioning good.
I got a form. In this form there is a tab control and there is a menu strip. I would like the tab control to take up the space of the entire form; however, I do not want the menu strip to cover the top of it.
In order to combat this problem. I made a control and I made the panel the same size as the menu strip. I put added the panel control to the menu strip first and then I added the tab control; however, I did not get the desired result. Can someone show me how to get the result I desire?
This is what it looks like without the additional panel.
Here us the code :
public Main()
{
InitializeComponent();
//Panel placeholder = new Panel()
//{
// MaximumSize = menuStrip1.MaximumSize,
// MinimumSize = menuStrip1.MaximumSize,
// Size = menuStrip1.Size,
// Padding = menuStrip1.Padding,
// Visible = true,
//};
//placeholder.Dock = DockStyle.Top;
//Controls.Add(placeholder);
Controls.Add(InitNavigation());
}
TabControl InitNavigation()
{
//Declare All Variables
TabControl control = new TabControl();
TabPage queryPage = new TabPage();
TabPage tablePage = new TabPage();
control.TabPages.Add(queryPage);
control.TabPages.Add(tablePage);
//Customize Table Control
control.Top = menuStrip1.Size.Height;
control.Dock = DockStyle.Fill;
Padding Margin = control.Margin;
MessageBox.Show("" + menuStrip1.Size.Height);
//control.Margin = new Padding(Margin.Left,, Margin.Right, Margin.Bottom);
//Customize Query Tab
queryPage.Text = "Queries";
//Customize Table Page
tablePage.Text = "Tables";
control.Visible = true;
return control;
}
This is what it looks like with the panel
Here is the code:
public Main()
{
InitializeComponent();
Panel placeholder = new Panel()
{
MaximumSize = menuStrip1.MaximumSize,
MinimumSize = menuStrip1.MaximumSize,
Size = menuStrip1.Size,
Padding = menuStrip1.Padding,
Visible = true,
};
placeholder.Dock = DockStyle.Top;
Controls.Add(placeholder);
Controls.Add(InitNavigation());
}
TabControl InitNavigation()
{
//Declare All Variables
TabControl control = new TabControl();
TabPage queryPage = new TabPage();
TabPage tablePage = new TabPage();
control.TabPages.Add(queryPage);
control.TabPages.Add(tablePage);
//Customize Table Control
control.Top = menuStrip1.Size.Height;
control.Dock = DockStyle.Fill;
Padding Margin = control.Margin;
MessageBox.Show("" + menuStrip1.Size.Height);
//control.Margin = new Padding(Margin.Left,, Margin.Right, Margin.Bottom);
//Customize Query Tab
queryPage.Text = "Queries";
//Customize Table Page
tablePage.Text = "Tables";
control.Visible = true;
return control;
}
finally. This last picture just shows that the tabs are in fact there (its the same code as the first code posted except the menu strip visibility is set to false).
Any insight on how to fix this problem would be appreciated
If the fully docked control is underneath a top docked control, try bringing the fully docked control to the front (right click on it in the designer and select bring to front) this should fix it.
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
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'm making a C# project with WPF and Visual Studio.
I'm stuck at trying to drag/move a TextBox around on my canvas in run-time.
I got the idea that, if you make a border thickness big enough, you could use the border for some sort of focus and then make an EventHandler for the border?
private Canvas DrawBox(ClassBox box)
{
Canvas myCV = new Canvas();
TextBox box1 = new TextBox();
box1.Background = new SolidColorBrush(Colors.Blue);
box1.BorderThickness = new System.Windows.Thickness(5);
box1.Foreground = new SolidColorBrush(Colors.White);
box1.MinWidth = 30;
box1.TextWrapping = TextWrapping.Wrap;
box1.AcceptsReturn = true;
myCV.Children.Add(box1);
Canvas.SetLeft(box1, box.Left);
Canvas.SetRight(box1, box.Right);
Canvas.SetTop(box1, box.Top);
return myCV;
}