Switching to dynamically created TabPage on button click - c#

I'm practicing my coding and am trying to create a till by Dynamically prefill the items that are stored in a text file. I can dynamically create Tabs, Buttons and create menu buttons based on the categories of items. Where I'm struggling is I'm trying to switch the Tab on a button click. The Tabs are given a Name, which is the category ID, and the Text displays the category. In the event that trys to switch the tab I get the following error:
Error CS0266 Cannot implicitly convert type 'object' to 'System.Windows.Forms.TabPage'. An explicit conversion exists (are you missing a cast?)
I'm presuming that I need to Create a tab page or something, but I can't find out how to do this. Been on it for hours! Here's my code....
public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
string[] loadedFile = File.ReadAllLines(#"h:\shopItemsV2.txt");
foreach (string line in loadedFile)
{
// Split string and create required variables
string[] newBtnData = line.Split(',');
int catID = int.Parse(newBtnData[0]);
string cat = newBtnData[1];
string item = newBtnData[2];
double price = double.Parse(newBtnData[3]);
// Create tab if needed
if (tabControl1.TabCount < catID)
{
TabPage tp = new TabPage()
{
Text = cat,
Name = catID.ToString()
};
tabControl1.TabPages.Add(tp);
// Add FlowLayoutPanel with unique name
FlowLayoutPanel fp = new FlowLayoutPanel()
{
Name = "fp" + catID.ToString(),
Dock = DockStyle.Fill
};
// Add FlowLayoutPanel to correct Tab
tabControl1.TabPages[catID-1].Controls.Add(fp);
// Create Button for menu
Button newMenuBtn = new Button()
{
Name = cat + "Btn",
Tag = catID,
Width = 100,
Height = 50,
Text = cat,
};
newMenuBtn.Click += SwitchTab;
menuFP.Controls.Add(newMenuBtn);
}
// Create Button
Button newBtn = new Button()
{
Name = item,
Tag = price,
Width = 100,
Height = 100,
Text = item,
};
newBtn.Click += AddItem;
//Add button to correct tab
foreach (TabPage tabP in tabControl1.TabPages)
{
if (tabP.Name == catID.ToString())
{
Control fp = this.Controls.Find("fp"+catID, true).First();
fp.Controls.Add(newBtn);
}
}
}
}
private void SwitchTab(object sender, EventArgs e)
{
// Create button, switch to required Tab
// Tabs named the same as Tag on the button
Button clickedBtn = sender as Button;
tabControl1.SelectedTab = clickedBtn.Tag;
}
}
Any help would be greatly appreciated.

You can store anything in the Tag() property of your button. With that in mind, store a reference to your TabPage!
Change:
// Create Button for menu
Button newMenuBtn = new Button()
{
Name = cat + "Btn",
Tag = catID,
Width = 100,
Height = 50,
Text = cat,
};
To:
// Create Button for menu
Button newMenuBtn = new Button()
{
Name = cat + "Btn",
Tag = tp; // store the reference to the TabPage you created
Width = 100,
Height = 50,
Text = cat,
};
Then the suggestion by Uwe Keim should work:
tabControl1.SelectedTab = clickedBtn.Tag as TabPage;

private void AddNewPr_Click(object sender, EventArgs e)
{
TabPage tab = new TabPage();
_list = new ListBox();
_list2 = new ListBox();
PictureBox picBox = new PictureBox();
picBox.Click = picBox_Click;
//More stuff here
//Add the controls
tabControl1.Controls.Add(tab);
tab.Controls.Add(list);
tab.Controls.Add(list2);
tab.Controls.Add(pictureBox);
}

Related

Repositioning element on the form

