Expander Control - Read txt-Files in C# (WindowsForms) - c#

I‘m trying to add the second .txt file (book2) into Text Expander control, unfortunately without success.
public Form1()
{
InitializeComponent();
CreateAccordion();
}
private void CreateAccordion(string book1)
{
Accordion accordion = new Accordion();
accordion.Size = new Size(400, 350);
accordion.Left = 10;
Expander expander1 = new Expander();
expander1.BorderStyle = BorderStyle.FixedSingle;
ExpanderHelper.CreateLabelHeader(expander1, "Book1", SystemColors.ActiveBorder);
CreateContentLabel(expander1, book1, 140);
accordion.Add(expander1);
Expander expander2 = new Expander();
expander2.BorderStyle = BorderStyle.FixedSingle;
ExpanderHelper.CreateLabelHeader(expander2, "Book2", SystemColors.ActiveBorder);
CreateContentLabel(expander2, "book2", 120);
accordion.Add(expander2);
this.Controls.Add(accordion);
}
The following code reads only a single txt.file. Can anyone help me please?
private void CreateAccordion()
{
ReadTxtFiles();
}
private void ReadTxtFiles()
{
string path = #"C:\..\..\READBOOKS";
string[] files = new string[] { "book1.txt", "book2.txt" };
foreach (string file in files)
{
string fullPath = Path.Combine(path, file);
string booktxt = File.ReadAllText(fullPath);
string book1 = booktxt;
CreateAccordion(book1);
}
}

I‘m trying to add the second .txt file (book2) into Text Expander control, unfortunately without success.
The main reason behind this is because you're only passing the string from your first book, you'll need to pass all of the text.
private void ReadTxtFiles()
{
string path = #"C:\..\..\READBOOKS";
string[] files = new string[] { "book1.txt", "book2.txt" };
List<string> books = new List<string>();
foreach (string file in files)
{
string fullPath = Path.Combine(path, file);
string booktxt = File.ReadAllText(fullPath);
books.Add(booktxt);
}
CreateAccordion(books);
}
Change the signature of CreateAccordion:
private void CreateAccordion(List<string) books)
{
Accordion accordion = new Accordion();
accordion.Size = new Size(400, 350);
accordion.Left = 10;
Expander expander1 = new Expander();
expander1.BorderStyle = BorderStyle.FixedSingle;
ExpanderHelper.CreateLabelHeader(expander1, "Book1", SystemColors.ActiveBorder);
CreateContentLabel(expander1, books[0], 140);
accordion.Add(expander1);
Expander expander2 = new Expander();
expander2.BorderStyle = BorderStyle.FixedSingle;
ExpanderHelper.CreateLabelHeader(expander2, "Book2", SystemColors.ActiveBorder);
CreateContentLabel(expander2, books[1], 120);
accordion.Add(expander2);
this.Controls.Add(accordion);
}
Please note that I'm accessing index's here, you may want to check that the index exist before trying to access it. Also there's more way's than this, but should give you a better understanding to accomplish this.

Related

How to interact with buttons created using a foreach loop?

