At the top :
private int gap = 0;
Then in a button click event :
private void btnADD_Click(object sender, EventArgs e)
{
var fsd = new FolderSelectDialog();
fsd.Title = "What to select";
fsd.InitialDirectory = #"c:\";
if (fsd.ShowDialog(IntPtr.Zero))
{
AddGroupBox(Path.GetFileName(fsd.FileName));
}
}
And the AddGroupBox method :
private void AddGroupBox(string Name)
{
gap = gap + 83;
GroupBox gb = new GroupBox();
gb.Location = new Point(3, gap);
gb.Size = new Size(1311, 100);
gb.BackColor = SystemColors.Window;
gb.Text = Name;
this.Controls.Add(gb);
}
First time the location on y is 83 next time the location is on y 166 but the new groupBox on 1660 is still some of it touch or inside the first groupBox.
I dont wan't a space between them I want that the borders of the first and the next one will touch each other like one common border at the top ofthe new and the bottom of the first. and the same when adding a new groupBox each time.
So many ways to do this...
How about just storing the last GroupBox placed and using its Bounds.Bottom property? If that variable is null, then place at the initial position of 83.
Something like:
private int startingY = 83;
private GroupBox lastGB = null;
private void AddGroupBox(string Name)
{
GroupBox gb = new GroupBox();
gb.Location = new Point(3, (lastGB==null ? startingY : lastGB.Bounds.Bottom));
gb.Size = new Size(1311, 100);
gb.BackColor = SystemColors.Window;
gb.Text = Name;
this.Controls.Add(gb);
lastGB = gb;
}
Related
I am displaying a transparent icon (32x32) of a red triangle in the upper right of a button control that signifies an error exists. Additionally, when the user hovers over the icon, a tool tip is displayed.
I have been able to display the icon and the associated tool tip. The problem is a transparent 32x32 icon with the red triangle being only 12x12. The tool tip should only trigger when hovering over the red triangle and not the transparent space.
Attempts have been made to display the triangle as a button as well as a picture box, however the tool tip still triggers in the transparent space. Additionally, the error provider was first used as a goal of what I am trying to accomplish.
UI items:
Button control: "btnAttachments"
Error Provider control: "errManager"
public class StackTest
{
private static Dictionary<string, Control> _errorMessages = new Dictionary<string, Control>();
public StackTest()
{
InitializeComponent();
InitErrors();
}
private void InitErrors()
{
_errorMessages.Clear();
AddErrorControl(btnAttachments, "Missing file attachment(s).");
//errManager.SetError(btnAttachments, "Missing file attachment(s)."); errManager.SetIconPadding(btnAttachments, -32);
}
private void AddErrorControl(Control control, string message = null, Enum selectedImage = null, EventHandler handler = null)
{
string name = "errFor" + control.Name;
if (_errorMessages.ContainsKey(name)) { return; }
Button errorIcon = CreateErrorControl(name, control);
errorIcon.BackgroundImage = Theme.GetImage(selectedImage ?? eImages_OtherIcons.Error_TopRight_Small);
//PictureBox errorIcon = CreateErrorControl2(name);
//errorIcon.Image = Theme.GetImage(selectedImage ?? eImages_OtherIcons.Error_TopRight_Small);
//errorIcon.Image = Bitmap.FromHicon((Theme.GetIcon(selectedImage ?? eImages_OtherIcons.Error_TopRight_Small)).Handle);
if (null != handler) { errorIcon.Click += handler; }
new ToolTip().SetToolTip(errorIcon, message);
errorIcon.Tag = message;
control.Controls.Add(errorIcon);
control.Controls[name].Location = new Point(control.Width - errorIcon.Width +20 , 0 );
_errorMessages.Add(name, errorIcon);
}
private Button CreateErrorControl(string name, Control control)
{
var errorIcon = new Button();
errorIcon.Name = name;
errorIcon.Size = new Size(32, 32);
//errorIcon.Location = new Point(control.Width - errorIcon.Width, 0);
errorIcon.Cursor = Cursors.Hand;
errorIcon.FlatStyle = FlatStyle.Flat;
errorIcon.BackColor = Color.Fuchsia;
errorIcon.FlatAppearance.MouseDownBackColor = Color.Transparent;
errorIcon.FlatAppearance.MouseOverBackColor = Color.Transparent;
errorIcon.FlatAppearance.BorderSize = 0;
errorIcon.Visible = false;
return errorIcon;
}
private PictureBox CreateErrorControl2(string name)
{
var errorIcon = new PictureBox();
errorIcon.Name = name;
errorIcon.Size = new Size(32, 32);
errorIcon.Cursor = Cursors.Hand;
errorIcon.BackColor = Color.Transparent;
errorIcon.Visible = false;
return errorIcon;
}
}
The built in Error Provider control achieves the desire results that I would like to replicate. Doing so will allow for a more robust application with more custom functionality then what the error provider offers.
Based on the GetPixel suggestion from #TaW, I did further R&D and now have something functional. The Tag of the picture box contains the tool tip message to be displayed. With the picture box being the "sender" of the mouse move, it was easy to extract the image back to a bitmap.
Thanks to all for the feedback.
First, I switched the testing to use CreateErrorControl2 with the PictureBox and added in a MouseMove.
private PictureBox CreateErrorControl2(string name) //, Control control)
{
var errorIcon = new PictureBox();
errorIcon.Name = name;
errorIcon.Size = new Size(32, 32);
errorIcon.Cursor = Cursors.Default;
errorIcon.BackColor = Color.Transparent;
errorIcon.Visible = false;
errorIcon.MouseMove += new MouseEventHandler(DisplayToolTip);
return errorIcon;
}
The following code was also added in support of the DisplayToolTip method.
private bool _toolTipShown = false;
private bool IsTransparent(PictureBox pb, MouseEventArgs e)
{
Color pixel = ((Bitmap)pb.Image).GetPixel(e.X, e.Y);
return (0 == pixel.A && 0 == pixel.R && 0 == pixel.G && 0 == pixel.B);
}
private void DisplayToolTip(object sender, MouseEventArgs e)
{
Control control = (Control)sender;
IsTransparent((PictureBox)control, e);
if (IsTransparent((PictureBox)control, e))
{
_toolTip.Hide(control);
_toolTipShown = false;
}
else
{
if (!_toolTipShown)
{
_toolTip.Show(control.Tag.ToString(), control);
_toolTipShown = true;
}
}
}
I have a checkedListBox in a TabControl
What I want is to create a label and NumericUpDown dynamically, when User check an item of checkedListBox it will show the new label and NumericUpDown
Then , when it Unchecked this item ,The numericUpDown will be clear (empty).
Conclusion: As many checked items , as many w've create labels and NumericUpDowns.
Please, how will I do that ??
For each checkbox item in your checkedListBox in properties switch to events and create subscriber checkBoxName_CheckStateChanged for event CheckStateChanged.
The code in the sucriber can be like this:
private void checkBox1_CheckStateChanged(object sender, EventArgs e)
{
var source = sender as CheckBox;
if (source.Checked == true)
{
this.numericUpDown1.Text = "TextWhenChecked";
this.labelAtTheNumericUpDown.Text = "TextWhenChecked";
}
else
{
this.numericUpDown1.Text = "TextWhenUnchecked";
this.label1.Text = "TextWhenUnchecked";
}
}
You fill the strings as you want. These are only examples.
To have only checkBox checked at a time look at here: https://stackoverflow.com/a/24693858/6650581.
What you need to do is creating Label and NumericUpDown manually and show it by adding to Controls collection. A TableLayoutPanel can help you arranging controls without setting Size and calculate Location manually.
Here is an example:
public class MainForm : Form
{
private CheckedListBox checkedListBox;
private TableLayoutPanel tableLayoutPanel;
public MainForm()
{
InitializeComponent();
//Fill checkedListBox and create controls
for( int i = 0; i <= 5; i++ )
{
checkedListBox.Items.Add( i.ToString() );
Label lbl = new Label()
{
Name = "lbl" + i,
Text = "Label " + i,
Visible = false
};
NumericUpDown num = new NumericUpDown()
{
Name = "num" + i,
Value = i,
Visible = false
};
tableLayoutPanel.Controls.Add( lbl, 0, i );
tableLayoutPanel.Controls.Add( num, 1, i );
}
}
private void checkedListBox_ItemCheck( object sender, ItemCheckEventArgs e )
{
if( e.NewValue == CheckState.Checked )
{
tableLayoutPanel.Controls["lbl" + e.Index].Visible = true;
tableLayoutPanel.Controls["num" + e.Index].Visible = true;
}
else
{
tableLayoutPanel.Controls["lbl" + e.Index].Visible = false;
((NumericUpDown)tableLayoutPanel.Controls["num" + e.Index]).Value = 0M;
}
}
private void InitializeComponent()
{
this.checkedListBox = new System.Windows.Forms.CheckedListBox();
this.tableLayoutPanel = new System.Windows.Forms.TableLayoutPanel();
this.SuspendLayout();
//
// checkedListBox
//
this.checkedListBox.Location = new System.Drawing.Point(8, 8);
this.checkedListBox.Name = "checkedListBox";
this.checkedListBox.Size = new System.Drawing.Size(200, 100);
this.checkedListBox.TabIndex = 1;
this.checkedListBox.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.checkedListBox_ItemCheck);
//
// tableLayoutPanel
//
this.tableLayoutPanel.AutoScroll = true;
this.tableLayoutPanel.ColumnCount = 2;
this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel.Location = new System.Drawing.Point(8, 112);
this.tableLayoutPanel.Name = "tableLayoutPanel";
this.tableLayoutPanel.Size = new System.Drawing.Size(200, 100);
this.tableLayoutPanel.TabIndex = 2;
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(223, 227);
this.Controls.Add(this.tableLayoutPanel);
this.Controls.Add(this.checkedListBox);
this.Name = "MainForm";
this.ResumeLayout(false);
}
}
I'm trying to add panel to already existing panel with name Tablica
but im doing it wrong im increasing the global variable each time i add the a panel so the new added panel has different Y location and this makes my panels not overlapping eachother.
But now i want to use a different approach so that i dont add them with fixed X,Y location and instead i somehow dock them on top and each new panel addded stays at the top of the parent panel so in other words the last panel added when the button is clicked stays on top of the Tablica panel
This is the code i use now which works except that the last panel is added at the bottom of the panel:
int TabliciLocation = 30; //global variable
private void OK_Click(object sender, EventArgs e)
{
Panel newPanel = new Panel();
newPanel.Size = new System.Drawing.Size(1200, 52);
newPanel.Location = new System.Drawing.Point(16, TabliciLocation);
Tablica.Controls.Add(newPanel);
TabliciLocation += 60;
}
So the new approach has to be something like this :
private void OK_Click(object sender, EventArgs e)
{
Panel newPanel = new Panel();
newPanel.Size = new System.Drawing.Size(1200, 52);
newPanel.Dock = DockStyle.Top; // if this can help
Tablica.Controls.Add(newPanel);
}
The FlowLayoutPanel was made for this.
Here is an example; I use different BackColors to show how each new Panel pushes the previous ones down:
Random R = new Random();
private void button1_Click(object sender, EventArgs e)
{
Panel p = new Panel();
p.Name = "panel" + (flowLayoutPanel1.Controls.Count + 1);
p.BackColor = Color.FromArgb(123, R.Next(222), R.Next(222));
p.Size = new Size(flowLayoutPanel1.ClientSize.Width, 50);
flowLayoutPanel1.Controls.Add(p);
flowLayoutPanel1.Controls.SetChildIndex(p, 0); // this moves the new one to the top!
// this is just for fun:
p.Paint += (ss, ee) => {ee.Graphics.DrawString(p.Name, Font, Brushes.White, 22, 11);};
flowLayoutPanel1.Invalidate();
}
I am new to C# and programming in general. I was trying to make a simple shopping list app using a windows form application and Visual studio. This is how I am adding items to the list.
public Form1()
{
InitializeComponent();
}
int x = 50;
int y = 58;
private void addButton_Click(object sender, EventArgs e)
{
Label itemName = new Label();
itemName.Text = itemInput.Text;
itemInput.Text = "";
this.Controls.Add(itemName);
itemName.Location = new Point(x, y);
itemName.Width = 260;
CheckBox coupon = new CheckBox();
coupon.Location = new Point(x - 30, y);
this.Controls.Add(coupon);
y = y + 25;
}
The main issue I have is that I can't have another event change the properties of the label. EX:
public Form1()
{
InitializeComponent();
}
int x = 50;
int y = 58;
private void addButton_Click(object sender, EventArgs e)
{
Label itemName = new Label();
itemName.Text = itemInput.Text;
itemInput.Text = "";
this.Controls.Add(itemName);
itemName.Location = new Point(x, y);
itemName.Width = 260;
CheckBox coupon = new CheckBox();
coupon.Location = new Point(x - 30, y);
this.Controls.Add(coupon);
Button deletButton = new Button();
deletButton.Text = "delete";
this.Controls.Add(deletButton);
deletButton.Location = new Point(x + 260, y);
deletButton.Width = 50;
y = y + 25;
}
private void deletButton_Click(object sender, EventArgs e)
{
itemName.Text = "";
}
It says
the name itemName does not exist in the current context
which make sense because it is in a different method.
My main question is, can I make itemName available outside of that method? Or am I totally going about this wrong and have to redesign the program for the ground up?
Let's say that you want to stick with the dynamic addition of controls, as you're doing now, then a simple way is to give it a name and the find it by that name:
// When you're creating it.
itemName.Name = "itemName";
// Finding it.
var itemName = (Label)this.Controls["itemName"];
// Another way to find it.
var itemName = (Label)this.Controls.Find("itemName", true);
How do I make a kind of grid with colours that can be selected and be saved to a field on selection? Like in the chat options in Twitch.
Windows Form provides the ColorDialog
ColorDialog colorDialog = new ColorDialog();
colorDialog.ShowDialog();
The selected Color could be called with:
colorDialog.Color
All you need is TableLayoutPanel and panel for each cell of it:
public partial class MainForm : Form
{
private Color selected_color;
private List<Color> colors;
public MainForm()
{
InitializeComponent();
colors = new List<Color>();
colors.Add(Color.Red);
colors.Add(Color.Green);
colors.Add(Color.Blue);
colors.Add(Color.Yellow);
colors.Add(Color.Teal);
colors.Add(Color.RosyBrown);
colors.Add(Color.Lime);
colors.Add(Color.Gray);
tableLayoutPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetDouble;
for (byte i = 0; i < tableLayoutPanel.Controls.Count; i++)
{
Panel p = tableLayoutPanel.Controls[i] as Panel;
p.BackColor = colors[i];
p.Click += panel_click;
}
}
private void panel_click(object sender, EventArgs e)
{
Panel p = sender as Panel;
selected_color = p.BackColor;
lbl_color.Text = selected_color.ToString();
lbl_color.ForeColor = selected_color;
}
private void btn_showMoreColours_Click(object sender, EventArgs e)
{
Panel[] panels = new Panel[4];
for (byte i = 0; i < panels.Length; i++)
{
panels[i] = new Panel();
panels[i].Dock = DockStyle.Fill;
panels[i].Location = new System.Drawing.Point(3, 3);
panels[i].Name = "panel" + (i + 4);
panels[i].Size = new System.Drawing.Size(123, 100);
panels[i].BackColor = colors[i + 4];
panels[i].Click += panel_click;
tableLayoutPanel.Controls.Add(panels[i]);
}
Size = new Size(Size.Width, Size.Height * 2);
}
}
After clicking a cell you will get Color in field selected_color.
EDIT:
A have added show more colours button as well. It will expand like shown below:
Source code here