Need to Scramble the selected Text - c#

protected void Button_Upload_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/Data/" + FileUpload1.FileName);
string[] readtext = File.ReadAllLines(path);
var a = readtext;
List<string> strList = new List<string>();
foreach (string s in readtext)
{
strList.Add(s);
}
ListBox1.DataSource = strList;
ListBox1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
var b = ListBox1.SelectedIndex;
var a = ListBox1.SelectedValue.ToString();
if (b < 0)
{
// no ListBox item selected;
return;
}
StringBuilder jumbleSB = new StringBuilder();
jumbleSB.Append(a);
Random rand = new Random();
int lengthSB = jumbleSB.Length;
for (int i = 0; i < lengthSB; ++i)
{
int index1 = (rand.Next() % lengthSB);
int index2 = (rand.Next() % lengthSB);
Char temp = jumbleSB[index1];
jumbleSB[index1] = jumbleSB[index2];
jumbleSB[index2] = temp;
}
Console.WriteLine(jumbleSB);
TextBox1.Text = ListBox1.Text.ToString();
//TextBox1.Text = ListBox1.Text.Insert(jumbleSB);
Here I need to Jumble the values which selected by User. When User selects a Value it has to jumble and has to come to Textbox. I am not able to displaying the Jumble values. Any help Please...??

Console.WriteLine(jumbleSB.ToString());

Related

How to remove even numbers from an arraylist in c#?

So I'm trying to remove all even numbers from the generated random list I created. But this message keeps showing up: "System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index'
I don't know what I'm doing wrong. What do I need to change?
public partial class Form2 : Form
{
ArrayList array = new ArrayList();
int count = -1;
int[] numbers = new int[5];
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Random randNum = new Random();
for (int i = 0; i < 5; i++)
{
numbers[i] = randNum.Next(-100, 100);
array.Add(numbers[i]);
richTextBox1.Clear();
}
for (int i = 0; i <= count; i++)
{
//displays random numbers to rich textbox
richTextBox1.AppendText(array[i].ToString() + '\n');
}
}
private void button2_Click(object sender, EventArgs e)
{
foreach (int i in array)
{
if (Convert.ToInt32(array[i]) % 2 == 0)
{
array.RemoveAt(i);
}
richTextBox1.Text = array[i].ToString();
}
}
Your code in Button2_click is wrong. You are using foreach loop and then trying to access the index of array using its elements value.
change your second button code with following code
for (int i = 0; i < array.Count; )
{
if (Convert.ToInt32(array[i]) % 2 == 0)
{
array.RemoveAt(i);
}
else {
i++;
}
}
foreach (var item in array)
{
richTextBox1.Text = item +"\n";
}
You should change your enumeration like:
private void button2_Click(object sender, EventArgs e)
{
for (int i = 0; i < array.Count; i++)
{
if (Convert.ToInt32(array[i]) % 2 == 0)
{
array.RemoveAt(i);
}
richTextBox1.Text = array[i].ToString();
}
}
Using LINQ you can achieve this easily.
var oddArray = array.Where(a => a % 2 != 0);
richTextBox1.Text = string.Join(", ", oddArray);
PS - You just need to add a namespace i.e. System.Linq

Count the string by words not char

I am working on this code in which it can identify the word listed below from the Web Browser. Those words will turn into asterisk once they are identified and will count how many words were replaced but it didn't work. Someone can help?
Here is my code:
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(txbAdress.Text);
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
IHTMLDocument2 doc2 = webBrowser1.Document.DomDocument as IHTMLDocument2;
StringBuilder html = new StringBuilder(doc2.body.outerHTML);
string query;
query = #"
select Word from ListWords
";
List<string> words = new List<string>();
DataSet ds;
DataRow drow;
ds = DatabaseConnection.Connection1(query);
int index, total;
total = ds.Tables[0].Rows.Count;
string current_word;
for (index = 0; index < total; index++ )
{
drow = ds.Tables[0].Rows[index];
current_word = drow.ItemArray.GetValue(0).ToString();
words.Add(current_word);
}
Console.WriteLine(query);
Console.WriteLine("array:" + words);
foreach (String key in words)
{
// String substitution = "<span style='background-color: rgb(255, 0, 0);'>" + key + "</span>";
int len = key.Length;
string replace = "";
for ( index = 0; index < len; index++)
{
replace += "*";
}
html.Replace(key, replace);
//count++;
}
//Console.WriteLine("Total number of words: " + count);
doc2.body.innerHTML = html.ToString();
}
Just count them before you replace them
Given
public int CountOccurrences(string source, string match)
{
var pos = 0;
var count = 0;
while ((pos < source.Length) && (pos = source.IndexOf(match, pos, StringComparison.Ordinal)) != -1)
{
count++;
pos += match.Length;
}
return count;
}
Example
var count = CountOccurrences(html,key);
html.Replace(key, substitution);

