Split the content of textbox into respective individual textboxes at run time in c#? - c#

I want the text contents of a TextBox to split into respective individual Textbox carrying a single character using c# windows application form.
Eg : A single Textbox containing text as-[Orange]
Expected output:-
in 20 separate textboxes
[o][r][a][n][g][e][][][][][][][][][][][][][][]
Till now I have done this..
`string name = textBox1.Text;
string str = name;
int chunkSize = 1;
Int32 stringLength = str.Length;
for (int i = 0; i < stringLength; i += chunkSize)
{
TextBox txtbox = new TextBox();
//if (i + chunkSize > stringLength)
//chunkSize = stringLength - i;
string singlechar = str.Substring(i, chunkSize);
txtbox.TextAlign = HorizontalAlignment.Center;
txtbox.BorderStyle = BorderStyle.FixedSingle;
txtbox.Font = new Font(txtbox.Font, FontStyle.Bold);
txtbox.Text = singlechar;
//txtbox.MaxLength = 1;
int a = 30;
int x = (i + 10) * a;
txtbox.Text = txtbox.Text.ToUpper();
txtbox.Location = new System.Drawing.Point(x, 100);
txtbox.BackColor = Color.White;
txtbox.Size = new System.Drawing.Size(30, 20);
this.Controls.Add(txtbox);
}`

I think you want this:-
textBox1 = mainTextBox.Text[0] ;
textBox2 = mainTextBox.Text[1] ;
// and so on..

assuming other TextBoxs in Panel named panel1 you can write something like :
for (int i = 0; i < textBox1.Text.Length; i++)
((TextBox)panel1.Controls[i]).Text = textBox1.Text[i].ToString();

Related

Is it possible to write into a RichTextBox at a given line, column?

I have a WinForm with a RichTextBox.
I am trying to write into the RichTextBox at a given line and column:
Here is the code:
public Form1()
{
InitializeComponent();
richTextBox1 = new RichTextBoxWithMouseSelectionFixed();
richTextBox1.Location = new System.Drawing.Point(62, 46);
richTextBox1.Name = "richTextBox1";
richTextBox1.Size = new System.Drawing.Size(461, 391);
richTextBox1.TabIndex = 0;
richTextBox1.Text = "";
richTextBox1.HideSelection = false;
Controls.Add(richTextBox1);
int line = 3;
int column = 5;
GoToLineAndColumn(richTextBox1, line, column);
}
private RichTextBoxWithMouseSelectionFixed richTextBox1;
private void GoToLineAndColumn(RichTextBox richTextBox1, int line, int column)
{
int offset = 0;
for (int i = 0; i < line - 1 && i < richTextBox1.Lines.Length; i++)
{
offset += richTextBox1.Lines[i].Length + 1;
}
richTextBox1.Focus();
richTextBox1.Select(offset + column, 0);
}
I tryed to write with:
richTextBox1.AppendText("currentWord");
and also with:
richTextBox1.Text = "currentWord";
but in both cases the currentWord is written in the first line at column 0.
public Form1()
{
InitializeComponent();
int line = 6;
int column = 30;
RichTextBox richTextBox1 = new RichTextBox();
richTextBox1.Location = new System.Drawing.Point(62, 46);
richTextBox1.Name = "richTextBox1";
richTextBox1.Size = new System.Drawing.Size(461, 391);
richTextBox1.TabIndex = 0;
richTextBox1.Text = "";
richTextBox1.HideSelection = false;
Controls.Add(richTextBox1);
for (int i = 0; i < line - 1; i++)
richTextBox1.Text += "\n";
for (int i = 0; i < column - 1; i++)
richTextBox1.Text += " ";
richTextBox1.Text += "Just Text";
}
The RichTextBox class has properties you can use to write text to a specific index in it. first you define the zero based index of the line and column you would like to write to and then you get the index of the first character in that line like below
// define the index of the line and the column of the line you want to write to in the rich text box
int lineIndex = 2;
// zero-based index of the line
int columnIndex = 10;
// Get the index of the first character in that line you specified above
int index = richTextBox.GetFirstCharIndexFromLine(lineIndex) + columnIndex;
//declare the string you wish to paste
string mytext = "Write to this section only";
//Use the SelectionStart and SelectionLength properties to write the text
richTextBox.SelectionStart = index;
richTextBox.SelectionLength = mytext.Length;
// clear any previous selection
richTextBox.SelectedText = mytext;
The only solution I found is by adding empty lines before the text and blank characters before the text:
private void WriteAtLineAndColumn(RichTextBox richTextBox1, string text, int line, int column)
{
// Insert blank lines at the beginning of the RichTextBox.
for (int i = 0; i < line - 1; i++)
{
richTextBox1.AppendText(Environment.NewLine);
}
int index = richTextBox1.GetFirstCharIndexFromLine(line - 1);
richTextBox1.Select(index, 0);
string spaces = new string(' ', column);
richTextBox1.SelectedText = spaces + "This is a sample text";
// Insert the text at the desired position.
richTextBox1.Select(richTextBox1.GetFirstCharIndexFromLine(line - 1) + column, 0);
richTextBox1.SelectedText = text;
}

