C# Array strings in a Group of Radio Buttons - c#

My code is C# windows form
I have a file with data:
David One And Two/Three
Alex One Two Four And Five/Six
Amanda Two Seven/Ten
Micheal Seven/Nine
and trying to have them in array like
string[] Names = File.ReadAllLines("C:\Students\Name.txt", Encoding.Default);
and have them in Group of Radio Buttons
RadioButton[] n = new RadioButton[Names.Length];
for (int i = 0; i < Names.Length; i++)
{
n[i] = new RadioButton();
n[i].Text = Names[i];
n[i].Location = new Point(10, 10 + i * 20);
groupBox1.Controls.Add(n[i]);
}
But it shows as my attached image
I tried without Encoding.Default and Encoding.UTF8 but the same problem.
What is I'am doing wrong? Please see my image and help me. Thank you in advance!

I'm guessing your file contains empty lines at the end. You can try to remove them before creating a radio button:
for (int i = 0; i < Names.Length; i++)
{
var name = Names[i];
if(name.Trim() == string.Empty) continue;
n[i] = new RadioButton();
n[i].Text = Names[i];
n[i].Location = new Point(10, 10 + i * 20);
groupBox1.Controls.Add(n[i]);
}
If your text is trimmed you can try to increase the width of the control:
n[i].Width = 300; // Put a value which will show the entire text

Related

c# Fill a listview with items from a richtextbox (each 5 lines into columns)

I have a list view and a richtextbox.
This richtextbox has data like below. And I want to input the first 5 lines in one row and 5 columns like below.
richtextbox example and List view example
I have the below code so far
for ( int i = 0; i < richTextBox1.Lines.Count(); i++)
{
string[] arry = new string[5];
ListViewItem itms;
arry[0] = richTextBox1.Lines[i];
arry[1] = richTextBox1.Lines[i+1];
arry[2] = richTextBox1.Lines[i + 1];
arry[3] = richTextBox1.Lines[i+2];
arry[4] = richTextBox1.Lines[i+3];
itms = new ListViewItem(arry);
listView1.Items.Add(itms);
}
But it is not giving me the desired output, as it repeats itself over and over like seen here
Undesired Output
Kindy let me know where I am going wrong
Your for loop is going line by line, not by groups of 5 lines.
Change it to this:
for ( int i = 0; i < richTextBox1.Lines.Count(); i += 5)

Button Click action does not register itself the method it is assigned to

I've got myself into a little bit of a jam. As I tried to code the entire alphabet into my app, I didn't really feel like typing the letters in one by one, so I used ASCII characters and used a for-loop and inserted them as buttons. So, now I need those buttons to click according to my needs, but somehow I cant seem to figure out what is the problem with the issue. The code is as follows:
private void Hangman_OnLoaded()
{
const int btnSize = 35;
var c = 0;
for (var i = 65; i <= 90; i++)
{
var btn = new Button {
Content = (char) i,
Click += GuessClick()
};
btn.Width = btn.Height = btnSize;
var margin = btn.Margin;
margin.Left = c += 37;
btn.Margin = margin;
GridMain.Children.Add(btn);
}
}
private void GuessClick(object sender, EventArgs e) {
var choice = sender as Button;
if (choice == null || !copyCurrent.Contains(choice.DataContext.ToString())) return;
var temp = copyCurrent.ToCharArray();
var find = copyCurrent.ToCharArray();
var guessChar = choice.DataContext.ToString().ElementAt(0);
for (var index = 0; index < find.Length; index++) {
if (find[index] == guessChar) {
temp[index] = guessChar;
}
}
copyCurrent = new string(temp);
DisplayTheWord();
}
I will take a stab at "Psychic Debugging" this code. There are lots of things that can be improved, changed, etc, but I am guessing this is where you may be having an issue:
var temp = copyCurrent.ToCharArray();
var find = copyCurrent.ToCharArray();
var guessChar = choice.DataContext.ToString().ElementAt(0);
for (var index = 0; index < find.Length; index++) {
if (find[index] == guessChar) {
temp[index] = guessChar;
}
}
copyCurrent = new string(temp);
No matter what the guessChar is, at the end of this block of code copyCurrent will be unchanged (as far as what the actual string content of copyCurrent is). First you make two arrays that are identical. Then you are looping through the find array looking for the guessChar. If you find the guessChar then you are "modifying" the temp array to have guessChar at the very same index where you found it in find. Since both arrays are identical, the end result is that no "change" takes place. Once you have executed the loop you are creating a new string from the temp array, but since both find and temp are identical and unchanged after this loop, you effectively are making a new string that is identical to the one you started with. I don't know what you are expecting this code to do, but I am assuming this is not it.
I am going to assume (from the load method name) that this is a game of hangman and that at the outset of the game copyCurrent has some content like "????" and that you have some other variable, maybe called answer and it would have the content, for example, "food". If that is the case, then you would just need to modify these two lines:
var temp = copyCurrent.ToCharArray();
var find = answer.ToCharArray();

Button Array using String and Integer C#