Read File and store into array and display sum in textbox

I'm trying to get data from a file and store them in an array then display the data in a listbox the find then sum and display it in a text box. Here's my code and it doesn't work. I'm not sure what i'm doing wrong.
private void findClick(object sender, EventArgs e)
{
int sum;
using (OpenFileDialog ofd = new OpenFileDialog())
{
if (ofd.ShowDialog() == DialogResult.OK)
{
using (StreamReader InputFile = new StreamReader(ofd.FileName))
{
while (InputFile.EndOfStream == false)
{
int[] array = new int[listBox.Items.Count];
for (int i = 0; i < listBox.Items.Count; i++)
{
// array[i] = Convert.ToInt32(listBox.Items[i].ToString());
array[i] = int.Parse(listBox.Items[i].ToString());
sum = array.Sum();
TotalAmtlabel.Text = sum.ToString("N0");
TotalNumberslabel.Text = listBox.Items.Count.ToString();
TotalAmountlabel.Text = string.Format("{0:N0}", sum);
}
}
}
}
}
}
listBox.Items.AddRange(File.ReadAllLines(ofd.FileName));
Try this and modify according to your needs:
string[] amounts = File.ReadAllLines(ofd.FileName);
int currentSum = 0;
int totalSum = 0;
ListItem[] amountItems = new ListItem[amounts.Length];
for (int i = 0; i < amounts.Length; i++)
{
if (int.TryParse(amounts[i], out currentSum))
{
totalSum += currentSum;
}
amountItems[i] = amounts[i];
}
listBox.Items.AddRange(amountItems);
TotalAmountlabel.Text = string.Format("{0}", totalSum);
You can also datasource to bind the list. Please go through below MSDN references once atleast to understand security cautions:
ListItem
ListBox

How do I get role name for user in dnn?

I am new programmer in DNN. My question is How do I get role name for user in DNN?
My code is:
protected void Page_Load(object sender, EventArgs e)
{
int UserId = UserController.GetCurrentUserInfo().UserID;
int PortalId = PortalSettings.PortalAlias.PortalID;
UserInfo user = UserController.GetUserById(PortalId, UserId);
String[] array = new String[5];
for (int x = 0; x < 5; x++)
{
array[x] = UserInfo.Roles.ToString();
}
TextBox1.Text = array[0];
TextBox2.Text = array[1];
}
But just display System.String[] in textboxes.
What should i do?
You can use this code:
String[] array = new String[5];
var user = UserController.GetUserByName(UserInfo.Username);
for (int x = 0; x < user.Roles.Count(); x++)
{
array[x] = user.Roles[x];
}

How to interact with variables (just created in the program) that are in another method?

