Drag and Drop Buttons - How to get access the dropped button? - c#

I want when I move button2 into button1 (img2) below code will done.
My code:
private void ButtonDragDrop(object sender, DragEventArgs e)
{
var btn = (Button)e.Data.GetData(typeof(Button)); //button2
(sender as Button).Text = "test1"; //button1
btn.Text = "Test2"; //button2
}
How Can I make when button1 touch button2 code will done?

Related

Place selected item from drop down list in to a text box

I want to get a selected item from a drop down list, on button click need to show that selected item into a text box.
I haven't tried anything cause i don't know what to do.
Just use this code...
Suppose, Button click event:
protected void Button1_Click(object sender, EventArgs e)
{
Textbox1.Text = DropDownList1.SelectedItem.Text.ToString();
}
The following class can give you the general idea of doing what you want.
public class MyClass
{
public MyClass()
{
comboBox = new ComboBox();
button = new Button();
textBox = new TextBox();
button.Click += OnButtonClick;
}
private void OnButtonClick(Object sender, EventArgs e)
{
textBox.Text = comboBox.SelectedItem.ToString();
}
ComboBox comboBox;
Button button;
TextBox textBox;
}

Change dynamic buttons text with textbox

Let me tell you what I want to do.
1.there are two button in my form.One of them let button when I click it.Another button finish to create button.
bool active = false;
private void button1_Click(object sender, EventArgs e)
{
active = true;
}
private void button2_Click(object sender, EventArgs e)
{
active = false;
}
2.I will create button with mousedown event I want to set location of this buttons with coordinate like this.
Button button_create;
private void frm_tr_MouseDown(object sender, MouseEventArgs e)
{
if (active)
{
button_create = new Button();
button_create.Location = new Point(e.X + 5, e.Y - 15);
button_create.Size = new Size(75, 30);
button_create.Text = "Button";
this.Controls.Add(button_create);
button_create.MouseClick += new MouseEventHandler(button_create_MouseClick);
}
}
3.I started mouseclickevent of my created button.When I click this created button I will create new form textbox and button.
TextBox button_text;
void button_create_MouseClick(object sender, MouseEventArgs e)
{
Form frm = new Form();
frm.Show();
button_text = new TextBox();
Button accept = new Button();
accept.Location = new Point(frm.Width / 2, frm.Height / 2);
frm.Controls.Add(button_text);
frm.Controls.Add(accept);
accept.MouseClick += new MouseEventHandler(accept_MouseClick);
}
4.I started mouseclickevent of my last created button.I want to change text of my first created button.
void accept_MouseClick(object sender, MouseEventArgs e)
{
button_create.Text = button_text.Text;
}
5.I will click button1 and I click anywhere of this form new button will create and I also click button2 to finish create new button.I can change my created button with textbox but
If I click button1 and I create a new one button I cant change text of previous created button How can I do that?

how to execute click event on dynamically created button in c#.net

I am trying to build an app, where user can select category and according to it displays its sub categories , these sub categories are buttons, which are dynamically created.
Now, as buttons are dynamically created so I am confuse how to write code under button_click event as I dont know how many subcategories are there.
So is there any way I can execute click event of a particular button , so that I can execute certain commands?
EDITED
This is the code that i tried
Button btnDynamicButton = new Button();
private void btnclick_Click(object sender, EventArgs e)
{
label2.Text = btnDynamicButton.Text;
}
private void btnappetizer_Click(object sender, EventArgs e)
{
groupBox2.Visible =false;
DataTable dt = new DataTable();
dt = itemmasterbl.SelectallrecordFromtblItem(btnappetizer.Text);
for (int i = 0; i < dt.Rows.Count; i++)
{
string name = "Appetizer" + DynamicButtonCount;
Button btnDynamicButton1 = new Button();
btnDynamicButton1.Name = name;
btnDynamicButton1.Text = name;
btnDynamicButton1.Size =
new System.Drawing.Size(150, 30);
btnDynamicButton1.Location =
new System.Drawing.Point(180, DynamicButtonCount * 30);
btnDynamicButton1.Click +=new EventHandler(btnclick_Click);<br>
Controls.Add(btnDynamicButton1);
DynamicButtonCount++;
btnDynamicButton = btnDynamicButton1;
}
}
Once I do this it creates three buttons according to number of values in itemmaster DB under appetizer, but once I click on any of the three buttons the label displays only last buttons text,because in last line I have :
btnDynamicButton = btnDynamicButton1;
Which will last buttons infos,but rather I want which ever button I press, label should display respective text. How can I achieve this.
you can put all your logic into one handler:
System.Windows.Forms.Button b = new System.Windows.Forms.Button();
b.Click += new EventHandler(b_Click);
//finally insert the button where it needs to be inserted.
...
void b_Click(object sender, EventArgs e)
{
MessageBox.Show(((System.Windows.Forms.Button)sender).Name + " clicked");
}
To your edit:
You are storing the reference for your button(s) inside the Field btnDynamicButton. Hence it always gets overwritten with the latest button you have created. You should not reference the button by using a field. The sender parameter of the click-handler contains the button element that has been clicked. See the code above: Simple cast sender to Button and you know which button has been clicked:
private void btnclick_Click(object sender, EventArgs e)
{
Button btn = (Button)sender
label2.Text = btn.Text;
}