C# Write each array element to different textbox on form

Hi all hoping for some direction. I have a text file that I am reading into a C# Form as a simple string array. I want to take each element and put them into their own text box on the form. I know I can do it as
textBox1.Text = myArray[0];
textBox2.Text = myArray[1];
and so forth but I was hoping for some sort of for or foreach statement to iterate through the array and textboxes.
For reference the form has 50 boxes on it and the array will always have 50 elements.
Thanks in advance!
Update, I appreciate both comments it got me pointed in the right direction. I was really struggling to think through it. What I ended up coming up with is:
//Read Data
string[] lines = File.ReadAllLines(Globals.savePath);
//Write Data To Form
for (int i = 1; i <= 20; i++)
{
TextBox textBox = (TextBox)groupBox1.Controls["textBox" + i];
textBox.Text = lines[i-1];
}
for (int i = 21; i <= 40; i++)
{
TextBox textBox = (TextBox)groupBox2.Controls["textBox" + i];
textBox.Text = lines[i - 1];
}
for (int i = 41; i <= 60; i++)
{
TextBox textBox = (TextBox)groupBox3.Controls["textBox" + i];
textBox.Text = lines[i - 1];
}
for (int i = 61; i <= 80; i++)
{
TextBox textBox = (TextBox)groupBox4.Controls["textBox" + i];
textBox.Text = lines[i - 1];
}
I am sure there is a cleaner way to do this. But this worked for me and was much better then doing the whole
textbox1.Text = myArray[0]
Plus it had the added benefit of translating over when to the next part where I was editing text boxes and than writing back to the text file.
string[] lines = new string [81];
for (int i = 1; i <= 20; i++)
{
TextBox textBox = (TextBox)groupBox1.Controls["textBox" + i];
lines[i - 1] = textBox.Text;
}
for (int i = 21; i <= 40; i++)
{
TextBox textBox = (TextBox)groupBox2.Controls["textBox" + i];
lines[i - 1] = textBox.Text;
}
for (int i = 41; i <= 60; i++)
{
TextBox textBox = (TextBox)groupBox3.Controls["textBox" + i];
lines[i - 1] = textBox.Text;
}
for (int i = 61; i <= 80; i++)
{
TextBox textBox = (TextBox)groupBox4.Controls["textBox" + i];
lines[i - 1] = textBox.Text;
}
lines[81] = "End";
File.Delete(Globals.savePath);
File.WriteAllLines(Globals.savePath, lines);
Once again Thank You to those that commented!
This is my first attempt at an answer, so fingers crossed this is helpful. I would store the textboxes in a list, then when you loop through your array, just do something like the following:
var maxItems = myArray.Length;
for(int loop = 0; loop < maxItems; loop++)
{
textBoxItemList[loop].Text = myArray[loop];
}
This could be tweaked to determine the loop size using the smallest array size, so you didn't go out of bounds. The maxItems is there just as an example of where you would set this up.
Hope that helps.

Iterate through list of labels and assign text through the code behind

The labels aren't populating with text and I'm not sure why. I need lbl0 to lbl6 filled with an incremented year.
int year= Convert.ToInt32(hdnYear.Value);
List<Label> lbl = new List<Label>();
for (int i = 0; i < 6; i++)
{
Label lbls = new Label();
lbl.Add(lbls);
int yearValue = (FY + i);
string lblID = "lbl" + i;
lbl[i].ID = lblID;
lbl[i].Text = yearValue.ToString();
}
you want to do something like this:
List<Label> labels = new List<Label>();
for(int i = 0; i < 6; i++)
{
Label newLabel = new Label();
//set Properties of newLabel like ID, Content etc
labels.Add(newLabel);
}
fyi: A TextBlock has a Text property, a Label has Content property

Issue with Creating List of Link Labels

