Before information is loaded from a txtfile, the buttons should be disabled first.
It will only be enabled if the txtfile is already loaded and the first letter in the first index of the txtfile (which is contact's nickname) matches the button's letter.
Here's my code for the contact information application:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
namespace Version1
{
public partial class Form1 : Form
{
List<string> contactList = new List<string>();
Button button;
public Form1()
{
InitializeComponent();
//generate 26 buttons A-Z
int locX = 10, locY = 30;
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < 26; i++)
{
button = new Button();
// button.Name = alphabet[i].ToString()/* + "_button"*/;
button.Size = new Size(30, 30);
button.Location = new Point(locX, locY);
button.Text = alphabet[i].ToString();
this.Controls.Add(button);
button.Enabled = false;
button.Click += new EventHandler(button_Click);
locX += 35;
if (i == 12)
{
locY = 65;
locX = 10;
}
}
}
private void loadToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult dr = openFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
openFileDialog1.Filter = "Text Files(*.txt) |*.txt";
string file = openFileDialog1.FileName;
listBox1.Items.Clear();
contactList.Clear();
try
{
StreamReader sr = new StreamReader(file);
while (true)
{
string line = sr.ReadLine();
if (line == null)
{
break;
}
else
{
contactList.Add(line);
string nickname = line.Split('|')[0];
string first = nickname.Substring(0, 1).ToUpper();
//im thinking of putting the enable/disable button method here but i dont know how. i already tried something like this:
if (first==button.Text.ToString()){
button.Enabled = true;
} else {
button.Enabled = false;
}
//the problem i encountered by using this code is that the last letter which is Z (doesnt have corresponding nicknames under it) is the only one that was disabled. i tried looping like this:
// for ( int i = 1; i < contactList.Count; i++)
// { split nickname, get the first letter
// if true: enable button letter, else disable
// }
// still, it didn't work :<
}
}
}
catch (Exception e1)
{
MessageBox.Show(e1.Message, "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void button_Click(object sender, EventArgs e)
{
if (sender is Button)
{
Button clickedButton = (Button)sender;
string letterPressed = clickedButton.Text.ToString();
LoadContacts(letterPressed);
}
}
private void LoadContacts(string letter)
{
listBox1.Items.Clear();
string l = letter/*.ToUpper()*/;
for (int i = 0; i < contactList.Count; i++)
{
string nickname = contactList.ElementAt(i).Split('|')[0];
string firstLetter = nickname.Substring(0, 1).ToUpper();
if (l == firstLetter)
{
listBox1.Items.Add(nickname);
}
}
}
private void listBox1_selectIndexChange(object sender, EventArgs e)
{
string nickname = listBox1.SelectedItem.ToString();
for (int i = 0; i < contactList.Count; i++)
{
string nn = contactList.ElementAt(i).Split('|')[0];
string gender = contactList.ElementAt(i).Split('|')[1];
if (nn == nickname)
{
textBoxName.Text = contactList.ElementAt(i).Split('|')[2];
textBoxAddress.Text = contactList.ElementAt(i).Split('|')[3];
textBoxPhone.Text = contactList.ElementAt(i).Split('|')[4];
textBoxEmail.Text = contactList.ElementAt(i).Split('|')[5];
pictureBox1.Image = new Bitmap((Image)Version1.Properties.Resources.ResourceManager.GetObject(nickname));
if (gender == "female")
{
textBoxName.ForeColor = Color.Pink;
textBoxAddress.ForeColor = Color.Pink;
textBoxEmail.ForeColor = Color.Pink;
textBoxPhone.ForeColor = Color.Pink;
}
else
{
textBoxName.ForeColor = Color.Blue;
textBoxAddress.ForeColor = Color.Blue;
textBoxEmail.ForeColor = Color.Blue;
textBoxPhone.ForeColor = Color.Blue;
}
break;
}
}
}
private void copyRightNotice_Click(object sender, EventArgs e)
{
AboutBox1 aboutBox = new AboutBox1();
aboutBox.Show();
}
private void exit_click(object sender, EventArgs e)
{
this.Close();
}
}
}
I am just a newbie in this field, so please bear with me

New Topic/Question
Reply




MultiQuote





|