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#
- Host: GitHub
- URL: https://github.com/karenpayneoregon/dynamic-textboxs-csharp
- Owner: karenpayneoregon
- Created: 2021-05-11T21:01:52.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-07-14T16:53:57.000Z (almost 5 years ago)
- Last Synced: 2025-01-29T00:29:55.599Z (over 1 year ago)
- Topics: csharp, sqlserver, windows-forms
- Language: TSQL
- Homepage:
- Size: 107 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# About
Demonstrates creating dynamic buttons with data using Microsoft SQL-Server, C#, .NET Framework 4.8.
---

---

---
# 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();
}
}
```