i have added update button inside datagridview how to change text on it when button is clicked in c#?

I am developing a windows application in c#. I have created a button inside datagridview using following code
DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
dataGridViewTrial.Columns.Add(btn);
btn.HeaderText = "Update";
btn.Name = "btn";
btn.Text = "Update";
btn.UseColumnTextForButtonValue = true;
now i want to change the text of btn to "save" when the update button get clicked.Also i want to update my table.I m not getting it.Please help me:(
If im not mistake you should do this
//Here you add event to button
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is Button)
{
Button btn = e.Control as Button;
btn.Click -= new EventHandler(btn_Click);
btn.Click += new EventHandler(btn_Click);
}
}
void btn_Click(object sender, EventArgs e)
{
if(sender is button)
((button)sender).Text = "new text";
}
I hope this help
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is Button)
{
Button btn = e.Control as Button;
// hook or unhook click event here
}
}

Escape button to cancel action

Hi currently i have 2 buttons, Update and Modify. Update button is set to be hidden initially.
When i click Modify button, Modify button hides, Update button appears, Textbox becomes non read only. Then Clicking update button will hide update button and modify buttons appear and textbox will be hidden and label will appear.
How can i change the code so that:
When i first click modify button, and i can get to update the textbox values and in this state if i press "ESC" button, i will hide "update" button and textbox will be read only?
The following is the current code:
private void ProjectnumberupdateButton_Click(object sender, RoutedEventArgs e)
{
ProjectnumberresultLabel.Content = ProjectnumberTextBox.Text;
ProjectnumberupdateButton.Visibility = Visibility.Hidden;
ProjectnumberTextBox.Visibility = Visibility.Hidden;
ProjectnumbermodifyButton.Visibility = Visibility.Visible;
PreviousbuildversionresultLabel.Content = "" + MajorversionresultLabel.Content + "." + MinorversionresultLabel.Content + "." + ProjectnumberresultLabel.Content + "." + BuildnumberresultLabel.Content;
}
private void ProjectnumbermodifyButton_Click(object sender, RoutedEventArgs e)
{
ProjectnumberupdateButton.Visibility = Visibility.Visible;
ProjectnumberTextBox.Visibility = Visibility.Visible;
ProjectnumbermodifyButton.Visibility = Visibility.Hidden;
}
EDIT:
This is what i have done so far:
private void MajorversionTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
MajorversionupdateButton.Visibility = Visibility.Hidden;
MajorversionTextBox.Visibility = Visibility.Hidden;
MajorversionmodifyButton.Visibility = Visibility.Visible;
}
}
private void MajorversionmodifyButton_Click(object sender, RoutedEventArgs e)
{
MajorversionupdateButton.Visibility = Visibility.Visible;
MajorversionTextBox.Visibility = Visibility.Visible;
MajorversionmodifyButton.Visibility = Visibility.Hidden;
Keyboard.Focus(MajorversionTextBox);
MajorversionTextBox_KeyDown(); // this is the line. i have trouble hooking this up
}
sorry, i changed the project number to majorversion.
You can write OnKeyPress Event for the window and trace the ESC button click.
Inside that you can write the logic to toggle the visibility of the controls.
You can hook / handle the KeyDown event, check if the key pressed was the Escape button and make your changes to you buttons and text boxes in the code from there.
You can set focus on text box when modify button clicked and then use KeyDown event on text box:
private void ProjectnumberTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
ProjectnumberTextBox.ReadOnly = true;
ProjectnumbermodifyButton.Visibility = Visibility.Hidden;
}
}

Categories

Resources