i have use until now a methods for click button after i have click another button. (i click button "A" and it actives the button "B")
buttonDeleteFields.PerformClick();
Now, because I innovated design, i have replace this button with a icon (picturebox), but the method PerformClick, with a PictureBox, doesn't work. Can you give me a method that will resolve this problem?
( I click button "A" and it active the picturebox "B")
To fully emulate the Click event (as if the user has clicked the picture box),
Add these to your form class:
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
private int WM_LBUTTONDOWN = 0x0201;
private int WM_LBUTTONUP = 0x0202;
And call like this inside your button click event:
private void button1_Click(object sender, EventArgs e)
{
// OLD CODE: Used to perform click on button 2,
// before it was changed to a picture box
// buttonDeleteFields.PerformClick();
// new code, emulating picture box click:
SendMessage(pictureBoxDeleteFields.Handle, WM_LBUTTONDOWN, 0, 1);
SendMessage(pictureBoxDeleteFields.Handle, WM_LBUTTONUP, 0, 0);
}
You just need to call the method bound to the PictureBox.Click event in method button1_Click.
private void pictureBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("123");
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1_Click(this.pictureBox1, e);
}
Related
Ok, so in my program I tried making buttons and assigning different methods for each button pressed. But I came into a problem where I also want the user to use his keyboard and assign buttons pressed on keyboard into same buttons on screen. Yet firstly, I tried if button is pressed by mouse or keyboard yet the method doesn't allow KeyEvents in 'EventArgs' (which is fine by me), so I created different method and made a boolean variable so that if in that separate method the key is pressed, make that variable true and in that main method if that is true then perform the code, yet the program ignores that keyboard variable and I have no idea why.
Then I tried making a different class as I thought maybe that would help. Now I can call that class and method inside it but not pass a parameter as it says it's a method so it can't do anything else but only be called.
If you're curious, here's the code below...
___
// the button '1' variable
bool pressOne = false;
___
// method for if that button is pressed
private void AnyNumberClick(object sender, EventArgs e)
{
Button btnSender = (Button)sender;
if (btnSender == btn_Num1 || pressOne)
{
// if button is pressed by either, perform code
}
}
___
// method for detecting which key is pressed for certain bool variable into button's method
public void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D1)
{
pressOne = true;
}
else
{
pressOne = false;
}
___
// Call another class inside 'From1_KeyDown' method
Class1 newclass = new Class1();
newclass.buttonused();
NumResult.Text = newclass.buttonused.num();
The one with class I don't know how to start it. I don't even know if new class will help me there or not. I did the research but didn't find the answer. I appreciate any help from this.
Try it this way. I've setup a Dictionary<Keys, Button> to represent the relationship between a Key and a Button. Then I've overridden ProcessCmdKey() to trap key presses. If the key pressed exists in our lookup, then we click it with .PerformClick():
public partial class Form1 : Form
{
private Dictionary<Keys, Button> btnLookups = new Dictionary<Keys, Button>();
public Form1()
{
InitializeComponent();
// make your key -> button assignments in here
btnLookups.Add(Keys.F1, button1);
btnLookups.Add(Keys.F2, button2);
btnLookups.Add(Keys.F3, button3);
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
Button btn;
if (btnLookups.TryGetValue(keyData, out btn))
{
btn.PerformClick();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("button1");
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("button2");
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("button3");
}
}
You need an event handler to tie to your method "AnyNumberClick". This is done in the Designer.cs portion of your form. Create a character array char[] and create a function within a button pressed event method, and then compare the button pressed against the set of characters in your array.
private void txt_box_keypress(object sender, KeyPressEventArgs e)
{
char[] SomeArray = {'a','b','c', etc};
int LengthOfArray = SomeArray.Length;
for (int x = 0; x < LengthOfArray; x++)
{
if (txt_box.Text.Contains(SomeArray[x]))
{
'Your method event here'
}
}
}
Is there any way to open a dateTimePicker using a PictureBox in c# windowsform?
I wanted to minimize the dateTimePicker size so it can only show the drop down arrow and the calendar picture but thats not possible, so now i want to open the calendar using a PictureBox, is that possible?
Thanks
Although it is not possible to programatically show picker in DateTimePicker, you can create your own this way:
[ToolboxItem(true)]
public class CustomDateTimePicker : DateTimePicker
{
private const int WM_KEYDOWN = 0x100;
private const int VK_F4 = 0x73;
private const int F4_KEYDOWN_LPARAM = 0x003e0001;
public void ShowPicker()
{
Focus();
var m = Message.Create(Handle, WM_KEYDOWN, new IntPtr(VK_F4), new IntPtr(F4_KEYDOWN_LPARAM));
WndProc(ref m);
}
}
Than in Click handler of your picture box just call that ShowPicker method.
private void PictureBox1_Click(object sender, EventArgs e)
{
customDateTimePicker1.ShowPicker();
}
I am creating a form which has a Multiline TextBox to enter an URL. Expected URLs will be very long.
User will paste the URL and move to next box.
Right now, TextBox shows ending part of the URL when user moves to next TextBox. I want such that it will show starting of URL (Domain name) instead of trailing part.
Current:
Expected:
And this should happen when user leaves the TextBox.
I tried various methods of Selection in textBox_Leave() event but I guess, these methods won't work if focus is lost.
I am using .Net framework 3.5.
Update: Textbox I am using is Multiline. Answers suggested by #S.Akbari and #Szer are perfect if the Mutliline property is set to False. I realized it late that Multiline will play such a significant role. Hence updating the question!
Use SelectionStart in the Leave event should works:
private void textBox1_Leave(object sender, EventArgs e)
{
textBox1.SelectionStart = 0;
}
Before:
After leaving TextBox:
Tried it and it works. Proof
public Form1()
{
InitializeComponent();
textBox1.LostFocus += TextBox1_LostFocus;
}
private void TextBox1_LostFocus(object sender, EventArgs e)
{
textBox1.SelectionStart = 0;
textBox1.SelectionLength = 0;
}
I can see how it doesn't work with the Multiline property set to true.
A simple API call can make this work:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
private const int WM_VSCROLL = 0x115;
private const int SB_TOP = 6;
void textBox1_Leave(object sender, EventArgs e) {
SendMessage(textBox1.Handle, WM_VSCROLL, (IntPtr)SB_TOP, IntPtr.Zero);
}
Firstly, I apologize if the title does not make much sense, as I did not know the best way to explain it.
Now to really explain it. What I have done is created a control in a Class Library project in Visual Studio 2013. This control is supposed to act as the caption bar for form that is set with the "FormBorderStyle" as "None". This imitation caption bar control is supposed to move the form, just like a normal forms' caption bar would.
I have achieved this, but only in the forms code. This is the code I use:
private int mouseStartX, mouseStartY;
private int formStartX, formStartY;
private bool FormDragging = false;
private void titleBar_MouseDown(object sender, MouseEventArgs e)
{
this.mouseStartX = MousePosition.X;
this.mouseStartY = MousePosition.Y;
this.formStartX = this.Location.X;
this.formStartY = this.Location.Y;
FormDragging = true;
}
private void titleBar_MouseMove(object sender, MouseEventArgs e)
{
if (FormDragging)
{
this.Location = new Point(
this.formStartX + MousePosition.X - this.mouseStartX,
this.formStartY + MousePosition.Y - this.mouseStartY
);
}
}
private void titleBar_MouseUp(object sender, MouseEventArgs e)
{
FormDragging = false;
}
"this.*" is obviously referring to the form, when in the forms code. So of course, if I were to simply put this into the controls code, it'd obviously be referring to the control, and thus the control would be the one moving around on the form.
I've also created a control in the Class Library that acts as a close button. All I had to do was:
Form.ActiveForm.Close();
Same for minimize being:
Form.ActiveForm.WindowState = FormWindowState.Minimized;
And maximize being:
Form.ActiveForm.WindowState = FormWindowState.Maximized;
On the controls' click events.
When I try to replace "this." with "Form.ActiveForm.", in the first code posted - it returns this error:
'System.Windows.Forms.Form' does not contain a definition for 'mouseStarX' and no extension method 'mousStartX' accepting a first argument of type 'System.Windows.Forms.Form' could be found (are you missing a using directive or an assembly reference?)
That's about it, I don't know how else to go about this.
There is a simple pinvoke you can use to move the form via your control.
Adapted from C# - Make a borderless form movable?, instead of using Form.ActiveForm, you would use this.FindForm() to get the parent form of the control. It's used here to pass the form's handle value:
public class MyHeader : Control {
private const int WM_NCLBUTTONDOWN = 0xA1;
private const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int Msg,
int wParam, int lParam);
[DllImportAttribute("user32.dll")]
private static extern bool ReleaseCapture();
protected override void OnMouseDown(MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
ReleaseCapture();
SendMessage(this.FindForm().Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
base.OnMouseDown(e);
}
}
For closing the form, you would use the same method:
this.FindForm().Close();
I have a label working as a button. I would like when I press a button the click event to this label to take action. for example
private void Label1_Click(object sender, EventArgs e)
{
MessageBox.Show("I pressed this label and showed me this messagebox");
}
Now I want when I press this button, the label1 click event to be performed
private void button1_Click(object sender, EventArgs e)
{
// I want when I press this button something like this happens
Label1.PerformClick();
}
private void button1_Click(object sender, EventArgs e)
{
Label1_Click(sender, e);
}
now if you want to show a message of which control was clicked all in one method do the following
private void label1_Click(object sender, EventArgs e)
{
Control control = (Control)sender;
var name = control.Name;
MessageBox.Show(string.Format("I pressed this {0} and showed me this messagebox",name));
}
Two ways to do this.
First:
private void button1_Click(object sender, EventArgs e)
{
Label1_Click(sender, e); // Just call the Label's click handler
}
Second:
// Bind the Label1_Click handler to the button1 Click event, as they both use the same delegate
button1.Click += new EventHandler(Label1_Click);
With the second approach, note that in C# delegates are multi-cast, so both the button1_Click handler and the Label1_Click handler will be called when the button is clicked, in the order they were bound.
private void button1_Click(object sender, EventArgs e)
{
//What the label click do:
MessageBox.Show("I pressed this label and showed me this messagebox");
}
Is that not easier?
Why do you want to do it ?
I think it would be easier for you to just include the lable click functionality with the button click. Maybe even separate each piece in their own method and call them from the button click. Here is how you'd call another click event.
private void button1_Click(object sender, EventArgs e)
{
label1_Click(sender, e);
}
public class MyLabel:Label
{
public void PerformClick()
{
OnClick(new EventArgs());//InvokeOnClick(this,new EventArgs());
}
}