In the first method I'm creating a matrix of textboxes and in another (Button_click) method. I need to take the values of textboxes from the Button_Click method and then do something with them. I don't know how interact with just created values(the names are also new). But I know that I can't use t[j].
private void vsematrici_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int selectedIndex = vsematrici.SelectedIndex+2;
StackPanel[] v = new StackPanel[selectedIndex];
for (int i = 0; i < selectedIndex; i++)
{
v[i] = new StackPanel();
v[i].Name = "matrixpanel" + i;
v[i].Orientation = Orientation.Horizontal;
TextBox[] t = new TextBox[selectedIndex];
for (int j = 0; j < selectedIndex; j++)
{
t[j] = new TextBox();
t[j].Name = "a" + (i + 1) + (j + 1);
t[j].Text = "a" + (i + 1) + (j + 1);
v[i].Children.Add(t[j]);
Thickness m = t[j].Margin;
m.Left = 1;
m.Bottom = 1;
t[j].Margin = m;
InputScope scope = new InputScope();
InputScopeName name = new InputScopeName();
name.NameValue = InputScopeNameValue.TelephoneNumber;
scope.Names.Add(name);
t[j].InputScope = scope;
}
mainpanel.Children.Add(v[i]);
}
Button button1 = new Button();
button1.Content = "Найти определитель";
button1.Click += Button_Click;
mainpanel.Children.Add(button1);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// Double sa11v = Convert.ToDouble(t[j].Text);
}
Sorry for my English, I'm from Russia :)
you could create a member variable that is used outside the event in which you have created TextBox Array-
TextBox[] _t = null;
private void vsematrici_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int selectedIndex = vsematrici.SelectedIndex + 2;
StackPanel[] v = new StackPanel[selectedIndex];
for (int i = 0; i < selectedIndex; i++)
{
v[i] = new StackPanel();
v[i].Name = "matrixpanel" + i;
v[i].Orientation = Orientation.Horizontal;
_t = new TextBox[selectedIndex];
for (int j = 0; j < selectedIndex; j++)
{
_t[j] = new TextBox();
_t[j].Name = "a" + (i + 1) + (j + 1);
_t[j].Text = "a" + (i + 1) + (j + 1);
v[i].Children.Add(_t[j]);
Thickness m = t[j].Margin;
m.Left = 1;
m.Bottom = 1;
_t[j].Margin = m;
InputScope scope = new InputScope();
InputScopeName name = new InputScopeName();
name.NameValue = InputScopeNameValue.TelephoneNumber;
scope.Names.Add(name);
_t[j].InputScope = scope;
}
mainpanel.Children.Add(v[i]);
}
Button button1 = new Button();
button1.Content = "Найти определитель";
button1.Click += Button_Click;
mainpanel.Children.Add(button1);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (_t != null)
{
//Do your work here
// Double sa11v = Convert.ToDouble(t[j].Text);
}
}
but for a broader aspect, you can use dictionary,
Dictionary<string, TextBox> _dicTextBoxes;
private void vsematrici_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int selectedIndex = vsematrici.SelectedIndex + 2;
StackPanel[] v = new StackPanel[selectedIndex];
for (int i = 0; i < selectedIndex; i++)
{
v[i] = new StackPanel();
v[i].Name = "matrixpanel" + i;
v[i].Orientation = Orientation.Horizontal;
_dicTextBoxes = new Dictionary<string, TextBox>();
for (int j = 0; j < selectedIndex; j++)
{
TextBox txtBox = new TextBox();
txtBox = new TextBox();
txtBox.Name = "a" + (i + 1) + (j + 1);
txtBox.Text = "a" + (i + 1) + (j + 1);
v[i].Children.Add(txtBox);
Thickness m = txtBox.Margin;
m.Left = 1;
m.Bottom = 1;
txtBox.Margin = m;
InputScope scope = new InputScope();
InputScopeName name = new InputScopeName();
name.NameValue = InputScopeNameValue.TelephoneNumber;
scope.Names.Add(name);
txtBox.InputScope = scope;
_dicTextBoxes.Add(txtBox.Name, txtBox);
}
mainpanel.Children.Add(v[i]);
}
Button button1 = new Button();
button1.Content = "Найти определитель";
button1.Click += Button_Click;
mainpanel.Children.Add(button1);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (_dicTextBoxes != null)
{
// a23 is the name of textbox, 'a' is prefixe, '2' is the 2nd stackpanel you have added
// '3' is the 3rd textbox you have added in stackpanel
if (_dicTextBoxes.ContainsKey("a23"))
{
//Do your work here
Double sa11v = Convert.ToDouble(_dicTextBoxes["a23"].Text);
}
}
}
Loop through the Children of mainpanel and check each control if it is a TextBox. Something like:
foreach (UIElement ctrl in mainpanel.Children)
{
if (ctrl.GetType() == typeof(TextBox))
{
TextBox oneOfYourTextBoxes = ((TextBox)ctrl);
// do your thing
}
}
You cannot change an arrays size after you created it. Hence your size is known only at runtime after firing your event you cannot set its size within the declaration.
class MyClass
{
Textbox[] myTextBoxes;
private void vsematrici_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// ...
myTextBoxes = new TextBoxes[selectedIndex];
}
private void Button_Click(object sender, RoutedEventArgs e)
{
int myIndex = // your selected item here
if (this.myTextBoxes != null && myIndex < this.myTextBoxes.Length)
{
Console.WriteLine(this.myTextBoxes[myIndex].Text);
}
}
}
This will also work:
button1.Click += (sender, args) => DoSomethingToTextboxes(_t);

Categories

Resources