I'm building that To-Do list app and I'm trying to create a "Add New task" button.
I wanted to create a new Task (TextBox) on the button click and also move the button bellow the TextBox added. So, I tried to create multiple panels and when the button was clicked, move the "New Task" button to the second panel and add the new textbox created to the first panel. And after that if the user wanted to remove that Task (TextBox), to repositionate the button and Tasks according to that.
Can someone please help me with that??
`
private void btnNewTask_Click(object sender, EventArgs e)
{
// Creating new Textbox
TextBox txtTask = new TextBox();
btnNewTask.Location = new Point(panel2);
// txtTask parameter
txtTask.BorderStyle = BorderStyle.None;
txtTask.ReadOnly = false;
txtTask.PlaceholderText = "Input a new Task.";
txtTask.Location = new Point(panel1);
//Adding txtTask.
this.Controls.Add(txtTask);
}
`
I founded The Solution. I deleted the panels and leaved the btnNewTask on the form.
On button click I needed to create and set the txtTask location as the btnNewTask location and change the Y location of btnNewTask to move it bellow. After that check if there was any other control that had the same or less Y location than txtTask, and if there was any, add 35 to his Y location too.
There is my new Code:
int c = 0;
private void btnNewTask_Click(object sender, EventArgs e)
{
// Creating new Textbox
TextBox txtTask = new TextBox();
// txtTask parameter
Font font = new Font("Segoe UI", 10.0f);
txtTask.Name = "txtTask" + ++c;
txtTask.BorderStyle = BorderStyle.None;
txtTask.ReadOnly = false;
txtTask.PlaceholderText = "Input a new Task.";
txtTask.Size = new Size(817, 31);
txtTask.Font = font;
txtTask.Location = new Point(btnNewTask.Location.X , btnNewTask.Location.Y); // txtTask will get the "btnNewTask" Location
btnNewTask.Location = new Point(txtTask.Location.X , btnNewTask.Location.Y + 35); // That should add 35 to the Y location
foreach (Control item in this.Controls)
{
if (item.Location.Y >= txtTask.Location.Y)
{ // if there is an item that has greater Y location
item.Location = new Point(item.Location.X, txtTask.Location.Y + 35); // It should increase its value as 35 too.
}
this.Controls.Add(txtTask);
}
There's an example:
Thank for the help and ideas!

Check all items in a specified checked list box c#

I have created a small kitchen display program that display food orders. So I created dynamically a panel that contains a table layout panel that contains a checked list box and a check all button . My problem is... I have a check all button in each table layout panel created dynamically and every time I click it, it checks all items in the last created CheckedListBox not the clicked one.
This is my code:
p = new Panel();
p.Size = new System.Drawing.Size(360, 500);
p.BorderStyle = BorderStyle.FixedSingle;
p.Name = "panel";
tpanel = new TableLayoutPanel();
tpanel.Name = "tablepanel";
clb = new CheckedListBox();
tpanel.Controls.Add(b1 = new Button() { Text = "CheckAll" }, 1, 4);
b1.Name = "b1";
b1.Click += new EventHandler(CheckAll_Click);
b1.AutoSize = true;
private void CheckAll_Click(object sender, EventArgs e)
{
var buttonClicked = (Button)sender;
var c = GetAll(this, typeof(CheckedListBox));
for (int i = 0; i < c.Count(); i++)
{
\\any help
}
}
public IEnumerable<Control> GetAll(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetAll(ctrl, type)).Concat(controls).Where(c =>
c.GetType() == type);
}
First I will describe the struct
Order = TableLayoutPanel
TableLayoutPanel has 1 CheckAll Button and CheckListBox
And you want when you click to CheckAll Button it will checks exactly all items in current TableLayoutPanel.
So try this code
class XForm : Form {
// create Dictionary to store Button and CheckListBox
IDictionary<Button, CheckListBox> map = new Dictionary<Button, CheckListBox> ();
// when you create new order (new TableLayoutPanel)
// just add map Button and CheckListBox to map
private void CreateOrder () {
var panel = new Panel ();
panel.Size = new System.Drawing.Size (360, 500);
panel.BorderStyle = BorderStyle.FixedSingle;
panel.Name = "panel";
var table = new TableLayoutPanel ();
var checklistBox = new CheckedListBox ();
var button = new Button () { Text = "CheckAll" };
table.Controls.Add (button, 1, 4);
button.Name = "b1";
button.Click += new EventHandler (CheckAll_Click);
button.AutoSize = true;
map[button] = checklistBox;
}
// and on event handle
private void CheckAll_Click (object sender, EventArgs e) {
var buttonClicked = (Button) sender;
var c = map[buttonClicked];
if (c == null) return;
for (int i = 0; i < c.Items.Count; i++)
{
c.SetItemChecked(i, true);
}
}
}
And dont for get remove it from map when remove the order.
Hope it helps

Get Value Dynamic Textbox in C#

