So I made a program that contains a lot of textboxes and comboboxes for some calculations. The program has an Output button and an Input button. At the moment I have managed the Input through skipping lines and inserting the information like this:
ComboBoxes like this.
if (File.ReadLines(filename).Skip(23).Take(1).First() == "TEMP")
{
TEMP5.SelectedIndex = 2;
}
Textboes like this.
TEMP1.Text = File.ReadLines(filename).Skip(25).Take(1).First();
and the Output is just a massive block of Output containing newlines
System.IO.File.WriteAllText(filename,bla bla bla
Is there a better way?? There has to be.
EDIT: So one button "Output" saves all data contained in textboxes and comboboxes in a txt file.
The "Input" button makes you able to choose the txt file and copy the data into the boxes.
I usually find that if someone has to name boxes "Box1", "Box2", "Box3"... "Box99" then their doing something wrong. Unless each value has a specific, unique calculation that needs to be done with it, you can usually make things cleaner with a list.
In WPF, you can use ItemsControls like DataGrid to generate rows of TextBoxs that will link back to your list with data binding. If that looks like something that might fit, I would highly recommend looking into it. It will save you a lot of work in the long run.
That being said, if you do have a series of controls named consecutively ("Box1", "Box2", "Box3", etc), you can use FindName() to access them dynamically:
var outputBuilder = new StringBuilder();
for (int i = 1; i < 100; i++)
{
ComboBox box = (ComboBox)FindName("TEST" + i.ToString());
outputBuilder.AppendLine(box.Text);
}
System.IO.File.WriteAllText(filename, outputBuilder.ToString());
Related
I am trying to show a dropdown composed of several CheckBoxes (a list of CheckBox) in a DataGridView. Specifically, the DataGridView provide a DataGridViewComboBoxColumn that allows to have a dropdown with several items. However, if you try to add as a datasource of the column a list of checkboxes you will find out that the solution is not working (the checkboxes are not shown).
In the following link there is a solution on "regular" ComboBox: https://www.codeproject.com/Articles/31105/A-ComboBox-with-a-CheckedListBox-as-a-Dropdown
However, I need it for DataGridViewComboBoxColumn. Someone has an idea of what can be done to reach the goal? Thanks for answering (I link an example of the code below)
for (int i = 0; i < dataGridView.Rows.Count; i++)
{
// Put List of Checkboxes in DataGridViewComboBoxCell for each row of the Grid
((DataGridViewComboBoxCell) dataGridView.Rows[i].Cells[1]).DataSource = new List<CheckBox> { new CheckBox(), new CheckBox(), new CheckBox(), new CheckBox() };
}
I have modified DataGridViewColumns in past (OK, not entirely myself, according to some CodeProject cook book too :-) ), so I have some idea.
However this can't probably work in this case, there's a problem with this idea. You'd not only need to change how the cell looks, but also think how to store the data. How would you assign a separate datasource to each single DataGridViewComboboxCell? If possible, that would be probably quite extensive work, probably a class handling the structure and the events.
If I can offer a solution, you can have another object (i.e. another DataGridViw at the side with CheckBoxColumn and TextColumn which would load on DataGridView1.SelectionChanged), or you can populate it in a modal form on CellClick of that cell and feed back a list of selected items strings on close, etc. I did that in past many times and I don't think there's an easy way around it.
i'm making a vocabulary trainer via visual C#. I'm storing words in data grid view table, where I have colums
word
grammatic
translation
language
and unit.
I need to get all words (values from column called word) to be able to create some sort of test.
User will choose from which language (for example english) and from which unit (for example weather) he wants to be tested.
After pressing a button called Test, another form will be opened and the data in data grid view table will be filtered with two filters - language and unit (I managed to solve this problem). In the open form there will be two labels and a button. Instead of text of the first label there should be a word from the datagridview table, after pressing the button also translation should be shown - instead of text of the second label.
Could anyone help me how to do this?
I think something along the lines:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
Console.WriteLine(dataGridView1.Rows[i].Cells["cname"].Value.ToString());
}
I am hoping somebody can help me out here and that this questions won't be too vague.
I am trying to determine a way that I can highlight the differences between two text strings within a winform.
I have the original string in one column of a data grid and the string to compare to in a second column. What I would like is to highlight where the comparison string is different within the data grid view itself.
Example:
Thanks for the hep
Thanks for the help!!
where the l, and two ! would be highlighted
I have tried the following (Shout out to Bouam for his help in this Previous Post):
for (int i = 0; i < TextProcessingResults.RowCount; i++)
{
if (TextProcessingResults.Rows[i].Cells[1].Value != null)
{
if ((string)TextProcessingResults.Rows[i].Cells[1].Value != (string)TextProcessingResults.Rows[i].Cells[2].Value)
{
TextProcessingResults.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
}
}
}
But this only highlights which rows are different not at the character level. I'm new to all of this, so is this a crazy endeavor to undertake?
Could anybody help or point me towards a resource that would be useful?
You can create a new DatGridViewColumn based on RichTextBox and then use your new rich text column, to highlight differences in rich text.
This may help you:
RichTextBox Cell in a DataGridView
And here is an ouptput based on this:
You can apply the algorithm you need to compare strings for differences to columns and using this DataGridViewRichTextBoxColumn highlight the differences.
To learn more about creating custom datagridview column types you can take a look at Build a Custom NumericUpDown Cell and Column for the DataGridView Control.
I have a GUI Program, that creates labels containing names of files according to the search query provided by the user at runtime and displays them. The no of labels displayed can vary considerably based on the user input.
The no can vary between 0 to 2000(approx) . When the no of labels exceed 1000 or so, the time taken for the form in which the labels are contained takes a lot of time to display.
The time taken to display the form completely outweighs the time saved in making a good algorithm(for some cases)!. I am looking for an efficient way so that I can display the form easily in less amount of time. Currently, It takes 1minute 45 seconds to display a form containing 1499 Labels.
Currently , I am doing like this:-
foreach(string Elements in FileList)
{
Label LabelA = new Label() ;
// other code here to modify the appearence of label
LabelA.Show() ;
}
MyForm.Show();
// File List is the List of file names which are to be displayed.
//MyForm is the name of the form in which labels are added at runtime.
There could be at least a couple of techniques:
it's technically impossible that one can look on all of them contemporary, so solution is just devide them into Tabs. So divide them into groups of your application logic.
defered scrolling. So when user scrolls the window, only the data inside them changed (and also corresponding data field)
Just to give an idea, but basically to invite attention to a simple fact that you for sure do need all that controls at once on UI. If these solutions are not ok, change control, so rapresentation of data.
Asyn get result Maybe you can use BackGroundWorker to asynchronously query the result and create the label. The way will give you a well user experience .
Suspend then Resume When you add the label you should use Control.SuspendLayout() and Control.ResumeLayout()
Page the Result Other way you can page for the result.
If you want to use labels with that create labels on program startup and add them a list for instant access to them
// create a list on there
List<Label> lblList = new List<Label>;
public Form1()
{
InitializeComponent();
for(int i = 0 ; i<1500;i++)
{
Label LabelA = new Label() ;
// other code here to modify the appearence of label
LabelA.Show() ;
lblList.Add(LabelA);
}
}
if you do this on a backGroundWorker your program will open fast and you can make a prograss bar for label creation. this will create labels and add them a list. You can access them with their indexes and change their text like that:
int index=0;
foreach(Label o in lblList)
{
o.Text="Text";
index++;//you can use this index if you want to know which label you editing
}
if you create labels on start up this will take some time but when you use them this way will be faster from creating them and values will display instantly.
Sorry for bad english.
I am creating a windows form app with the following goal:
get a list of products, each with a unique name and a category (from an enumerated list) from a user (and then do some things after, but this is not relevant to the question).
The idea is I would like to have the user specify they would like to configure "n" products by entering a value in a text box. I have the event handler for the text box calling a method which sets a variable to this value n. This value, "n", will be used as the loop counter, or what have you - the point is it will create the bound for the number of boxes to create.
I would then like to add (dynamically based on n), n number of (text box / combo box) pairs to the form. If there is no room to add another (text box / combo box) pair below the last one created, it should create another column.
n is unbounded, but, realistically, will likely never exceed 20. In any event, I'd like to be able to handle it if there are more products than this.
the options in the combo box will be filled from a string list that is passed in at run time, but will be consistent per box, per instance of this Form application.
i tried to enter a mock up image but stack overflow won't let me until i have earned some reputation points :(
I understand how to create a number of boxes using something like the code below, but its the finer points i'm stuck on. Can anyone help?
thanks!
` private void Method1()
{
int boxes = Int32.Parse(NumProducts.Text);
for (int i = 0; i < boxes; i++)
{
TextBox tb = new TextBox();
tb.Location = new System.Drawing.Point(40, i * 20);
tb.Name = "TextBoxName" + i.ToString();
tb.Size = new System.Drawing.Size(184, 20);
tb.TabIndex = i + 2;
tb.Text = String.Empty;
panel1.Controls.Add(tb);
}
}
private void NumProducts_TextChanged(object sender, EventArgs e)
{
Method1();
}`
Sounds to me like a DataGridView would be the better choice here. You can configure it with a DataGridViewTextBoxColumn as the first column and a DataGridViewComboBoxColumn for the second. It supports a "new row" as the last item.
Read the docs. Drop one on a form and play with it.
Asking the user for the number of rows in advance is not very good from a usability viewpoint.
You should rather create an interface that keeps creating new boxes as the user inputs things, either by having a "new row" row that activates when the user types something into it (the empty row isn't saved) or by having a "new row" button.
To achieve the layout, use a FlowLayoutPanel control, and add the controls to this instead of to the panel as you are already doing. That should transparently take care of the columns issue, and add scroll bars if the user goes beyond your anticipated maximum number of edit boxes. General info on the FlowLayoutPanel here (as well as many others).