How can I create a button with a dropdown arrow near it? - c#

I did :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test
{
public partial class Form1 : Form
{
private int x, y;
private int gap = 0;
private int startingY = 83;
private GroupBox lastGB = null;
public Form1()
{
InitializeComponent();
button1.Text = "Details " + char.ConvertFromUtf32(8595);
}
The result is something bad not looking good :
This is what I want to make : The NEW is acting like a button it is a button. The arrow near it is like a combobox :
If I click on the NEW it will open folder dialog if I click on the arrow it will open kind of combobox without the borders.

This is called SplitButton. This question has been asked few times over the years.
You can draw an inspiration from the Windows Forms codebase.
Or look at other answers to the same question: Split button in .NET Winforms.
Or search online for SplitButton.

Related

C# smooth resize of the form (smooth resize efect)

First of all, beginner here, apologies in advance if my question has obvious answer, but until now, I didn't manage to figure it out.
So, I'm trying to make an Windows Form App. In this app I have two forms: the login form and main window form. To make it look more "fancy", after successfully logging in, the main window form must increase its size (same size as the login form) to something bigger. I managed to create the code I need, but the resize effect looks "laggy". Is there a way to make this smother? Or is there another way to make the resize of the form smoother?
PS. I have set the "FormBorderStyle" property to "None"
Bellow, my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SlindingPanel
{
public partial class MainWindow : Form
{
public MainWindow()
{
InitializeComponent();
// Enables the timer
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Width >= 820 && this.Height >= 540) this.timer1.Enabled = false;
else {
// Resizes the form
this.ClientSize = new System.Drawing.Size(Width + 20, Height + 20);
//Centers the form on the screen
this.CenterToScreen();
}
}
}
This may be late, but I suggest using the "anchor" attribute of the UI controls.

How to use event handlers in C#

I created a form with just 2 textboxes and a button. In the first one I type a temperature in Fahrenheit and when I press the button "Convert", the program calculates and puts the temperature in Celsius in the other TextBox. It's working fine.
Now I want the program to clear the second TextBox when I start typing in the first TextBox. Below I show just a part of the code, which didn't work. Can anybody help me?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Conv_Temp
{
public partial class Frm_Principal : Form
{
public Frm_Principal()
{
InitializeComponent();
}
public event EventHandler Leave;
private void Tb_Temp_Leave(object sender, EventArgs e)
{
MessageBox.Show("Leaving TB Tb_Temp");
Tb_Result.Text="";
}
}
}
I think you are almost there.
Try adding this under InitializeComponent();
this.Tb_Temp.TextChanged += new System.EventHandler(this.Tb_Temp_Leave);
Add new Event handler in your form designer code.
this.textBox1.TextChanged += new System.EventHandler(this.ModifyTextBox1);
and implement this event in above form.cs file(Form_Principal )
private void ModifyTextBox1(object sender, EventArgs e)
{
textBox2.Text = String.Empty;
}
Please follow good convention for writing codes this is just a demo.

Set the WinSpy++ Control id at a C# window

I have a little C# program with TextBox and other components. When I examing the TextBox (or other) components with WinSpy++, I get a Control ID which I cannot find at my C# source code. For example - see screenshots: When I examine the TextBox field with number 5, WinSpy++ detects the Control ID 000209AA.
How identifies WinSpy the Control ID? Is there any possibility to set the Control ID at the C# source code?
Cheers,
Achim
GetDlgCtrlID from user32.dll will provide the control ID given the window handle (ie, Control.Handle). Very simple form to demonstrate:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll")]
static extern int GetDlgCtrlID(IntPtr hwnd);
private void textBox1_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("Control ID: " + GetDlgCtrlID(textBox1.Handle));
}
}
}
I'm not sure that there's any way to set the control ID - I suspect the control ID is set automatically by Windows Forms somehow in the form construction. But being able to retrieve it should be sufficient for most applications (eg direct use of PostMessage/SendMessage from Windows API.)

TextBox not showing in WinForms form

I am adding a TextBox to my form at runtime, and this is a brand new project, so this is the only code I have so far, so I am 100% positive that this is not my own doing:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TextBox box = new TextBox();
box.Location = new Point(2, 2);
this.Controls.Add(box);
}
}
}
Why won't the TextBox display? There's nothing at all. I set breakpoints all over the place, but none of them how anything that could help me. All seems normal, but isn't.
The code is very simple, the only reason I can think of is you have some other control added before (wide enough to cover the added TextBox), try this:
private void button1_Click(object sender, EventArgs e)
{
TextBox box = new TextBox();
box.Location = new Point(2, 2);
this.Controls.Add(box);
box.BringToFront();
}
Also check the event handler ControlAdded, I guess the form has some code for this event handler and discard the control added if it's type of TextBox, something like this:
private void form_ControlAdded(object sender, ControlEventArgs e) {
if(e.Control is TextBox) Controls.Remove(e.Control);
}
The code that adds the textbox to the form is in button1_Click event handler. If you move it to the constructor, it will work just fine.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
TextBox box = new TextBox();
box.Location = new Point(2, 2);
this.Controls.Add(box);
}
}
}
I had a similar problem.
Looking at the code I found out that the textboxes that DO show were of type System.Windows.Forms.TextBox while those that DID NOT show were of type VisualJS.Web.TextBox. Perhaps your problem is similar.

How to move form box to a new location

I have a windows form project, and I want the whole form to change location automatically, but the truth is that I have no idea what to call, and where to call it. I have searched online, and all code I discovered was incomplete. I am fairly new at this, so it did not help me.
Here is the code that I am working with, if it helps:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private SoundPlayer _soundplayer;
public Form1()
{
InitializeComponent();
SoundPlayer player = new SoundPlayer(Properties.Resources.sound);
player.Play();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
var myForm = new Form2();
myForm.Show();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
_soundplayer.PlayLooping();
}
}
}
Change the Location for the form:
this.Location = new Point(400, 500);
You just need to decide which event will trigger this code; for example, the Click event of a button.
MSDN: Location
To position forms using the Properties window
In the Properties window, choose the form from the drop-down. Set the form's StartPosition property to Manual.
Type the values for the Location property, separated by a comma, to position the form, where the first number (X) is the distance from the left border of the display area and second number (Y) is the distance from the upper border of the display area.
Note Expand the Location property to enter the X and Y subproperty values individually.
Reference MSDN

Categories

Resources