I made dynamic textboxes and keep it List
private void ConvertButton_Click(object sender, EventArgs e)
{
List<TextBox> textBoxes = new List<TextBox>();
foreach (Control item in this.Controls)
{
if (item is TextBox)
{
TextBox txt = item as TextBox;
textBoxes.Add(txt);
}
}
}
I get all of textboxes value but i have a problem. For examples; if user add 3 label like (A,B,C) and add Textboxes for them like (labelA has 2,labelB has 3,labelC has 1) and textboxes get value like array (textboxes[0] has value).The problem is i dont know which label has which value.
I added Textbox just like this:
private void addNewTextbox(object sender, EventArgs e)
{
Button button = (Button)sender;
List<TextBox> textBoxes = button.Tag as List<TextBox>;
if (textBoxes == null)
button.Tag = textBoxes = new List<TextBox>();
TextBox textBox = new TextBox();
textBoxes.Add(textBox);
textBox.Location = new Point(90 * textBoxes.Count, button.Top);
textBox.Size = new Size(50, 50);
this.Controls.Add(textBox);
}
I try to show screen for example;
LabelA-->Textbox1 , Textbox2
</br>
labelB -->Textbox3
</br>
LabelC --> Textbox4 , Textbox5 , TextBox6
Every control that is added has a Name property. Use this property to link the different controls together. For example (and you should come up with your own naming convention), you could do this:
LabelA --> TextboxA1, TextboxA2
LabelB --> TextboxB1
LabelC --> TextboxC1, TextboxC2, TextboxC3
If you wish to have a more complete (i.e. complex) solution, you could:
Create your own control that inherits from TextBox
Add a property for the name of the associated Label control
Set this property when a new control is instantiated
Label[] labelArray = new Label[10];
for (int i = 0; i < labelNumber; i++)
{
labelArray[i] = new Label();
labelArray[i].Text = states[i] + "-->";
this.Controls.Add(labelArray[i]);
labelArray[i].Top = 100 + i * 30;
labelArray[i].Left = 10;
labelArray[i].Width = 30;
}
i did label-created just like this.labelnumber is how many label user wants.#interceptwind
I am trying very hard to understand your question, I guess you are trying to link the user-created textboxes to specific labels? Forgive me if I am completely wrong. How about structure your code to something like this:
Note: Edited with OP's new code
Dictionary<Int, List<TextBox>> label_Textboxes_Dict = new Dictionary<Int, List<TextBox>>();
List<Label> labelArray = new List<Label>(); //I suggest use list as you don't know the array size beforehand
void addLabel(int labelNumber)
{
int currentLabelArrayCount = labelArray.count; //So that users can add multiple times
for (int i = currentLabelArrayCount; i < currentLabelArrayCount +labelNumber; i++)
{
labelArray.Add(new Label());
labelArray[i].Text = states[i] + "-->";
this.Controls.Add(labelArray[i]);
labelArray[i].Top = 100 + i * 30;
labelArray[i].Left = 10;
labelArray[i].Width = 30;
label_Textboxes_Dict.Add(i, new List<TextBox>());
}
}
void addTextBoxForLabel(int labelNum)
{
TextBox t1 = new TextBox();
TextBox t2 = new TextBox();
//etc...
if (label_Textboxes_Dict.ContainsKey(labelNum))
{
label_Textboxes_Dict[labelNum].Add(t1);
label_Textboxes_Dict[labelNum].Add(t2);
}
}
void doSomethingForAllTextboxesOfLabel(int labelNum)
{
List<TextBox> listOfTextBoxes;
if(label_Textboxes_Dict.TryGetValue(labelNum, out listOfTextBoxes))
{
foreach(TextBox tb in listOfTextBoxes)
{
//do your stuff
}
}
}

Creating a find dialog

