why doesnt my panel show all my buttons in my c# application? - c#

my panel in my windows form application doesn't include all the buttons i asked it to. It shows only 1 button, Here is the code
private void AddAlphaButtons()
{
char alphaStart = Char.Parse("A");
char alphaEnd = Char.Parse("Z");
for (char i = alphaStart; i <= alphaEnd; i++)
{
string anchorLetter = i.ToString();
Button Buttonx = new Button();
Buttonx.Name = "button " + anchorLetter;
Buttonx.Text = anchorLetter;
Buttonx.BackColor = Color.DarkSlateBlue;
Buttonx.ForeColor = Color.GreenYellow;
Buttonx.Width = 30;
Buttonx.Height = 30;
this.panelButtons.Controls.Add(Buttonx);
//Buttonx.Click += new System.EventHandler(this.MyButton_Click);
}
}

Aren't they all going to be on the same position?
Try setting Buttonx.Location = new Point(100, 200);
(but with different points for different buttons)

You could use a FlowLayoutPanel, which would take care of the layout for you, or you need to track the locations yourself, or which could look something like this:
private void AddAlphaButtons()
{
char alphaStart = Char.Parse("A");
char alphaEnd = Char.Parse("Z");
int x = 0; // used for location info
int y = 0; // used for location info
for (char i = alphaStart; i <= alphaEnd; i++)
{
string anchorLetter = i.ToString();
Button Buttonx = new Button();
Buttonx.Name = "button " + anchorLetter;
Buttonx.Text = anchorLetter;
Buttonx.BackColor = Color.DarkSlateBlue;
Buttonx.ForeColor = Color.GreenYellow;
Buttonx.Width = 30;
Buttonx.Height = 30;
// set button location
Buttonx.Location = new Point(x, y);
x+=30;
if(x > panel1.Width - 30)
{
x = 30;
y+=30;
}
this.panelButtons.Controls.Add(Buttonx);
//Buttonx.Click += new System.EventHandler(this.MyButton_Click);
}
}

Related

Why label overLapping on text box in win form c# while generating dynamic text box and label?

try
{
int txtno = 10;
int Textbox_pointY = 15;
int label_pointY = 15;
int label_pointX = 10;
int Textbox_pointX = 75;
panel1.Controls.Clear();
for (int i = 0; i < txtno; i++)
{ //Lable creation
Label lbl = new Label();
panel1.Controls.Add(lbl);
lbl.Text = "Test_" + i;
lbl.Location = new Point(label_pointX, label_pointY);
label_pointY += 22;
//Text box creating
TextBox a = new TextBox();
panel1.Controls.Add(a);
a.Text = (i + 1).ToString();
a.Location = new Point(Textbox_pointX, Textbox_pointY);
//panel1.Show();
Textbox_pointY += 22;
//label_pointY += 5;
}
}
catch (Exception)
{
MessageBox.Show(e.ToString());
}
When dynamically generate label and enter code heretext box in winform c# Label overlaps over textbox .I need to keep text box closely to label text .I have added my code here .
Set AutoSize to false and specify labels' Width explicitly:
int txtno = 10;
int label_pointY = 15;
int label_pointX = 10;
int Textbox_pointX = 75;
// Don't do this: it just removes conrols from the panel,
// but does't free resources (and you have resource leakage)
// panel1.Controls.Clear();
// If you want to get rid of all controls on the panel1 (i.e. dispose them)
// do it like this:
for (int i = panel1.Controls.Count - 1; i >= 0; --i)
panel1.Controls[i].Dispose();
for (int i = 0; i < txtno; i++) {
Label lbl = new Label() {
Parent = panel1,
Text = "Test_" + i,
Location = new Point(label_pointX, label_pointY),
AutoSize = false,
Width = Textbox_pointX - label_pointX,
};
TextBox box = new TextBox() {
Parent = panel1,
Text = (i + 1).ToString(),
Location = new Point(Textbox_pointX, label_pointY)
};
label_pointY += Math.Max(box.Height, lbl.Height);
}

How to place button at the end of form, but when AutoScroll = true

