How to use Flow Layout Panel in C# - c#

I am currently working with c# and a flp.
My program uses a loop to add buttons like
I need a third button directly under the red one, to get a view like
Any ideas?
My code:
private void createButton(Entry e)
{
Button newButton = new Button();
newButton.Text = e.title;
newButton.BackColor = System.Drawing.Color.FromArgb(e.colorARGB);
//sizing
newButton.Size = new System.Drawing.Size(300, 60);
newButton.Font = new System.Drawing.Font(newButton.Font.FontFamily, 18);
newButton.Click += new EventHandler(newButton_Click);
newButton.ForeColor = System.Drawing.Color.FromArgb((int)InvertColor((uint)newButton.BackColor.ToArgb()));
newButton.Tag = Link_List.Count - 1;
m_fLPQuickboard.Controls.Add(newButton);
Button newButtonX = new Button();
newButtonX.Text = "X";
newButtonX.BackColor = System.Drawing.Color.FromName("Red");
newButtonX.Size = new System.Drawing.Size(30, 30);
newButtonX.Click += new EventHandler(newButtonX_Click);
newButtonX.Tag = Link_List.Count - 1;
//Tooltips
ToolTip newButtonTooltip = new ToolTip();
newButtonTooltip.SetToolTip(newButton, getParameterFileAsTooltip(e.path));
}

Related

How can I use a textbox, label and button that was added to a panel to input and show information