I want these buttons created using a foreach loop from a list of items to access the information that is stored in these items.
I used a foreach loop to create buttons from a list of items that represent files in a directory and those items hold links to these files. My goal is to make pressing these buttons open a dialog with these files to write a line in them.
public List<mov.Movie> movies = new List<mov.Movie>() { };
private void writeReportToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (string file in Directory.EnumerateFiles(#"C:\Users\sloup\OneDrive\Desktop\MovieList", "*.txt"))
{
var lines = File.ReadAllLines(file);
string movieName = lines[0];
if (movies.Select(x => x.Name).Contains(movieName))
{
}
else
{
string movieLength = lines[1];
string movieYear = lines[2];
string movieReport = lines[3];
movies.Add(new mov.Movie(movieName, movieLength, movieYear, movieReport, #"C:\Users\sloup\OneDrive\Desktop\MovieList" + movieName + ".txt"));
}
}
int X = 1;
int Y = 1;
foreach (var movie in movies)
{
var movie1Button = new Button();
movie1Button.Text = movie.Name;
movie1Button.Font = new Font("Calibri", 12);
movie1Button.ForeColor = Color.Black;
movie1Button.Padding = new Padding(6);
movie1Button.AutoSize = true;
this.Controls.Add(movie1Button);
movie1Button.Location = new System.Drawing.Point(20, 50 * X);
X++;
movie1Button.Click += Movie1Button_Click;
var movie1Label = new Label();
movie1Label.Text = movie.Year;
movie1Label.Font = new Font("Calibri", 12);
movie1Label.ForeColor = Color.Black;
movie1Label.Padding = new Padding(6);
movie1Label.AutoSize = true;
this.Controls.Add(movie1Label);
movie1Label.Location = new System.Drawing.Point(200, 50 * Y);
Y++;
}
}
private void Movie1Button_Click(object? sender, EventArgs e)
{
string[] lines1 = File.ReadAllLines();
var lines = File.ReadAllLines();
string movieName = lines[0];
string movieLength = lines[1];
string movieYear = lines[2];
}
public class Movie
{
public string Name;
public string Length;
public string Year;
public string Report;
public string Link;
public Movie(string movieName, string movieLength, string movieYear, string movieReport, string movieLink)
{
Name = movieName;
Length = movieLength;
Year = movieYear;
Report = movieReport;
Link = movieLink;
}
}
You can use the Tag property to attach the movie object to each button in the loop:
movie1Button.Tag = movie;
afterwards in the click event grab the button from the sender and cast the Tag-object back to a Movie
private void Movie1Button_Click(object? sender, EventArgs e)
{
Button button = sender as Button
(if button != null)
{
Movie movie = button.Tag as Movie;
// do what ever you like afterwards
}
}

Can I make a dynamic combo Box from a text file?

I want to make a combo box like this:
But the boxes should not be hardcoded they should come from a text file like this:
Addition of data in text file should result in addition of combo Boxes. Also each comboBox should have the same list of options in it which are 1,2,3,4
I made the following class to read and write the text file, but I couldn't find any resources in the internet to turn these text files to combo Box.
public static string ReadFromTextFile(string path)
{
if (File.Exists(path))
{
string data;
using (StreamReader r = new StreamReader(path))
{
data = r.ReadToEnd();
}
if (data != "")
{
data = "[" + data + "]";
}
return data;
}
return null;
}
public static void WriteToTextFile(string path, string data, bool append = true, int count = 1)
{
if (!File.Exists(path))
{
var file = File.Create(path);
file.Close();
}
using (StreamWriter writer = new StreamWriter(path, append: append))
{
if (!append)
{
//remove opening bracket "[" from data passed
data = data.Trim().Substring(1, data.Trim().Length - 1);
//remove last bracket "]" from data passed
data = data.Trim().Substring(0, data.Trim().Length - 1);
}
if (count != 0)
{
data = data + ",";
}
writer.WriteLine(data);
}
}
public static DataTable ConvertToDataTable<T>(IList<T> data)
{
PropertyDescriptorCollection properties =
TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
if (data != null)
{
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
}
return table;
}
I don't have visual studio available now. So, I'll give you the way forward.
Read the line and split it into string array.
string[] arr= line.Split(",");
The first one (say Food) is the heading and the remaining are values.
Loop through the array.
for (int i=1;i<= arr.Length;i++)
{
}
Add it to the combobox items like cbo.Items.Add(arr[i]).
Loop through the lines in the file and you get the desired output.
You can use the string "Food"/"Water" as the Name property of the ComboBox to identify the each ComboBox.
Besides, note that should set a different Location for each ComboBox.
private void buttonCreateComboBox_Click(object sender, EventArgs e)
{
int locationX = 50;
int locationY = 10;
string line;
System.IO.StreamReader file =
new System.IO.StreamReader(#"C:\Users\Administrator\Desktop\test.txt");
while ((line = file.ReadLine()) != null)
{
// Remove the extra ','
string comboName = line.Substring(0, line.Length - 1);
ComboBox comboBox = new ComboBox();
comboBox.Name = comboName;
comboBox.Items.AddRange(new object[] { 1, 2, 3, 4 });
comboBox.Location = new Point(locationX, locationY);
this.Controls.Add(comboBox);
Label label = new Label();
label.Text = comboName;
label.Location = new Point(0, locationY);
this.Controls.Add(label);
locationY += 30;
}
file.Close();
}
If you want to access a specific ComboBox, you can call Control.ControlCollection.Find(String, Boolean) Method to get it.
private void buttonGetComboWaterText_Click(object sender, EventArgs e)
{
ComboBox comboWater = (ComboBox)this.Controls.Find("Water", true)[0];
MessageBox.Show(comboWater.Text);
}
Without going into details (don't know why you need DataTable etc.) I will answer your main question from title.
This is how my textfile looks, no need for comma if you read line by line:
public void ReadFromTextFile(string path)
{
if (File.Exists(path))
{
using (StreamReader r = new StreamReader(path))
{
String line;
while ((line = r.ReadLine()) != null)
{
CreateComboBox(line.ToString());
}
}
}
}
public void CreateComboBox(string definition)
{
var combo = new ComboBox();
combo.Name = definition;
combo.Items.AddRange(new object[] { "1", "2", "3", "4" });
var label = new Label();
label.Text = definition;
this.flowLayoutPanel1.Controls.Add(label);
this.flowLayoutPanel1.Controls.Add(combo);
}
private void Form1_Load(object sender, EventArgs e)
{
ReadFromTextFile(#"c:\temp\MyTest.txt");
}
Use File.ReadAllLines(...) to short the txt read.
Point to control the position.
Attaches a delegate to SelectedIndexChanged that I imagine you will need for the next step.
private void Form1_Load(object sender, EventArgs e)
{
var lines = File.ReadAllLines(#"src.txt").Select(str => str.Replace(",", "")).ToList();
Label lbl, lastLbl = null;
ComboBox combo, lastCombo = null;
for (int i = 0; i < lines.Count(); i++)
{
lbl = new Label();
lbl.Text = lines[i];
if (i > 0) // adjust position
lbl.Location = new Point(lastLbl.Location.X, lastLbl.Location.Y + lastLbl.Height);
this.Controls.Add(lbl);
lastLbl = lbl;
combo = new ComboBox();
combo.DataSource = new List<int>() { 1, 2, 3, 4 };
if (i > 0) // adjust position
combo.Location = new Point(lastCombo.Location.X, lastCombo.Location.Y + lastCombo.Height);
else
combo.Location = new Point(lbl.Width + 5, 0);
//combo.SelectedIndexChanged += (s, a) => { }; // action you may need
this.Controls.Add(combo);
lastCombo = combo;
}
}

Load object from xml, edit values in a dynamic UI and save back to xml

I just read and applied it to the dynamic object on the panel for the container, the label for the title, the textbox for the contents.
but here I can't save it when I fill in the data in the textbox.
this is my deserialize xml code:
string Location= Path.Combine("D:\\Data\\Code.xml");
XmlDocument doc = new XmlDocument();
doc.Load(lokasiString);
foreach (XmlNode node in doc.DocumentElement)
{
string name = node.Attributes[0].InnerXml;
string value = node.InnerText;
// create panel
Panel panel = new Panel();
panel.Name = "panelImages";
panel.Size = new Size((int)(this.Width*0.9), 30);
panel.Dock = DockStyle.Top;
panel.BorderStyle = BorderStyle.FixedSingle;
Label l = new Label();
l.Text = name;
l.Font = new Font("Serif", 12, FontStyle.Bold);
l.ForeColor = Color.Black;
l.Size = new Size((int)(this.Width * 0.2), 30);
l.Dock = DockStyle.Left;
TextBox tb = new TextBox();
tb.Text = value;
tb.Font = new Font("Serif", 12, FontStyle.Bold);
tb.ForeColor = Color.Black;
tb.Size = new Size((int)(this.Width * 0.9), 30);
tb.Dock = DockStyle.Left;
panel.Controls.Add(tb);
panel.Controls.Add(lt);
panel.Controls.Add(l);
flowLayoutPanel1.Controls.Add(panel);
}
and this my Xml Code:
<resources>
<string name="name">Tap Crush</string>
<string name="mode">Slow</string>
<string name="score">12345</string>
</resources>
I have no prior knowledge of parsing Xml with C#.
Define model classes and use databinding to edit the model, then you can break the problem to the following pieces:
Defining a Model class containing a List<Resource> and each Resource having Title and Content.
Write some logic to load model from xml or save model to xml.
Write a piece of code to arrange UI and setup UI controls to use databinding to your mode.
Then you can easily Load data from xml, edit in the UI and Save data to xml.
Model classes
You can model classes like this:
public class Model
{
public List<Resource> Resources { get; set; }
}
public class Resource
{
public string Title { get; set; }
public string Content { get; set; }
}
Setting up UI
There are different approaches to dynamically show a collection of controls in a form. Here I'll show how you can do that using a DataGridView as well as a TableLayoutPanel:
DataGridView
TableLayoutPanel
Create DataGridView
var dg = new DataGridView();
dg.Dock = DockStyle.Fill;
dg.BorderStyle = BorderStyle.None;
dg.GridColor = Color.Black;
dg.AutoGenerateColumns = true;
dg.EditMode = DataGridViewEditMode.EditOnEnter;
dg.DataSource = model.Resources;
dg.DataBindingComplete += (o, a) =>
{
dg.RowHeadersVisible = dg.ColumnHeadersVisible = false;
dg.AllowUserToResizeColumns = false;
dg.AllowUserToResizeRows = false;
dg.BackgroundColor = SystemColors.Control;
dg.Columns[0].ReadOnly = true;
dg.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dg.Columns[0].DefaultCellStyle.ForeColor = Color.Black;
dg.Columns[0].DefaultCellStyle.BackColor = SystemColors.Control;
dg.Columns[0].DefaultCellStyle.SelectionForeColor = Color.Black;
dg.Columns[0].DefaultCellStyle.SelectionBackColor = SystemColors.Control;
dg.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
};
this.Controls.Add(dg);
Create TableLayoutPanel
var tlp = new TableLayoutPanel() { ColumnCount = 2, AutoSize = true };
tlp.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
tlp.SuspendLayout();
foreach (var resource in model.Resources)
{
tlp.RowStyles.Add(new RowStyle(SizeType.AutoSize));
var lbl = new Label() { AutoSize = true, Margin = new Padding(4) };
lbl.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
lbl.DataBindings.Add(new Binding(nameof(Label.Text), resource, nameof(Resource.Title)));
var txt = new TextBox();
txt.DataBindings.Add(new Binding(nameof(TextBox.Text), resource, nameof(Resource.Content)));
txt.Dock = DockStyle.Fill;
tlp.Controls.AddRange(new Control[] { lbl, txt });
}
tlp.ResumeLayout();
this.Controls.Add(tlp);
Load and Save Model
You can create a class like this:
public class ModelFactory
{
public Model FromXmlString(string xml)
{
return new Model()
{
Resources = XElement.Parse(xml).Elements()
.Select(x => ResourceFromXElement(x)).ToList()
};
}
public string ToXmlString(Model model)
{
return new XElement("resources",
model.Resources.Select(x => ResourceToXElement(x)).ToArray())
.ToString();
}
private Resource ResourceFromXElement(XElement element)
{
return new Resource()
{
Title = element.Attribute("name").Value,
Content = element.Value
};
}
private XElement ResourceToXElement(Resource resource)
{
return new XElement("string",
new XAttribute("name", resource.Title),
resource.Content);
}
}
Then easily load and save model:
Model model;
ModelFactory modelFactory = new ModelFactory();
private void loadButton_Click(object sender, EventArgs e)
{
var xml = #"
<resources>
<string name=""name"">Tap Crush</string>
<string name=""mode"">Slow</string>
<string name=""score"">12345</string>
</resources>";
//Load model from xml
model = modelFactory.FromXmlString(xml);
//Setup UI
}
private void saveButton_Click(object sender, EventArgs e)
{
//Save model to xml
var xml = modelFactory.ToXmlString(model);
MessageBox.Show(xml);
}

Put doubleclick event to multiple pushpins from a text file in bing maps wpf

I have no idea how to put doubleclick events to multiple pushpins that are initialized in the program itself(not in the xaml). The program will get lat and long data from a text file.
here's my current code:
public partial class BingMaps : Window
{
string role;
string nick;
int countLines=0;
public BingMaps(string value,string role2)
{
InitializeComponent();
nick = value;
role = role2;
setCount();
int cLines = 0;
Pushpin[] pushpins = new Pushpin [countLines];
StreamReader z = new StreamReader("dorms.txt");
while (z.Peek() > 0)
{
var pushpinLayer = new MapLayer();
pushpinLayer.Name = "PushPinLayer";
intraMap.Children.Add(pushpinLayer);
string line = z.ReadLine();
string[] temp = line.Split(new char[] { ';' });
var location = new Location(double.Parse(temp[3]), double.Parse(temp[4]));
var pushpin = new Pushpin();
pushpin.Name = "MyNewPushpin";
pushpin.Background = new SolidColorBrush(Colors.Blue);
pushpin.Location = location;
pushpin.ToolTip = "" + temp[0];
pushpins[cLines] = pushpin;
pushpinLayer.Children.Add(pushpins[cLines]);
cLines = cLines + 1;
}
z.Close();
}
public void setCount()
{
using (StreamReader cRead = new StreamReader("dorms.txt"))
{
while (cRead.Peek() > 0) {
cRead.ReadLine();
countLines = countLines + 1;
}
}
}
}
i'm quite new to c# so please bear with me. I need to display different data for each pushpin.
you can register to MouseDoubleClick event
var pushpin = new Pushpin();
pushpin.MouseDoubleClick += (sender, ea) => {
// do something
};

Hiding a PDF text by adding an extra layer

i need to hide a text by adding a new layer over the text i need to hide.
public void ReplacePDFText(string strSearch, StringComparison scCase, string strSource, string strDest)
{
PdfContentByte pCont = null;
if (File.Exists(strSource)) {
PdfReader pdfFileReader = new PdfReader(strSource);
using (PdfStamper psStamp = new PdfStamper(pdfFileReader, new FileStream(strDest, FileMode.Create))) {
for (int intCurrPage = 1; intCurrPage <= pdfFileReader.NumberOfPages; intCurrPage++) {
LocTextExtractionStrategy Strategy = new LocTextExtractionStrategy();
pCont = psStamp.GetUnderContent(intCurrPage);
Strategy.UndercontentCharacterSpacing = pCont.CharacterSpacing;
Strategy.UndercontentHorizontalScaling = pCont.HorizontalScaling;
string currText = PdfTextExtractor.GetTextFromPage(pdfFileReader, intCurrPage, Strategy);
List<iTextSharp.text.Rectangle> lstMatches = Strategy.GetTextLocations(strSearch, scCase);
PdfLayer pdLayer = default(PdfLayer);
pdLayer = new PdfLayer("over", psStamp.Writer);
pCont.SetColorFill(BaseColor.BLACK);
foreach (Rectangle rctRect in lstMatches) {
pCont.Rectangle(rctRect.Left, rctRect.Bottom, rctRect.Width, rctRect.Height);
pCont.Fill();
}
}
}
pdfFileReader.Close();
}
}
The problem with the approach above, is that the layer is added successfully with black color. So instead of the text i have a beautiful black line over the text.
But if i set the pCont.SetColorFill(BaseColor.BLACK) to WHITE, the text is still displayed.
How can i overcome this issue?
Instead of:
pCont = psStamp.GetUnderContent(intCurrPage);
Use:
pCont = psStamp.GetOverContent(intCurrPage);

Categories

Resources