So for this assignment in class I'm making a richtextbox editor in C# winforms, in this assignment I have to include a find function, but I can't seem to get the hang of it
EDIT: it closes every time I click the search button, but that's not what I want, I want it to close when the user closes the actual dialog via the X in the top right, the search button should just highlight the found string by using the .Find() method
Here's my code up until now:
private void zoekenToolStripMenuItem_Click(object sender, EventArgs e)
{
string searchValue = SearchDialog();
Search(searchValue);
}
public string SearchDialog()
{
Form findDialog = new Form();
findDialog.Width = 500;
findDialog.Height = 142;
findDialog.Text = "Zoeken";
Label textLabel = new Label() { Left = 10, Top = 20, Text = "Zoek naar:", Width = 100 };
TextBox inputBox = new TextBox() { Left = 150, Top = 20, Width = 300};
Button search = new Button() { Text = "Zoek", Left = 350, Width = 100, Top = 70 };
search.Click += (object sender, EventArgs e) => { findDialog.Close(); };
findDialog.Controls.Add(search);
findDialog.Controls.Add(textLabel);
findDialog.Controls.Add(inputBox);
findDialog.ShowDialog();
return (string)inputBox.Text;
}
void Search(string searchValue)
{
rtxtInhoud.Find(searchValue);
}
The part:
search.Click += (object sender, EventArgs e) => { findDialog.Close(); };
is what I'm really stuck on
Thanks in advance
EDIT: here's something that I tried to do, which didn't work
public string SearchDialog()
{
Form findDialog = new Form();
findDialog.Width = 500;
findDialog.Height = 142;
findDialog.Text = "Zoeken";
Label textLabel = new Label() { Left = 10, Top = 20, Text = "Zoek naar:", Width = 100 };
TextBox inputBox = new TextBox() { Left = 150, Top = 20, Width = 300};
Button search = new Button() { Text = "Zoek", Left = 350, Width = 100, Top = 70 };
Button findNext = new Button() { Text = "Volgende", Left 250, Width = 100, Top = 70};
search.Click += (object sender, EventArgs e) => { rtxtInhoud.Find(inputBox.Text); };
findDialog.Controls.Add(search);
findDialog.Controls.Add(textLabel);
findDialog.Controls.Add(inputBox);
findDialog.ShowDialog();
return (string)inputBox.Text;
}
This waits for the Dialog to be closed before highlighting the found string. Which is NOT what I want, I want it to keep the Dialog open, but still highlight the text
it closes every time I click the search button
Yes, that's because you added an event handler to the search button that closes the form:
search.Click += (object sender, EventArgs e) => { findDialog.Close(); };
but that's not what I want, I want it to [...]
Then write the code in that handler such that it does what you actually want it to do, instead of having it close the form.
In your main form code, create a method that searches your text and highlights a found string. Then hook this method up to your search dialog button like the sample below. Also, I think you should call form.Show() instead of .ShowDialog(), so the other form can respond to input.
private void HighlightString(string stringToHighlight)
{
// Code here to search your text and highlight a string.
}
private void SearchDialog()
{
var findDialog = new Form {Width = 500, Height = 142, Text = "Zoeken"};
var textLabel = new Label() {Left = 10, Top = 20, Text = "Zoek naar:", Width = 100};
var inputBox = new TextBox() {Left = 150, Top = 20, Width = 300};
var search = new Button() {Text = "Zoek", Left = 350, Width = 100, Top = 70};
search.Click += (object sender, EventArgs e) => HighlightString(inputBox.Text);
findDialog.Controls.Add(search);
findDialog.Controls.Add(textLabel);
findDialog.Controls.Add(inputBox);
findDialog.Show();
}
Use this code:
Application.Exit();

How to create windows forms dialog with dynamic number of buttons?

I need to create a sort of a dialog box or something like a popup screen. I have this array of items and then I need to create a buttons for each of them on the dialogbox so that i could navigate with a button click.
Whats the best way to do it in C#? can someone guide me on this please
If you are using winforms, then place FlowLayoutPanel on your form. Then add all controls to it at runtime.
foreach(var item in items)
{
Button button = new Button();
// setup button properties
// subscribe to events
flowLayoutPanel.Controls.Add(button);
}
FlowLayoutPanel will arrange your controls automatically.
Consider that you Dialog or similar parent element is called sp and ar is the array of elements that you want to use to create the buttons:
for(YourObject obj : ar)
{
System.Windows.Controls.Button newBtn = new Button();
newBtn.Content = obj.YourProperty;
newBtn.Name = "Button" + obj.YourProperty;
sp.Children.Add(newBtn);
}
I was playing with some concepts and one part of it seem to be exactly example of creating dynamic dialog. You can add dynamic button creation to it, combine it and properly format it using table or flow layout panel
DialogResult result;
using (var popup = new Form())
{
popup.Size = new Size(1000, 500);
popup.Location = new Point(Convert.ToInt32(this.Parent.Width / 2) - 500, Convert.ToInt32(this.Parent.Height / 2) - 250);
popup.FormBorderStyle = FormBorderStyle.FixedDialog;
popup.MinimizeBox = false;
popup.MaximizeBox = false;
popup.Text = "My title";
var lbl = new Label() { Dock = DockStyle.Top, Padding = new Padding(3), Height = 30 };
lbl.Font = new Font("Microsoft Sans Serif", 11f);
lbl.Text = "Do you want to Continue?";
// HERE you will add your dynamic button creation instead of my hardcoded
var btnYes = new Button { Text = "Yes", Location = new Point(700, 400) };
btnYes.Click += (s, ea) => { ((Form)((Control)s).Parent).DialogResult = DialogResult.Yes; ((Form)((Control)s).Parent).Close(); };
var btnNo = new Button { Text = "No", Location = new Point(900, 400) };
btnNo.Click += (s, ea) => { ((Form)((Control)s).Parent).DialogResult = DialogResult.No; ((Form)((Control)s).Parent).Close(); };
popup.Controls.AddRange(new Control[] { lbl, btnYes, btnNo });
result = popup.ShowDialog(this);
}
if (result == DialogResult.Yes)
{
// do this
}
else
{
// do that
}
This was example of how to create dynamic dialog with dialog result output.

Categories