I need to get the sender of the mouseDown event from within the event and set it as a global variable to use in a dragDrop event, so that it calls a method depending on what picturebox was dragged. I need the control name or something. My attempt:
Global variable "dragSource":
public partial class MapDesignerView : Form
{
public Map myMap { get; set; }
public MapController myMapController { get; set; }
public MapConstructor myMapConstructor { get; set; }
public MouseEventHandler myDetectMouse { get; set; }
object dragSource = null;
mouseDown
private void pbxMinotaur_MouseDown(object sender, MouseEventArgs e)
{
pbxMap.AllowDrop = true;
pbxMinotaur.DoDragDrop(pbxMinotaur.Name, DragDropEffects.Copy |
DragDropEffects.Move);
dragSource = sender;
}
DragDrop
private void pbxMap_DragDrop(object sender, DragEventArgs e)
{
{
if (dragSource == pbxMinotaur)
{
myDetectMouse.setMinotaur(e, myMap.myCells);
}
So what exactly isn't working... the only thing I can think that might be causing the problem is that you're storing the reference to the entire control in your drag source.
A better idea might be to just story the Id. and then test it based on the Id further down.
string dragSourceName = null;
private void pbxMinotaur_MouseDown(object sender, MouseEventArgs e)
{
pbxMap.AllowDrop = true;
pbxMinotaur.DoDragDrop(pbxMinotaur.Name, DragDropEffects.Copy |
DragDropEffects.Move);
Control c = (sender as Control);
if(c != null)
dragSourceName = c.Name;
}
private void pbxMap_DragDrop(object sender, DragEventArgs e)
{
if (dragSourceName == pbxMinotaur.Name)
{
myDetectMouse.setMinotaur(e, myMap.myCells);
}
Related
public partial class Form2 : Form
{
public bool male {get; set;}
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
maleGender_rb1 = male;
}
}
I want to display the radio buttton result from form1 to form2 using also radio buttons
The best solution is to setup an event in the child form and for the RadioButton controls subscribe to CheckedChanged. The CheckedChange event determines which RadioButton was checked and sends notification to any listeners.
Child form:
namespace PassStringFromChildToParentForm
{
public partial class ChildForm : Form
{
public delegate void OnPassData(bool isMale);
public event OnPassData PassData;
public ChildForm()
{
InitializeComponent();
MaleRadioButton.Checked = true;
MaleRadioButton.CheckedChanged += RadioButton_CheckedChanged;
FemaleRadioButton.CheckedChanged += RadioButton_CheckedChanged;
}
private void RadioButton_CheckedChanged(object sender, EventArgs e)
{
var radioButton = (RadioButton)sender;
if (radioButton.Checked)
{
PassData?.Invoke(radioButton == MaleRadioButton);
}
}
public void UpdateRadioButton(bool isMale)
{
if (isMale)
{
MaleRadioButton.Checked = true;
}
else
{
FemaleRadioButton.Checked = true;
}
}
}
}
Main Form:
Here we subscribe to the child form event and determine which RadioButton to check based on the bool value passed.
namespace PassStringFromChildToParentForm
{
public partial class MainForm : Form
{
private ChildForm _childForm;
public MainForm()
{
InitializeComponent();
MaleRadioButton.CheckedChanged += RadioButton_CheckedChanged;
FemaleRadioButton.CheckedChanged += RadioButton_CheckedChanged;
}
private void RadioButton_CheckedChanged(object sender, EventArgs e)
{
if (Application.OpenForms.OfType<ChildForm>().Count() != 1) return;
var radioButton = (RadioButton)sender;
if (!radioButton.Checked) return;
if (!radioButton.Checked) return;
_childForm.UpdateRadioButton(MaleRadioButton.Checked);
}
private void ShowChildForm_Click(object sender, EventArgs e)
{
_childForm = new ChildForm();
_childForm.PassData += ChildForm_PassData; ;
_childForm.Show(this);
}
private void ChildForm_PassData(bool isMale)
{
if (isMale)
{
MaleRadioButton.Checked = true;
}
else
{
FemaleRadioButton.Checked = true;
}
}
}
}
You can't convert bool to RadioButton. I think you need to use Checked property of radio button to pass boolean value to it.
For example:
maleGender_rb1.Checked = true;// or false
I have a custom control (UserControl) that contains two labels and a button, I load this control dynamically in a form. When I click on the custom control button, I would like to get the information contained for example in the label1 of the custom control.
private void mycustomcontrol_MouseClick(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
mycustomcontrol = sender as mycustomcontrol;
MessageBox.Show(mycustomcontrol.Info); // <-- mycustomcontrol.Info refers to the label1 text on the custom control
}
}
In my Usecontrol:
public new event MouseEventHandler MouseClick
{
add
{
button1.MouseClick += value;
}
remove
{
button1.MouseClick -= value;
}
}
I get an exception:
System.NullReferenceException: 'Reference to an object not set to an object instance.'
unorthodox solution, but in my case it's fine:
In my UserControl:
public static string _value{ get; set; }
private void Button1_MouseClick(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
_value = lbl_info.Text;
}
}
In my Form:
private void mycustomcontrol_MouseClick(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
MessageBox.Show(mycustomcontrol._value);
}
}
Is there any way to store the code below in another class called RecordAddControl?
Code from "RecordAdd.cs"
private void txtEilesNum_Enter(object sender, EventArgs e)
{
txtEilesNum.Clear();
txtEilesNum.ForeColor = SystemColors.Desktop;
}
private void txtEilesNum_Leave(object sender, EventArgs e)
{
if (txtEilesNum.Text == "")
{
txtEilesNum.ForeColor = SystemColors.InactiveCaption;
txtEilesNum.Text = "Eil Num";
}
}
Things I've tried like RecordAdd recordAdd = new RecordAdd(); doesn't seem to work when trying to get the class to recognise things like txtEilesNum.
RecordAdd is a form, and txtEilesNum is taken from "RecordAdd.Designer.cs"
it makes more sense to tailor helper class to work with TextBox directly (any TextBox, not only the one in RecordAdd)
public static class TexBoxDecorator
{
public static void UsePlaceholder(this TextBox tb)
{
tb.Enter += tb_Enter;
tb.Leave += tb_Leave;
}
private static void tb_Enter(object sender, EventArgs e)
{
var tb = (TextBox)sender;
tb.Clear();
tb.ForeColor = SystemColors.Desktop;
}
private static void tb_Leave(object sender, EventArgs e)
{
var tb = (TextBox)sender;
if (tb.Text == "")
{
tb.ForeColor = SystemColors.InactiveCaption;
tb.Text = "Eil Num";
}
}
}
txtEilesNum in a known member in RecordAdd, so it can be accessed to add event handlers:
txtEilesNum.UsePlaceholder();
Sure, you can store event handlers in another class, you'll need to make them public, and you'll need to cast the sender to the correct type:
public static class EventHandlers
{
public void EilesNum_Enter(object sender, EventArgs e)
{
var txtEilesNum = (TextBox)sender; // Assumed textbox
txtEilesNum.Clear();
txtEilesNum.ForeColor = SystemColors.Desktop;
}
public void EilesNum_Leave(object sender, EventArgs e)
{
var txtEilesNum = (TextBox)sender; // Also assumed textbox
if(txtEilesNum.Text == "")
{
txtEilesNum.ForeColor = SystemColors.InactiveCaption;
txtEilesNum.Text = "Eil Num";
}
}
}
In your form, you'll still need to wire up the events as before
txtEilesNum.Enter += EventHandlers.EilesNum_Enter;
txtEilesNum.Leave += EventHandlers.EilesNum_Leave;
I'm Working on a game that uses multiple layouts. To make those layouts, I use user controls by bringing them forward and sending them behind. Now I'm in a phase where I have to send all gathered data (hardness, what type etc) to User control where all rest of the work is done. I'm stuck with sending int values to my user control that does all the work. I thought about sending them with eventhandler, but EventHandler didn't seem to recognize my int values either
In short, I want to pass Int values that I have in a form, to User control
public void liitmine1(object sender, EventArgs e)
{
/*event*/
uCraskus1.BringToFront();
int What = 1;
}
public void kumme(object sender, EventArgs e)
{
uCmehanism1.BringToFront();
int Between = 1;
}
public event EventHandler KummeClick; /*Sends info to mechanism*/
private void KummeNupp(object sender, EventArgs e)
{
if(What == 1) /*The name'what' does not exist in current context error*/
if (this.KummeClick != null)
this.KummeClick(this, e);
}
if (this.KummeClick != null) is just to test, don't mind if anything is wrong with that
You can make your own EventArgs type, like this:
public class KummeClickEventArgs : EventArgs
{
public int MyProperty { get; set; }
}
And then use it (assumption that kumme method is one of the subscribers):
public void kumme(object sender, KummeClickEventArgs e)
{
//here is your logic like:
//int test = e.MyProperty;
uCmehanism1.BringToFront();
int Between = 1;
}
public event EventHandler<KummeClickEventArgs> KummeClick; /*Sends info to mechanism*/
private void KummeNupp(object sender, EventArgs e)
{
if(What == 1) /*The name'what' does not exist in current context error*/
if (this.KummeClick != null)
{
var eventArgs = new KummeClickEventArgs
{
MyProperty = 3
};
this.KummeClick(this, eventArgs);
}
}
I use OwnerDrawAll properties for my control:
this.customTreeView.DrawMode =System.Windows.Forms.TreeViewDrawMode.OwnerDrawAll;
this.customTreeView.DrawNode +=
new System.Windows.Forms.DrawTreeNodeEventHandler(customTreeView_DrawNode);
private void customTreeView_DrawNode(object sender, DrawTreeNodeEventArgs e) {
if(!myComboBoxChanged) { // Draw the whole control(tree and info)
drawNode(e);
drawInfo(e);
} else { // Draw only info
drawInfo(e);
}
}
Then I use text changed event:
private void cBox_TextChanged(object sender, EventArgs e)
{
text = cBox.Text; // I need this in drawInfo()
myComboBoxChanged = true;
this.customTreeView.Invalidate(); // It doesn't do what I want
myComboBoxChanged = false;
}
Here Invalidate() method redrawing the whole tree, how can I fix this so only drawInfo() will be called ?
the better way is to create your own TreeView Class and encapsulate all of your methods and properties so :
public class MyTreeView : TreeView
{
public bool TextBoxChanged { get; set; }
public MyTreeView()
{
DrawMode = TreeViewDrawMode.OwnerDrawAll;
DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(customTreeView_DrawNode);
}
protected override void OnInvalidated(InvalidateEventArgs e)
{
//comment the below line to create your own Invalidate
//base.OnInvalidated(e);
}
private void customTreeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
if (!TextBoxChanged)
{ // Draw the whole control(tree and info)
drawNode(e);
drawInfo(e);
}
else
{ // Draw only info
drawInfo(e);
}
}
private void drawNode(DrawTreeNodeEventArgs e)
{
//...........
}
private void drawInfo(DrawTreeNodeEventArgs e)
{
//...........
}
}