Basically i am doing a drag and drop using textbox and labels , by dragging a label to a textbox . Textboxes and Labels are created in same for loop .
I have dynamically created textboxes ( textbox is the drop target) like this :
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.Drop += 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());
}
I have also dynanmically create labels ( label is the drag target) like this :
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);
// lbl.MouseUp +=new MouseButtonEventHandler(lbl_MouseUp);
dockPanel1.Children.Add(lbl);
}
I have 2 issues here .
1st . I am using tbox.drop event to display MessageBox.Show( something ) ; to display a messagebox when the drag target is being drop but it doesn't work .
here is my code :
private void tbox_Drop(object sender, DragEventArgs e)
{
MessageBox.Show("Are you sure?");
}
Second , i also want to clear the tbox.Text when the drag target is dropped because i might have other drag target dropped into the tbox previously. So i want to clear the tbox.Text and drop the drag target everytime when i drag the target to textbox .
How do i do that? i am stucked at which event i should use for this and how do i access tbox from those event handlers ?
It worked for me.
private void lbl_MouseDown(object sender, MouseButtonEventArgs e)
{
Label _lbl = sender as Label;
DragDrop.DoDragDrop(_lbl, _lbl.Content, DragDropEffects.Move);
}
You do not need MouseMove and DragEnter events for Label if you are using them just for Drag purpose.
Replace Drop event with PreviewDrop for TextBox as shown below:
tbox.Drop += new DragEventHandler(tbox_Drop);
with this
tbox.PreviewDrop += new DragEventHandler(tbox_PreviewDrop);
private void tbox_PreviewDrop(object sender, DragEventArgs e)
{
(sender as TextBox).Text = string.Empty;
}
The TextBox you want to drag(add mousedown event)
private void dragMe_MouseDown(object sender, MouseButtonEventArgs e)
{
TextBox tb = sender as TextBox;
// here we have pass the textbox object so that we can use its all property on necessary
DragDrop.DoDragDrop(tb, tb, DragDropEffects.Move);
}
The TextBox on which you want to drop(add drop event and also you have to check the marked as allowdrop checkbox)
private void dropOnMe_Drop(object sender, DragEventArgs e)
{
TextBox tb= e.Data.GetData(typeof(TextBox)) as TextBox;
// we have shown the content of the drop textbox(you can have any property on necessity)
dropOnMe.Content = tb.Content;
}
Related
I want to add a event for label when a specific label is pressed, but my event handler is not reacting to the clicked label.
I created a calender so when I click a date I want to highlight that date, that's the requirement.
for (Int32 i = 1; i <= Dayz; i++)
{
ndayz += 1;
lblDayz = new Label();
lblDayz.Text = i.ToString();
lblDayz.Cursor = Cursors.Hand;
lblDayz.Name = "Date" + i;
lblDayz.Anchor = AnchorStyles.None;
lblDayz.TextAlign = ContentAlignment.MiddleCenter;
lblDayz.Click += lblDayz_Click;
}
Event handler looks like:
public void lblDayz_Click(object sender, EventArgs e)
{
lblDayz.BackColor = Color.FromArgb(176, 180, 43);
lblDayz.ForeColor = Color.White;
}
Your current implementation is trying to change the properties of lblDayz, which might be a single label somewhere. But the way you're creating labels you have a number of labels generated in code. One for each day
That means you need your handler to react to the label that was clicked. The label that was clicked is the sender in your event handler. Crudely then you could handle it like this
public void lblDayz_Click(object sender, EventArgs e)
{
Label clickedLabel = sender as Label;
clickedLabel.BackColor = Color.FromArgb(176, 180, 43);
clickedLabel.ForeColor = Color.White;
}
This question already has an answer here:
How can a hover effect be created for a grouping of controls?
(1 answer)
Closed 4 years ago.
I have a few objects in a groupBox in Visual Studio such as a few buttons. I have a label that pops up when I enter the groupBox and disappears when I leave it. However, when I hover over a button in this groupBox, the label disappears as the MouseLeave event only corresponds to the groupBox.
Is there anyway to have all of these objects grouped together so when I hover on anything in the groupBox the label stays and when I leave the groupBox altogether the label disappears? I just want an elegant way to do this.
Thanks so much for your help!
This is different than How can a hover effect be created for a grouping of controls? because I'd like a different result than what this person wants and also I tried something similar and it didn't work.
Unfortunately you can not add objects by groupbox but you can add them as a list of the control type of itself.
here you go.
private void Form1_Load(object sender, EventArgs e)
{
TextBox[] textboxes = new TextBox[] { textBox1 , textBox2, textBox3 };
Button[] buttons = new Button[] { button1 };
foreach (TextBox tbox in textboxes)
{
tbox.MouseEnter += new System.EventHandler(textbox_MouseEnter);
tbox.MouseLeave += new System.EventHandler(textbox_MouseLeave);
}
foreach (var button in buttons)
{
button.MouseEnter += new System.EventHandler(btn_MouseEnter);
button.MouseLeave += new System.EventHandler(btn_MouseLeave);
}
}
private void btn_MouseEnter(object sender, System.EventArgs e)
{
var Button = (Button)sender;
button1.BackColor = Color.Red;
//label show
}
private void btn_MouseLeave(object sender, System.EventArgs e)
{
var Button = (Button)sender;
button1.BackColor = SystemColors.Control;
//label hide
}
private void textbox_MouseEnter(object sender, System.EventArgs e)
{
var textbox = (TextBox)sender;
textbox.BackColor = Color.Red;
//label show
}
private void textbox_MouseLeave(object sender, System.EventArgs e)
{
var textbox = (TextBox)sender;
textbox.BackColor = SystemColors.Control;
//label hide
}
GIF IMAGE OF WORKING : GIF
You could do something like this,
//Add new event handlers on appropriate location in your code (perhaps in the load event of the form?)
groupBox1.MouseEnter += new EventHandler(MouseEnteredGroupBox);
groupBox1.MouseLeave += new EventHandler(MouseLeftGroupBox);
In the MouseEnter event handler you change the state of your controls which you want to show.
private void MouseEnteredGroupBox(object sender, EventArgs e)
{
//Add controls to be shown when entering the groupbox
button1.Visible = true;
}
Add this method to check if your cursor is inside of your groupbox area
private bool IsAboveGroupBox(GroupBox gb)
{
Point cursorPos = PointToClient(Cursor.Position);
bool resultX = (cursorPos.X > gb.Location.X && cursorPos.X < gb.Location.X + gb.Size.Width) ? true : false;
bool resultY = (cursorPos.Y > gb.Location.Y && cursorPos.Y < gb.Location.Y + gb.Size.Height) ? true : false;
return resultX && resultY;
}
Finally in the MouseLeave event handler you call the method
private void MouseLeftGroupBox(object sender, EventArgs e)
{
if (!IsAboveGroupBox(sender as GroupBox))
{
//Add controls to be hidden when leaving the groupbox
button1.Visible = false;
}
}
EDIT: Else you could just view your controls on the MouseEnter event on groupbox and hide your controls on the Forms MouseEnter event?
I have created a dynamic label on button click on Windows Form. And then on a right click on the label. I'm showing a context menu "cm". I obviously want to add functionality to the context menu items. But what I don't understand is how do I reference the "lbl" object inside the event handler? How can I edit the properties of the labels from inside the event handlers named MarkedImportant and EditLabel?
public void btnMonSub_Click(object sender, EventArgs e)
{
string s = txtMonSub.Text;
Label lbl = new Label();
lbl.Text = s;
lbl.Location = new System.Drawing.Point(205 + (100 * CMonSub), 111);
CMonSub++;
lbl.Size = new System.Drawing.Size(100, 25);
lbl.BackColor = System.Drawing.Color.AliceBlue;
this.Controls.Add(lbl);
ContextMenu cm = new ContextMenu();
cm.MenuItems.Add("Mark Important", MarkImportant);
cm.MenuItems.Add("Edit", EditLabel );
lbl.ContextMenu = cm;
}
private void MarkImportant(object sender, EventArgs e)
{
// imp..
}
private void EditLabel(object sender, EventArgs e)
{
// edit..
}
Or is there a better way to do this? Like dynamically adding the event handler itself?
Thanks in advance.
The ContextMenu has a property called SourceControl and MSDN says it
Gets the control that is displaying the shortcut menu.
So your event handler could reach the ContextMenu from the MenuItem passed as the sender parameter in this way
private void MarkImportant(object sender, EventArgs e)
{
// Convert the sender object to a MenuItem
MenuItem mi = sender as MenuItem;
if(mi != null)
{
// Get the parent of the MenuItem (the ContextMenu)
// and read the SourceControl as a label
Label lbl = (mi.Parent as ContextMenu).SourceControl as Label;
if(lbl != null)
{
....
}
}
}
i have an application Windows Form where i created dynamic controls and i need program events like Keypress but i can't make it 'cause they don't know each other once they just exists in runtime. (i' Sorry: English by google Tradutor)
The problem appears to be that you are creating your dynamic variables locally:
ComboBox c1 = new Combobox();
TextBox t1 = new TextBox();
Changing jacqijvv's answer a bit to look more like what I believe you are trying to achieve. I'm also going to assume that you have multiple textbox/combobox pairs that are related to each other so you could store them in a dictionary object to retrieve them later in the event handler:
public partial class Form1 : Form
{
public Dictionary<TextBox, ComboBox> _relatedComboBoxes;
public Dictionary<ComboBox, TextBox> _relatedTextBoxes;
public Form1()
{
InitializeComponent();
_relatedComboBoxes = new Dictionary<TextBox, ComboBox>();
_relatedTextBoxes = new Dictionary<ComboBox, TextBox>();
TextBox textBox = new TextBox();
textBox.Text = "Button1";
textBox.KeyDown += textBox_KeyDown;
ComboBox comboBox = new ComboBox();
// todo: initialize combobox....
comboBox.SelectedIndexChanged += comboBox_SelectedIndexChanged;
// add our pair of controls to the Dictionaries so that we can later
// retrieve them together in the event handlers
_relatedComboBoxes.Add(textBox, comboBox);
_relatedTextBoxes.Add(comboBox, textBox);
// add to window
this.Controls.Add(comboBox);
this.Controls.Add(textBox);
}
void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = sender as ComboBox;
TextBox textBox = _relatedTextBoxes[comboBox];
// todo: do work
}
void textBox_KeyDown(object sender, KeyEventArgs e)
{
TextBox textBox = sender as TextBox;
// find the related combobox
ComboBox comboBox = _relatedComboBoxes[textBox];
// todo: do your work
}
}
You can try the following code:
public partial class Form1 : Form
{
// Create an instance of the button
TextBox test = new TextBox();
public Form1()
{
InitializeComponent();
// Set button values
test.Text = "Button";
// Add the event handler
test.KeyPress += new KeyPressEventHandler(this.KeyPressEvent);
// Add the textbox to the form
this.Controls.Add(test);
}
// Keypress event
private void KeyPressEvent(object sender, KeyPressEventArgs e)
{
MessageBox.Show(test.Text);
}
}
This should work.
At my program i dynamicly add Buttons to my form
{
...
Button bt = new Button();
bt.Text = "bla bla";
bt.MouseClick += new MouseEventHandler(bt_MouseClick);
myPanel.Controls.Add(bt);
...
}
void bt_MouseClick(object sender, MouseEventArgs e)
{
TabPage _tab = new TabPage();
_tab.Text = ??? // I want to get the Button's text ! this.Text returns me the
//main form.Text
}
How can access my dynamic Buttons properties ? How can I understand whick button is
clicked either getting its text.
Thanks.
void bt_MouseClick(object sender, MouseEventArgs e)
{
TabPage _tab = new TabPage();
_tab.Text = ((Button)sender).Text;
}
When an EventHandler delegate is invoked, the sender parameter is the component that raised the event, and the e parameter is a subclass of EventArgs that provides any additional component/event specific information for the event.
Therefore you can establish which button the event fired on by casting the sender parameter to Button:
void bt_MouseClick(object sender, MouseEventArgs e)
{
var button = (Button)sender;
TabPage _tab = new TabPage();
_tab.Text = button.Text;
}