I have multiple GroupBox's, and that's why I set AutoScroll to true. I create all controls in Form_Load. How to place one button after all GroupBox'es?
The code, where I create GroupBoxes:
for (int i = 0; i < 10; i++)
{
GroupBox gb = new GroupBox();
gb.Name = "GroupBox" + (i + 1);
gb.Size = new Size(500, 200);
gb.Location = new Point(40, loc);
gb.BackColor = System.Drawing.Color.Aquamarine;
Label q_text = new Label(); // текст питання
q_text.Name = "label" + (i + 1);
q_text.Text = "Питання" + (i + 1);
q_text.Font = new Font("Aria", 10, FontStyle.Bold);
q_text.Location = new Point(10, 10);
gb.Controls.Add(q_text);
int iter = q_text.Location.Y + 30;
if (i <= 5)
{
foreach (string key in questions[i].answers.Keys)
{
RadioButton rb = new RadioButton();
rb.Text = key;
rb.Size = new Size(120, 25);
rb.Location = new Point(q_text.Location.X + 10, iter);
iter += 30;
gb.Controls.Add(rb);
}
}else
if (i > 5)
{
foreach (string key in questions[i].answers.Keys)
{
CheckBox rb = new CheckBox();
rb.Text = key;
rb.Size = new Size(120, 25);
rb.Location = new Point(q_text.Location.X + 10, iter);
iter += 30;
gb.Controls.Add(rb);
}
}
this.Controls.Add(gb);
loc += 200;
Place all the scrollable group boxes in one panel which is set to AutoScroll = true. Below this panel is another one containing the fixed button. This panel is docked to the bottom.
Since you are using a loc variable, you can do:
btnMyButton.Locaction= new Point(40, loc);
Anyway, if you want to find the position of the last group box dynamically, try this:
double leftPos=0,topPos=0;
foreach(Control c in Forms.Controls)
{
if(c.Name=="GroupBox")
{
if(c.Left>leftPos)
leftPos=c.Left;
if(c.Top>topPos)
topPos=c.Top;
}
}

How to enable auto scroll?

I have a feeling that Im am missing something obvious but:
I have a single row of pictures in a form, in theory the pictures could go on forever. I need a scroll bar so that the user can look at all of the pictures in the row. I know I need to enable auto scroll but I have no idea how to enable it. Can someone tell me how to enable it or something that I am missing?
If it helps this is the code i am using to generate the pictures:
private void imagePalletToolStripMenuItem_Click(object sender, EventArgs e)
{
MyPalletGui.Show();
Dictionary<string,Bitmap> MyPallet = MyImageCollection.ToDictionary();
int xcor = -50;
int ycor = 0;
foreach (Bitmap curtImage in MyPallet.Values){
PictureBox myPicBox = new PictureBox();
xcor += 50;
myPicBox.Location = new Point(xcor, ycor);
myPicBox.Width = 50;
myPicBox.Height = 50;
myPicBox.Visible = true;
myPicBox.Image = new Bitmap(curtImage);
this.MyPalletGui.Controls.Add(myPicBox);
This code will do exactly what you want, it uses the Form as the ViewPort with AutoScroll:
public Form1()
{
InitializeComponent();
PopulatePictures();
}
private void PopulatePictures()
{
this.AutoScroll = true;
string[] list = Directory.GetFiles(#"C:\\Users\\Public\\Pictures\\Sample Pictures", "*.jpg");
PictureBox[] picturebox= new PictureBox[list.Length];
int y = 100;
for (int index = 0; index < picturebox.Length; index++)
{
picturebox[index] = new PictureBox();
this.Controls.Add(picturebox[index]);
picturebox[index].Location=new Point(index * 120, y);
if(x%12 == 0)
y = y + 150;
picturebox[index].Size = new Size(100,120);
picturebox[index].Image = Image.FromFile(list[index]);
}
}

How Can I Add multipe buttons on c# dynamically using point class?

Hello I try to add multiple Buttons on a panel on form and next to each other but instead it put them above each other.
I am using the following function.
the Code:
private void CreatBtn()
{
Point[] p = new Point[6];
string log = "";
Form2 frm2 = new Form2();
Button[] btn = new Button[6];
for (int i = 0; i < btn.GetLength(0); i++)
{
btn[i] = new Button();
btn[i].Height = 65;
btn[i].Width = 80;
p[i] = new Point();
p[i].X = i * 83;
p[i].Y =0;
log +=p.ToString() +"\n";
btn[i].PointToClient(p[i]);
btn[i].Show();
}
panel1.Controls.AddRange(btn);
}
Add a Left value to your buttons
btn[i] = new Button();
btn[i].Height = 65;
btn[i].Width = 80;
btn[i].Left = i * 83; //Now they'll be next to each other.
You may also consider using the FlowLayoutPanel.
Use flowLayoutPanel in the panel of it'll but the next to each other until the button reach the end of it, then it will make an new raw
Point[] p = new Point[6];
string log = "";
Button[] btn = new Button[6];
for (int i = 0; i < btn.GetLength(0); i++)
{
btn[i] = new Button();
btn[i].Height = 65;
btn[i].Width = 80;
p[i] = new Point();
p[i].X = i * 83;
p[i].Y = 0;
log += p.ToString() + "\n";
btn[i].PointToClient(p[i]);
btn[i].Show();
}
FlowLayoutPanel pan = new FlowLayoutPanel();
pan.Width=500;//width of all buttons
pan.Height = 100;
pan.Controls.AddRange(btn);
panel1.Controls.Add(pan);

Custom MessageBox

I want to create a custom messagebox
but i want use the custom icons and custom sound in during show messagebox
how do I create this Messagebox??
I no want to use the shell32.dll and user32.dll
a messagebox the same as windows 7 for windows xp
I used a simple dialog with a static method ShowCustomButtonsDialog. I placed a text label in the top left corner and changed border style to Dialog. Method simply returns button index or -1.
public partial class CustomButtonsDialog : Form
{
private const int ButtonHeight = 24;
private const int ButtonPadding = 6;
private const int ButtonInnerPadding = 5;
private const int MaxFormWidth = 700;
private int buttonIndex = -1;
public int ButtonIndex
{
get { return buttonIndex; }
private set { buttonIndex = value; }
}
public static int ShowCustomButtonsDialog(string text, string title, params string[] buttonsText)
{
var dlg = new CustomButtonsDialog(text, title, buttonsText.ToList());
dlg.ShowDialog();
return dlg.ButtonIndex;
}
public static int ShowCustomButtonsDialog(string text, string title, List<string> buttonsText)
{
var dlg = new CustomButtonsDialog(text, title, buttonsText);
dlg.ShowDialog();
return dlg.ButtonIndex;
}
public CustomButtonsDialog()
{
InitializeComponent();
}
private CustomButtonsDialog(string text, string title, List<string> buttonsText)
{
InitializeComponent();
Text = title;
labelText.Text = text;
// добавить кнопки
var formWidth = ClientSize.Width;
List<int> buttonWidths;
using (var gr = CreateGraphics())
{
buttonWidths = buttonsText.Select(b => (int)gr.MeasureString(b, Font).Width + 2 * ButtonInnerPadding).ToList();
}
var totalButtonWd = buttonWidths.Sum() + (buttonWidths.Count - 1) * ButtonPadding;
if (totalButtonWd > formWidth)
{
if (totalButtonWd <= MaxFormWidth)
Width = Width - ClientSize.Width + totalButtonWd + ButtonPadding * 2;
else
{// trim some buttons
Width = Width - ClientSize.Width + MaxFormWidth;
totalButtonWd = ClientSize.Width - ButtonPadding * 2;
var avgWidth = (totalButtonWd - (buttonsText.Count - 1) * ButtonPadding) / buttonsText.Count;
var sumThins = buttonWidths.Sum(w => w <= avgWidth ? w : 0);
var countThins = buttonWidths.Count(w => w <= avgWidth);
var countFat = buttonsText.Count - countThins;
var spareRoom = totalButtonWd - sumThins;
var fatWidth = (countThins == 0) || (countFat == 0)
? avgWidth
: (spareRoom - (countThins - 1)*ButtonPadding)/countFat;
for (var i = 0; i < buttonWidths.Count; i++)
if (buttonWidths[i] > avgWidth) buttonWidths[i] = fatWidth;
}
}
// buttons' Y-coords and height
labelText.MaximumSize = new Size(totalButtonWd,
labelText.MaximumSize.Height);
var buttonTop = labelText.Bottom + ButtonPadding;
var formHeight = buttonTop + ButtonHeight + ButtonPadding;
Height = Height - ClientSize.Height + formHeight;
// do make buttons
var buttonLeft = ButtonPadding;
var tag = 0;
for (var i = 0; i < buttonWidths.Count; i++)
{
var button = new Button
{
Parent = this,
Width = buttonWidths[i],
Height = ButtonHeight,
Left = buttonLeft,
Top = buttonTop,
Text = buttonsText[i],
Tag = tag++
};
button.Click += ButtonClick;
buttonLeft = button.Right + ButtonPadding;
Controls.Add(button);
}
}
private void ButtonClick(object sender, EventArgs e)
{
ButtonIndex = (int) ((Button) sender).Tag;
Close();
}
}
Easiest way would be to create your own MessageBox window from scratch. If you are looking for hooks to default windows MessageBox you need to consider that later you can run into problems like compatibility with other Windows operating systems.
Here are couple of samples how to create your own MessageBox:
Creating A Custom Message Box
A Custom Message Box
Custom Message Box
That will give you an idea about logic and how to start writing your own custom MessageBox.

Categories

Resources