I want to make button array that include string and integer. I have 30 buttons and named B1, B2, ...., B30 and I want to change their color depend on the counter value. How can I do that? These are what I have done and I stuck
for(Cnt = 0; cnt < 30; cnt++)
{
Button[] Tombol = new Button[]{"B"+(cnt+1)};
Tombol[cnt].BackColor = Color.Red
}
The Control (custom) initialization in the Form (in your case, the Control being a Button) requires more than a simple declaration. Apart from giving name (which you do), two other important things to do are:
To add it to the Control parent
To locate it nicely
Thus, adding those considerations to the Button you are to create, you could do something like this
int noOfBtns = 30;
Button[] Tombols = new Button[30]; //actually, you may not need this at all
for (int cnt = 0; cnt < numOfBtns; cnt++)
{
Button tombol = new Button();
tombol.BackColor = Color.Red;
tombol.Location = new Point(5, cnt * 25); //Read (2) please formulate this more properly in your case
tombol.Name = "B" + (cnt + 1).ToString();
// others like size (maybe important depending on your need), color, etc
this.Controls.Add(tombol); //Read (1) this refers to the `Form` if the parent control you want to put your button to is `Form`. But change the `this` as you see fit
Tombols[cnt] = tombol; //again, actually you may not need this at all
}
Take care of how you formulate the location of your button, very important. The example I gave above is really simple formulation, which might not fit if your number of buttons grow large. But that gives you the basic idea on how important it is to set the location of the Button right.
You may need the array of Button at all, unless for some reason you want to list it. But even you want to list it, you should use List and List.Add instead of Array.
I suggest using Linq to generate the array:
Button[] Tombol = Enumerable
.Range(0, 30)
.Select(i => new Button() {
Text = String.Format("B{0}", i + 1),
BackColor = Color.Red,
//TODO: compute desired color as function of "i" like
// BackColor = Color.FromArgb(i * 8, 255 - i * 8, 128),
//TODO: assign Parent, Location, Size etc. like this:
// Parent = this,
// Location = new Point(10 + 40 * i, 10),
// Size = new Size(35, 20),
})
.ToArray();
I mean like this:
Button[] Tombol = new Button[30];
for(cnt = 0; cnt < 30; cnt++)
{
Tombol[cnt] = new Button
{
Text = "B" + (cnt+1),
BackColor = Color.Red
};
}
First you create the array and in the for loop you instantiate the actual buttons one by one.
You have to initialize the button array first
int numOfBtns = 30;
Button[] Tombol = new Button[numOfBtns];
and after you can fill required items
for (int cnt = 0; cnt < numOfBtns; cnt++)
{
// Name, Content, Foreground .. whatever
Tombol[cnt].Name = "B" + (cnt + 1);
}
At the moment you are recreating your array on each loop of your code rather than creating the array outside of the loop and then adding buttons to it in the loop.
In addition all your buttons are being created in the same position, so they are on top of each other meaning you can only see one of them.
Try something like this, based on a modified version of the code from this previous answer which creates the buttons, adds them to a list you can search and the sets the positions of the buttons too:
int top = 50;
int left = 100;
var buttons = new Dictionary<string, Button>();
for (int i = 0; i < 30; i++)
{
Button button = new Button();
buttons.Add("B"+i, button);
button.Left = left;
button.Top = top;
this.Controls.Add(button);
top += button.Height + 2;
button.BackColor = Color.Red;
}
You can then refer to an individual button later with the code:
buttons["B3"].BackColor= Color.Green;

how to add picture box to the <list> dynamically

I want to add 23 picture boxes which I have been using in my form . I tried this way but it did not work:
List<PictureBox> pbList = new List<PictureBox>();
for (int i = 0; i < 23; i++)
pbList.Add(pictureBox +i);
You are trying to add them by name, you have to do a find:
List<PictureBox> pbList = new List<PictureBox>();
for (int i = 0; i < 23; i++)
pbList.Add((PictureBox)Controls.Find("pictureBox" + i));
Edit: If your picture boxes are nested within other controls, you will have to pass "true" to the Find() method so that all children are searched. The other solutions (including mine) will only search for picture boxes that are directly within the control container "Controls"
var pbList = Controls.OfType<PictureBox>().Select(a => a.Name.StartsWith("pictureBox"));

Better Way to display emails c# winforms

hello i have the following code which lets me display the emails in my inbox on a list view control and display the corrospoding body in a rtb my question is there a better way to handle this data and display it than the way below im pretty new to c# so detailed answers would be great
p.s im using the chilkat imap component to handle the server side if things
Thanks In Advance
Chilkat.MessageSet msgSet = imap.Search("ALL", true);
bundle = imap.FetchBundle(msgSet);
Chilkat.Email email;
int i;
for (i = 0; i < bundle.MessageCount - 0; i++)
{
email = bundle.GetEmail(i);
System.Windows.Forms.ListViewItem itmp = new System.Windows.Forms.ListViewItem(email.From);
System.Windows.Forms.ListViewItem.ListViewSubItem itms1 = new System.Windows.Forms.ListViewItem.ListViewSubItem(itmp, email.Subject);
System.Windows.Forms.ListViewItem.ListViewSubItem itms2 = new System.Windows.Forms.ListViewItem.ListViewSubItem(itmp, email.FromName);
itmp.SubItems.Add(itms1);
itmp.SubItems.Add(itms2);
listView1.Items.Add(itmp).Tag = i;
richTextBox1.Text = email.Body;
}
// Save the email to an XML file
bundle.SaveXml("email.xml");
Updated Code
Chilkat.MessageSet msgSet = imap.Search("ALL", true);
bundle = imap.FetchBundle(msgSet);
Chilkat.Email email;
int i;
for (i = 0; i < bundle.MessageCount; i++)
{
email = bundle.GetEmail(i);
string[] row = new string[]{email.From,
email.Subject,email.FromName};
object[] rows = new object[] { row };
foreach (string[] rowArray in rows)
{
listView1.Rows.Add(rowArray);
}
I think , you are doing correct only small point
Put a null check for bundle and email object.
for (i = 0; i < bundle.MessageCount - 0; i++) m, why are you substracting 0 from bundle.MessageCount
your code is simple and its doing what it is intended to do so why change at this time.

Categories

Resources