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();
}
Related
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);
}
Visual Studio C #
I made a calculator, and now I have to make a calculator memory (event).
There are 4 components other than the calculator: one Textbox for the answer of the calculator, two Buttons for "M" and "M+", and one Lable to display the answer again.
When the user clicks the “M” button, the contents of the Answer TextBox should be copied to a memory variable. Also make it so that when the user moves the mouse over the label, the value in the memory variable will appear in this label, and then disappear, when the mouse moves away from the label. Also add one more button, an “M+” button. When the user clicks this button, the contents of the Results box will be added to Memory. You will need to use a Global Variable to store this data.
My problem is that the label doesn't appear when the mouse over the label, and also it doens't disappear when the mouse leave the label. How can I fix it?
And also, is this way the right way to use the Global variable?
Below is my code (I just put the code for "M" and "M+" buttons, not the code for the calculator).
private String ans;
private Double answer;
private Double answerPlus;
private void btnM_Click(object sender, EventArgs e)
{
ans = txtDisplay.Text;
answer = double.Parse(ans);
lblblank.Text = answer.ToString();
}
private void lblblank_MouseEnter(object sender, EventArgs e)
{
lblblank.Show();
lblblank.Text = answer.ToString();
}
private void lblblank_MouseLeave(object sender, EventArgs e)
{
lblblank.Hide();
}
private void btnMplus_Click(object sender, EventArgs e)
{
answerPlus = answer + double.Parse(ans);
lblblank.Text = answerPlus.ToString();
}
Storing variables
The way you store your values is fine.
Events
Once you call .Hide() the next MouseEnter/MouseLeave-event will not be triggered anymore. What you could do is to take a panel, or any layout element as a wrapper/parent-element for the label and then adjust your event-callbacks to something like that:
private void panel_MouseEnter(object sender, EventArgs e)
{
lblblank.Show();
lblblank.Text = answer.ToString();
}
private void panel_MouseLeave(object sender, EventArgs e)
{
lblblank.Hide();
}
Edit
~~~
What does it mean that any layout element as a parent-element for the
label? Could you explain more?
What I meant was to just create a new panel (or layout-element) and put the label into it as a child. See the picture below:
If you set that up correctly, the code snippet I posted above will work just fine. This solution does not prevent the MouseLeave event from triggering when your mouse enters the label. Therefore you could use an alternative solution using the MouseMove event.
Alternative
using System;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
this.InitializeComponent();
// Subscribe to the MouseMove event
this.panel.MouseMove += this.panel_MouseMove;
}
private void panel_MouseMove(object sender, MouseEventArgs e)
{
// Checks if current mouse position is within the panel
if (this.panel.Bounds.Contains(new Point(e.X, e.Y)))
{
// Current mouse position within the panel
this.label.Show();
return;
}
// Current mouse position outside the panel
this.label.Hide();
}
}
}
I have a listview control on my WinForms application.
here, on click of separate button, i do change couple of listview items backcolor and reload the whole grid as there are certain changes into database so, reloading from database on each click of button.
Now, problem is, once the grid is reloaded then lastly added items are scrolled so, need to scroll all items and find so, it makes hard to end user.
Is there any way to ,scroll the lastly added items or updated items into listview automatically (I mean, programmatically so, it could be view to user directly without being manually scrolled).
listView1.EnsureVisible(X);
where X is the item index.
This snippet can be used to scroll the ListView automatically to a particular index in the listView.
Consider the code: with this you can automatically scroll to the index 8 on button click
private void button2_Click(object sender, EventArgs e)
{
listView1.EnsureVisible(8);
}
Despite #user3711357 correct answer, I spent too much time trying to understand why it is not working for me.
I found that trying to call EnsureVisible in the constructor of the form will not work.
public class MyForm
{
public MyForm()
{
InitializeComponent();
listView1.EnsureVisible(8); // will not work !!!
}
private void MyForm_Load(object sender, EventArgs e)
{
listView1.EnsureVisible(8); // Works fine
}
}
Before you refresh the list, store the currently focussed or selected item (depending on how your interaction code works) into a variable, then you can restore the selected item afterwards. For example;
Dim selectedObjectName = listview.SelectedItems(0).Name
...
' refresh your list
...
Dim vItem as ListViewItem
If listview.SelectedItem.ContainsKey(selectedObjectName) Then
vItem = listview.Items(selectedObjectName)
Else
vItem = listview.Items(0)
End If
vItem.Selected = True
vItem.Focus
Can send messages directly.
public partial class Form1 : Form
{
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
public Form1()
{
InitializeComponent();
c_scroll.ScrollSlide += C_scroll_ScrollSlide;
}
private void C_vScrollBar_Scroll(object sender, ScrollEventArgs e)
{
const int LVM_SCROLL = (0x1000 + 20);
SendMessage(c_listView_show.Handle, LVM_SCROLL, 0, e.NewValue - e.OldValue);
}
}
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 would like to implement a drag&drop operation from TextBox to another control.
The problem is that when you select some part of text and then click on the TextBox text is deselected. Therefore when I perform DoDragDrop in MouseDown event the textBox.SelectedText is already empty.
Is there any way to avoid such behavior? I found following example but I don't want to loose the possibility to drag&drop only a part of text.
I found solution. You need to inherit TextBox and override OnMouseDown and WndProc:
public class DragTextBox : TextBox
{
private string dragText;
private const int WM_LBUTTONDOWN = 0x201;
protected override void OnMouseDown(MouseEventArgs e)
{
if (dragText.Length > 0)
{
SelectionStart = Text.IndexOf(dragText);
SelectionLength = dragText.Length;
DoDragDrop(dragText, DragDropEffects.Copy);
SelectionLength = 0;
}
base.OnMouseDown(e);
}
protected override void WndProc(ref Message m)
{
if ((m.Msg == WM_LBUTTONDOWN))
dragText = SelectedText;
base.WndProc(ref m);
}
}
Original code author post here