I am using code as below to create a list of link labels :
LinkLabel[] lnkArray = new LinkLabel[10];
for (int i = 0; i < 10; i++)
{
lnkArray[i] = new LinkLabel();
lnkArray[i].Text = "test" + i;
lnkArray[i].Location = new System.Drawing.Point(20 + (i + 5), 50);
lnkArray[i].Size = new Size(200, 25);
}
panel1.Controls.AddRange(lnkArray);
Here is a image of the result :
It looks good to me but this always makes one linklabel in the panel with text = test0 .So basically it is adding just the first one in the list any solution ?
There is no problem with AddRange.
The problem in your code is that the LinkLabel(s) is overlapping.
The width of the LinkLabel in your code is 200. Therefore, you should leave at least 200px gap between the labels.
Try changing your code to this:-
LinkLabel[] lnkArray = new LinkLabel[10];
for (int i = 0; i < 10; i++)
{
lnkArray[i] = new LinkLabel();
lnkArray[i].Text = "test" + i;
lnkArray[i].Location = new System.Drawing.Point(20 + (i + 200), 50);
lnkArray[i].Size = new Size(200, 25);
}
panel1.Controls.AddRange(lnkArray);
simply use this instead of array
for (int i = 0; i < 10; i++)
{
LinkLabel lnkLbl = new LinkLabel();
// add properties i.e Text , Location , size
panel1.Controls.Add(lnlLbl);
}

Dynamic UI, Loop not working correctly

Here is the code I'm using
It's supposed to create labels for each module name entered by a user and all the assessment names for that module under it. Should work with any number of modules and assessments. The problem is that it only shows assessments for the last module displayed.
dat.Modules and dat.Assessments are arraylists, each of them holds 4 elements with info about a module or an assessments, that is why i divide the count by 4.
private void testingButton_Click(object sender, EventArgs e)
{
int pos1 = 50;
int pos2 = 150;
int modLength = dat.Modules.Count;
modLength = modLength / 4;
int assessLength = 0;
int arrayData = 0;
int displayCount = 0;
for (int i = 0; i < modLength; i++)
{
this.moduleLabels.Add(new Label());
System.Drawing.Point pLabel1 = new System.Drawing.Point(50, pos1);
(moduleLabels[i] as Label).Location = pLabel1;
(moduleLabels[i] as Label).Size = new System.Drawing.Size(100, 13);
(moduleLabels[i] as Label).Text = dat.Modules[arrayData].ToString();
tabPage5.Controls.Add((moduleLabels[i] as Label));
String asd = dat.Modules[arrayData + 2].ToString();
Console.WriteLine(asd);
assessLength = int.Parse(asd);
pos2 = pos1 + 25;
for (int y = 0; y < assessLength; y++)
{
this.assessLabels.Add(new Label());
System.Drawing.Point pLabel2 = new System.Drawing.Point(70, pos2);
(assessLabels[y] as Label).Location = pLabel2;
(assessLabels[y] as Label).Size = new System.Drawing.Size(250, 13);
(assessLabels[y] as Label).Text = dat.Assessments[displayCount + 1].ToString() + " weights " + dat.Assessments[displayCount+2].ToString() +"%, Enter your mark:";
textboxComputer.Add(new TextBox());
System.Drawing.Point pText1 = new System.Drawing.Point(400, pos2);
(textboxComputer[y] as TextBox).Location = pText1;
(textboxComputer[y] as TextBox).Size = new System.Drawing.Size(20, 20);
tabPage5.Controls.Add(assessLabels[y] as Label);
tabPage5.Controls.Add(textboxComputer[y] as TextBox);
pos2 = pos2 + 25;
displayCount = displayCount + 4;
}
pos1 = pos2+25;
arrayData = arrayData + 4;
}
}
this is an example of what it displays
http://dc540.4shared.com/download/YI8IENYI/tsid20120501-211723-cbc785f9/asd.jpg
The first two modules should have their assessments listed. The first one doesn't display any. For Java it only displays the last one, out of 3 total for that module. And for the last Module "Another Module" it displays all assessments.
For each increment of i, y starts at 0. You then add new labels to assessLabels, but attempt to access the one you added by using assessLabels[y] which would usually yield the labels created for the previous value of i. This causes labels created for the first module to be reused by the next, and so forth.
A quick solution is not to use assessLabels[y] but assessLabels[assessLabels.Count - 1].
A better solution is to create a local variable for the labels, set their properties, and then add them to the list:
for (int y = 0; y < assessLength; y++)
{
Label assessLabel = new Label();
assessLabel.Location = ...;
// etc.
tabPage5.Controls.Add(assessLabel);
assessLabels.Add(assessLabel);
}
This would also remove the need to continuously cast the ArrayList members and unneeded access to the list.
PS. If assessLabels only contains objects of type Labels, consider using a List<Label> instead.

Categories