Okay so basically I have a calendar display and when you click on anyone of the dates on it, it creates a new panel with a label displaying the date selected. I also made it so when you click on a date and a new panel is made, a label, textbox and button is created and placed onto that new panel as well.
So what I want and have been struggling with is for me to enter something into that textbox then to press the button to submit it and then for it to show on the label.
I think I know what the issue is but I've been stuck at this for hours.
Here is my code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void monthCalendar1_DateSelected_1(object sender, DateRangeEventArgs e)
{
Panel newPanel = new Panel();
this.Controls.Add(newPanel);
newPanel.Visible = true;
newPanel.Size = new Size(564, 831);
newPanel.Location = new Point(0, 190);
newPanel.BringToFront();
Label textLabel = new Label();
textLabel.Size = new Size(500, 500);
textLabel.Font = new Font(textLabel.Font.Name, 25, textLabel.Font.Style);
textLabel.Location = new Point(3, 3);
Label dateLabel = new Label();
dateLabel.Size = new Size(500, 500);
dateLabel.Font = new Font(dateLabel.Font.Name, 25, dateLabel.Font.Style);
dateLabel.Location = new Point(128, 3);
Button Submitbutton = new Button();
Submitbutton.Location = new Point(100, 500);
Submitbutton.Text = "Add Food";
Submitbutton.Size = new Size(400, 100);
Submitbutton.BackColor = Color.Aqua;
Submitbutton.BringToFront();
Submitbutton.Click += Button_Click;
TextBox textBox = new TextBox();
textBox.Location = new Point(100, 650);
textBox.Size = new Size(500, 500);
textBox.BackColor = Color.Aqua;
textBox.Visible = true;
textBox.Text = "Enter food here...";
textBox.BringToFront();
Label inputtedFood = new Label();
inputtedFood.Size = new Size(500, 500);
inputtedFood.Font = new Font(inputtedFood.Font.Name, 25, inputtedFood.Font.Style);
inputtedFood.Location = new Point(100, 600);
inputtedFood.Text = "placeholder";
newPanel.Controls.Add(dateLabel);
newPanel.Controls.Add(textLabel);
newPanel.Controls.Add(Submitbutton);
newPanel.Controls.Add(textBox);
newPanel.Controls.Add(inputtedFood);
String myCalendar = monthCalendar1.SelectionRange.Start.ToShortDateString();
textLabel.Text = "Date:";
dateLabel.Text = myCalendar;
}
private void Button_Click(object sender, EventArgs e)
{
inputtedFood.Text = textBox.Text;
}
private void monthCalendar1_DateChanged_1(object sender, DateRangeEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
I tried the above code and was met with errors that are shown in the post.
Totally agree with both LarsTech and Ňɏssa Pøngjǣrdenlarp, you should be building a UserControl in place of the Panel and placing the TextBox, Button, and Label inside of that.
Your immediate question, though:
So what I want and have been struggling with is for me to enter
something into that textbox then to press the button to submit it and
then for it to show on the label.
Can be accomplished with this simple code:
Button Submitbutton = new Button();
// ... more code ...
Submitbutton.Click += (s2, e2) =>
{
inputtedFood.Text = textBox.Text;
};
Here's a little example showing it in action:
private void button1_Click(object sender, EventArgs e)
{
FlowLayoutPanel flp = new FlowLayoutPanel();
TextBox textBox = new TextBox();
// ... more code ...
Label inputtedFood = new Label();
inputtedFood.Text = "placeholder";
// ... more code ...
Button Submitbutton = new Button();
// ... more code ...
Submitbutton.Click += (s2, e2) =>
{
inputtedFood.Text = textBox.Text;
};
flp.Controls.Add(textBox);
flp.Controls.Add(Submitbutton);
flp.Controls.Add(inputtedFood);
flowLayoutPanel1.Controls.Add(flp);
}
The output:

Show blank date range on winforms but populate upon selecting date

I've got the first part working which is I want a date selection box to pop up with initial blanks in the date box. After selecting the dates I want them to be populated in the blank spaces. So far when I select the dates it still remains blanks
what I've tried so far is below; To be more specific the button click is where I'm expecting the date range to reappear.
DateTime dtpStartDate = new DateTime(1000, 1, 1);
DateTime dtpEndDate = new DateTime(1000, 1, 1);
Form frm = new Form();
frm.StartPosition = FormStartPosition.CenterScreen;
frm.Height = 160;
frm.Width = 300;
Label lbl = new Label();
lbl.Text = "Select Date Range";
lbl.AutoSize = true;
lbl.Width = 165;
lbl.Height = 13;
lbl.Font = new Font(FontFamily.GenericSansSerif, 8.25F, FontStyle.Bold);
lbl.Location = new Point(66, 10);
Label lblStartDate = new Label();
lblStartDate.Text = "Start Month/Year:";
lblStartDate.AutoSize = true;
lblStartDate.Location = new Point(13, 26);
Label lblEndDate = new Label();
lblEndDate.Text = "End Month/Year:";
lblEndDate.AutoSize = true;
lblEndDate.Location = new Point(165, 26);
DateTimePicker dtpStart = new DateTimePicker();
dtpStart.Location = new Point(12, 45);
dtpStart.Format = DateTimePickerFormat.Custom;
dtpStart.CustomFormat = " ";
dtpStart.Width = 120;
dtpStart.Value = first;
DateTimePicker dtpEnd = new DateTimePicker();
dtpEnd.Location = new Point(165, 45);
dtpEnd.Format = DateTimePickerFormat.Custom;
dtpEnd.CustomFormat = " ";
dtpEnd.Width = 120;
dtpEnd.Value = last;
Button btn = new Button();
btn.Text = "Submit";
btn.Location = new Point(100, 80);
btn.Click += (object sender, EventArgs e) =>
{
dtpStart.Enabled = true;
dtpStart.CustomFormat = "MMMM yyyy";
dtpEnd.Enabled = true;
dtpEnd.CustomFormat = "MMMM yyyy";
dtpStartDate = dtpStart.Value;
dtpEndDate = dtpEnd.Value;
frm.Close();
};
frm.Controls.Add(lbl);
frm.Controls.Add(lblStartDate);
frm.Controls.Add(lblEndDate);
frm.Controls.Add(dtpStart);
frm.Controls.Add(dtpEnd);
frm.Controls.Add(btn);
frm.Controls.Add(checkBoxBlanks11);
frm.ShowDialog();
I changed the format in the ValueChanged event and it worked. I took your code as it was, added the new handler. That's it.
{
...
dtpStart.ValueChanged += DtpStartValueChanged;
...
}
private void DtpStartValueChanged(object? sender, EventArgs e)
{
var dtpStart = (DateTimePicker)sender;
dtpStart.Format = DateTimePickerFormat.Short;
}
You are expecting to appear the dates on the button click, but you close the frm form in your button click handler.

C# navigating webbrowser from created combobox

I created visual web browser.
private void dodajKartę()
{
web = new WebBrowser();
web.DocumentCompleted += web_DocumentCompleted1;
web.Dock = DockStyle.Fill;
web.Visible = true;
web.ScriptErrorsSuppressed = true;
web.IsWebBrowserContextMenuEnabled = false;
pasek = new ToolStrip();
przyciskWstecz = new ToolStripButton();
przyciskDalej = new ToolStripButton();
pasekAdresu = new ToolStripComboBox();
przyciskZamknijKartę = new ToolStripButton();
przyciskMenu = new ToolStripDropDownButton();
wszystkiePrzyciskiMenu(); // this metod define menu buttons
WszystkoOPrzyciskach(); //this method define ToolStrip buttons
tabControl1.TabPages.Add("Nowa karta");
tabControl1.SelectTab(i);
tabControl1.SelectedTab.Controls.Add(web);
tabControl1.SelectedTab.Controls.Add(pasek);
i += 1;
}
I would like to navigate my browser from created ToolStripComboBox
This code not working:``
((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate((ToolStripComboBox)tabControl1.SelectedTab.Controls[10]).Text;
How can I change this code?

Iterating controls on button click in c# windows application

I am trying to create a windows application where I want to display a group of controls (Combo Box, Text Box and a button) on button click inside a panel.
I have created a code to create controls once but I want to create them again and again on button click one below another.
The code I am using is
public partial class Employee_PayHeads_add : Form
{
private TextBox txtBox = new TextBox();
private Button btnAdd = new Button();
private ComboBox combohead = new ComboBox();
public Employee_PayHeads_add()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.btnAdd.BackColor = Color.Gray;
this.btnAdd.Text = "Remove";
this.btnAdd.Location = new System.Drawing.Point(240, 25);
this.btnAdd.Size = new System.Drawing.Size(70, 25);
this.txtBox.Text = "";
this.txtBox.Location = new System.Drawing.Point(150, 25);
this.txtBox.Size = new System.Drawing.Size(70, 40);
this.combohead.Location = new System.Drawing.Point(10, 25);
panel1.Controls.Add(btnAdd);
panel1.Controls.Add(txtBox);
panel1.Controls.Add(combohead);
}
Also I want a vertical scroller in the panel if number controls overlap the space.
Thanks in advance
Inside the button click event create new objects, instead of using the one you declared before.
Try something like that:
public partial class Employee_PayHeads_add : Form
{
private TextBox txtBox = new TextBox();
private Button btnAdd = new Button();
private ComboBox combohead = new ComboBox();
private int txtBoxStartPosition = 150;
private int btnAddStartPosition = 240;
private int comboheadStartPosition = 10;
}
public Employee_PayHeads_add()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TextBox newTxtBox = new TextBox();
Button newBtnAdd = new Button();
ComboBox newCombohead = new ComboBox();
newBtnAdd.BackColor = Color.Gray;
newBtnAdd.Text = "Remove";
newBtnAdd.Location = new System.Drawing.Point(btnAddStartPosition, 25);
newBtnAdd.Size = new System.Drawing.Size(70, 25);
newTxtBox.Text = "";
newTxtBox.Location = new System.Drawing.Point(txtBoxStartPosition, 25);
newTxtBox.Size = new System.Drawing.Size(70, 40);
newCombohead.Location = new System.Drawing.Point(comboheadStartPosition, 25);
panel1.Controls.Add(newBtnAdd);
panel1.Controls.Add(newTxtBox);
panel1.Controls.Add(newCombohead);
txtBoxStartPosition += 50;
btnAddStartPosition += 50;
comboheadStartPosition += 50;
}
I havent tried your code yet, but it shows that it is always creating the new controls on every click event, but as youo have specified the hardcoaded location for buttons, so it must be creating new controls overlapping each other. so you can change the location dynamically and hopefully it will work
If you want to add the controls again and again you have to create new ones. So rather than defining them in your form like that you have to:
private void button1_Click(object sender, EventArgs e)
{
Button btnAdd = new Button();
btnAdd.BackColor = Color.Gray;
btnAdd.Text = "Remove";
btnAdd.Location = new System.Drawing.Point(240, 25);
btnAdd.Size = new System.Drawing.Size(70, 25);
TextBox txtBox = new TextBox();
txtBox.Text = "";
txtBox.Location = new System.Drawing.Point(150, 25);
txtBox.Size = new System.Drawing.Size(70, 40);
ComboBox combohead = new ComboBox();
combohead.Location = new System.Drawing.Point(10, 25);
panel1.Controls.Add(btnAdd);
panel1.Controls.Add(txtBox);
panel1.Controls.Add(combohead);
}
Now you can remove those private declarations on top of your class.

c# saving tabs using system.io;

I have the below code. I think I have pretty close to what I need. There is a main tab at startout (which does not contain tb, tb1, tb2, and tb3. Once I click the button, a tab is generated containing tb, tb1, tb2, tb3.
tb, tb1,tb2, and tb3 show errors of not existing. I simply cannot figure out how to get these saved.
public partial class Form1 : Form
{
public string status = "no";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string name = txtName.Text;
//validate information
try { }
catch { }
//create new tab
string title = name;
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);
//Add Labels
Label lb = new Label();
lb.Text = "Denomination:";
lb.Location = new System.Drawing.Point(150, 75);
lb.Name = "lbl";
lb.Size = new System.Drawing.Size(100, 20);
myTabPage.Controls.Add(lb);
Label lb2 = new Label();
lb2.Text = "Year:";
lb2.Location = new System.Drawing.Point(150, 120);
lb2.Name = "lbl2";
lb2.Size = new System.Drawing.Size(100, 20);
myTabPage.Controls.Add(lb2);
Label lb3 = new Label();
lb3.Text = "Grade:";
lb3.Location = new System.Drawing.Point(150, 165);
lb3.Name = "lbl3";
lb3.Size = new System.Drawing.Size(100, 20);
myTabPage.Controls.Add(lb3);
Label lb4 = new Label();
lb4.Text = "Mint Mark:";
lb4.Location = new System.Drawing.Point(150, 210);
lb4.Name = "lbl4";
lb4.Size = new System.Drawing.Size(100, 20);
myTabPage.Controls.Add(lb4);
//Add text boxes
TextBox tb = new TextBox();
tb.Location = new System.Drawing.Point(250, 75);
tb.Name = "txt";
tb.Size = new System.Drawing.Size(184, 20);
myTabPage.Controls.Add(tb);
TextBox tb1 = new TextBox();
tb1.Location = new System.Drawing.Point(250, 120);
tb1.Name = "txt1";
tb1.Size = new System.Drawing.Size(184, 20);
myTabPage.Controls.Add(tb1);
TextBox tb2 = new TextBox();
tb2.Location = new System.Drawing.Point(250, 165);
tb2.Name = "txt2";
tb2.Size = new System.Drawing.Size(184, 20);
myTabPage.Controls.Add(tb2);
TextBox tb3 = new TextBox();
tb3.Location = new System.Drawing.Point(250, 210);
tb3.Name = "txt3";
tb3.Size = new System.Drawing.Size(184, 20);
myTabPage.Controls.Add(tb3);
//put data inside of textboxes
tb.Text = txtCoin.Text;
tb1.Text = txtYear.Text;
tb2.Text = txtGrade.Text;
tb3.Text = txtMint.Text;
// Add delete button
Button bn = new Button();
bn.Location = new System.Drawing.Point(560, 350);
bn.Name = "btnDelete";
bn.Text = "Delete";
bn.Size = new System.Drawing.Size(100, 50);
bn.Click += MyClick;
myTabPage.Controls.Add(bn);
}
private void MyClick(object sender, EventArgs e)
{
Form2 myform = new Form2();
myform.ShowDialog();
if (status == "yes")
{ tabControl1.TabPages.Remove(tabControl1.SelectedTab); }
status = "no";
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
int counter;
int ccounter;
string outLine ;
string pathFileName = Path.Combine(Application.StartupPath, "coins.dat");
StreamWriter writeIt = new StreamWriter(pathFileName);
foreach (TabPage tabPage in tabControl1.TabPages)
{
if (tabControl1.TabCount > 1)
{
outLine = tabPage + tb.Text + tb1.Text + tb2.Text + tb3.Text + "\t";
writeIt.WriteLine(outLine);
}
if (tabControl1.TabCount == 1)
{
outLine = tabPage + "\t";
writeIt.WriteLine(outLine);
}
}
writeIt.Close();
}
}
}
You need to store tb1, etc in fields in your form so they can be accessed by other methods.
tb, tb1,tb2, and tb3 show errors of not existing.
Yes, they would - you're declaring them as local variables within button1_Click. To access them from other methods, you'll either need to just examine the controls within the tab page, or declare them as instance variables instead. However, in that case you'd need to consider the fact that there may be multiple tab pages.
It sounds like you really just need to iterate over the controls within each tab page, and pick out the textboxes. Either that, or perhaps create your own subclass of TabPage which knows about the textboxes. Then you could find each instance of your custom TabPage and ask it to save itself.

Categories

Resources