An open API service indexing awesome lists of open source software.

https://github.com/karenpayneoregon/dynamic-textboxs-csharp

Demonstrates creation of controls at runtime from SQL-Server using C#
https://github.com/karenpayneoregon/dynamic-textboxs-csharp

csharp sqlserver windows-forms

Last synced: about 2 months ago
JSON representation

Demonstrates creation of controls at runtime from SQL-Server using C#

Awesome Lists containing this project

README

          

# About

Demonstrates creating dynamic buttons with data using Microsoft SQL-Server, C#, .NET Framework 4.8.

---

![img](assets/figure1.png)

---

![img](assets/figure2.png)

---

# Core code

```csharp
public class CreateButtons
{
///
/// Size to create each button
///
///
public Size ButtonSize { get; set; }
///
/// Provides a reference to your buttons
///
///
public List Buttons { get; set; }
///
/// base name of buttone e.g. btn, cmd etc.
///
///
public string ButtonBaseName { get; set; }
///
/// Used to spread out buttons
///
///
public int Base { get; set; }
public int BaseAddition { get; set; }
///
/// Count of buttons
///
///
public int ButtonCount { get; set; }
///
/// control to place buttons onto
///
///
public Control ParentControl { get; set; }
///
/// Used here to push the current primary key back to the caller
///
public EventHandler ClickedHandler;
///
/// constructor for CreateButtonsFromTable
///
///
///
public CreateButtons(Control ParentControl, string BaseName)
{
this.ParentControl = ParentControl;
this.ButtonBaseName = BaseName;
}
///
/// Creates buttons with their names taken from fieldName with a prefix of Me.ButtonBaseName
/// and sets the tag of each button from Identifier which would be the primary key of a DataRow.
///
///
/// used to set tag for each button
/// used to set button text and button name
public void CreateButtonsFromTable(DataTable sender, string Identifier, string fieldName)
{

Buttons = new List();

ButtonCount = sender.Rows.Count - 1;

sender.AsEnumerable().Select((row) => new
{
ID = Convert.ToInt32(row[Identifier]),
Name = row[fieldName].ToString()
})
.ToList().ForEach((item) =>
{
Button b = new Button
{
Name = string.Concat(ButtonBaseName, item.Name),
Text = item.Name,
Tag = item.ID,
Size = ButtonSize,
Location = new Point(25, this.Base),
Parent = ParentControl,
Visible = true
};

b.Click += (object s, EventArgs e) => {
ClickedHandler(s, new IdentifierButtonEventArgs(Convert.ToInt32(((Button)s).Tag)));
};

ParentControl.Controls.Add(b);
Buttons.Add(b);
Base += BaseAddition;
});
}
///
/// Create buttons based on Me.ButtonCount
///
public CreateButtons()
{
Buttons = Enumerable.Range(0, ButtonCount).Select((Indexer) => {

Button b = new Button
{
Name = string.Concat(ButtonBaseName, Indexer + 1),
Text = (Indexer + 1).ToString(),
Size = this.ButtonSize,
Location = new Point(25, Base),
Parent =
ParentControl,
Visible = true
};

// alternate
//b.Click += new EventHandler(buttonIntregate);

b.Click += (object sender, EventArgs e) =>
{
Button tb = (Button)sender;
if (tb.Name == ButtonBaseName + "1")
{
tb.Text = "Got it";
}
else
{
MessageBox.Show(tb.Name);
}
};

this.ParentControl.Controls.Add(b);

Base += BaseAddition;

return b;

}).ToList();
}
}
```