C# groupBox lotto, need help assigning variable for group box - c#

private void LineUpNumbers ()
{
Array.Sort(lineArray[currentSelectedLine]); // Sorts the array in ascending order.
string numberBox = "Line" + (currentSelectedLine + 1).ToString() + "NumberBox";
string nb;
Image[] CircleColours = new Image[6] {Properties.Resources.BlueCircle, Properties.Resources.RedCircle , Properties.Resources.OrangeCircle , Properties.Resources.PurpleCircle , Properties.Resources.GreenCircle , Properties.Resources.YellowCircle};
Random r = new Random();
CircleColours = CircleColours.OrderBy(x => r.Next()).ToArray();
for (int i = 0; i <6; i++) // Populates NumberBoxes with the numbers from the array
{
nb = numberBox + (i + 1);
Line1.Controls[nb].Text = lineArray[currentSelectedLine][i].ToString();
Line1.Controls[nb].BackgroundImage = CircleColours[i];
}
}
Hi,
In the above snippet I'd like to change Line1 to be a string variable, but I'm not sure on the correct syntax for doing so. Could anyone help with a solution for this? This is C#
Line1 is the GrouBox name.
I'd like to be able to change Line1 to a string. Like this:
string groupBoxName = "Line1";
groupBoxName.buttonName.BackgroundImage = CircleColours[i];
The code doesn't work though

You can use the Controls array of the Form this.Controls like this...
string groupBoxName = "Line1";
Control groupBox = this.Controls[groupBoxName];
groupBox.Controls[nb].BackgroundImage = CircleColours[i];
...just like the way you are getting the NumberBox dynamically from the Controls array of the GroupBox.

Related

Visual Basic C# with Selenium variable in css selector

I am only a week or so into C#, but thanks to forums like this I am able to piece together a bunch of code and get a general understanding. However, I have a problem with using a variable I can't solve.
List<IWebElement> elementList = new List<IWebElement>();
elementList.AddRange(browser.FindElements(By.CssSelector("div[class^='CalendarDays-calendarDays-']>div:nth-child(3)>div>div[class^='CalendarEvent-event-']")));
int t = elementList.Count;
for (int i = 1; i <= t; i++)
{
var item + i = browser.FindElement(By.CssSelector("div[class^='CalendarDays-calendarDays-']>div:nth-child(3)>div>div:nth-child(i)>div>div[class^='CalendarEvent-title']")).GetAttribute("textContent");
}
Problems are "var item + i" and "div:nth-child(i)". The first is automatically creating sequential variables and the second trying to get the nth-child using the variable "i". I can't figure out how to format "i" in either instance.
Any help would be appreciated.
Working Code after edits:
List<IWebElement> elementList = new List<IWebElement>();
elementList.AddRange(browser.FindElements(By.CssSelector("div[class^='CalendarDays-calendarDays-']>div:nth-child(3)>div>div[class^='CalendarEvent-event-']")));
int t = elementList.Count;
List<String> listItems = new List<String>();
for (int i = 1; i <= t; i++)
{
String item = browser.FindElement(By.CssSelector("div[class^='CalendarDays-calendarDays-']>div:nth-child(3)>div>div:nth-child(i)>div>div[class^='CalendarEvent-title']")).GetAttribute("textContent");
listItems.Add(item);
}
Thanks,
Don
You should use a list to store the different items, you have to know the type of GetAttribute. I assume it is String :
List<String> listItems = new List<String>();
for (int i = 1; i <= t; i++)
{
String item = browser.FindElement(By.CssSelector("div[class^='CalendarDays-calendarDays-']>div:nth-child(3)>div>div:nth-child(i)>div>div[class^='CalendarEvent-title']")).GetAttribute("textContent");
listItems.Add(item);
}
then you can access the list items by index listItems[i]
For the second part of the question you have to concatenate the string with the i of the for loop like this:
browser.FindElement(By.CssSelector("div[class^='CalendarDays-calendarDays-']>div:nth-child(3)>div>div:nth-child("+i+")>div>div[class^='CalendarEvent-title']")).GetAttribute("textContent");
You can't do var item + i. Just use the same variable each time, it's inside the loop scope anyway.
To insert the i variable to the locator you can use string interpolation
var item = browser.FindElement(By.CssSelector($"div[class^='CalendarDays-calendarDays-']>div:nth-child(3)>div>div:nth-child({i})>div>div[class^='CalendarEvent-title']")).GetAttribute("textContent");

Blank space is not displayed the same in the console output and in listbox

I am writing code to automate the code of PLSQL basic structure.
So i have a list which holds the parameters, example as below,
pv_student_name IN VARCHAR2
pn_number OUT NUMBER
So the parameters must be properly aligned as above, So I have written a procedure (method) in c# which takes plain input and align the parameters and returns a list with properly aligned parameters.
But When I print in the Console using System.Console.WriteLine then the parameters are aligned properly But when I see the parameters in a List Box the parameters are not aligned properly , though the spaces are added.
Below is the alignment code.
//Alignment of parameters logic
private List<string> alignParameters(List<string> nonAligned_parameters){
List<string> alignedParamtersList = new List<string>();
List<string> parameterList = new List<string>();
List<string> inOutList = new List<string>();
List<string> dataTypeList = new List<string>();
foreach (string parameter in nonAligned_parameters)
{
string[] param = parameter.Split(' ');
int count = 1;
foreach (string word in param)
{
if (count == 1)
{
parameterList.Add(word.Trim());
}
else if (count == 2)
{
inOutList.Add(word.Trim());
}
else if (count == 3) {
dataTypeList.Add(word.Trim());
}
count++;
}
}
// find the longest string in the list
int maxLength = parameterList.Max(x => x.Length);
//aligning the parameters
int numberOfElements= parameterList.Count();
for (int i = 0; i < numberOfElements; i++)
{
string param = parameterList[i].ToString().Trim();
string inOut = inOutList[i].ToString().Trim();
string dataType = dataTypeList[i].ToString().Trim();
int requiredSpace = maxLength - param.Length + 1;
string spaces = new string(' ', requiredSpace);
string spc = "";
if ("OUT".Equals(inOut)) {
spc = " ";
}
else if ("IN".Equals(inOut)) {
spc = " ";
}
string alignedParameter = param +
spaces +
inOut +
spc +
dataType;
// adding aligned parameters in aligned parameter list to be returned
alignedParamtersList.Add(alignedParameter);
}
return alignedParamtersList;
}
Below is the procedure where it is called, when we add a new parameter then the parameter is added, aligned and then populated back in the ListBox
private void button_AddParameter_Click(object sender, EventArgs e)
{
string error = "N";
//validation code here
if ("N".Equals(error)) {
parameterName = label_paramPrefix.Text +
textBox_parameterName.Text +" "+
combo_InOut.SelectedItem.ToString()+" "+
combo_DataType.SelectedItem.ToString();
}
parameterList.Add(parameterName);
// Align the parameters
parameterListAligned = alignParameters(parameterList);
// populating parameter list in GUI with fresh aligned parameters
listBox_parameter.Items.Clear();
foreach (string parameters in parameterListAligned)
{
listBox_parameter.Items.Add(parameters);
System.Console.WriteLine("param" + parameters);
}
}
So the problem is that even if the Console outputs the parameters aligned properly, the ListBox doesn't.
Can you guys please help me with this, I need those parameters to appear aligned in the ListBox as well.
Screen shot of the application where we can see the parameters not aligned in the ListBox is attached. and console output screenshot is also attached below that.
All you need is to use a monospace font, you can change the font of a listview from its properties windows (choose the listview in visual studio and click F4) and then change the font to something like 'Courier'.

how to generate text boxes based on a string value and add each check box to a panel just like a todo-list

What I am trying to do is something like:
public void thisDoesNotWork (string n)
{
string textBoxName = n.Substring(0, 4); // assuming the string cannot be less than 4 charcters
checkbox textBoxName = new checkbox();
textBoxName.text = n;
panel.Controls.Add(textBoxName)
}
where the string that was past in becomes the name used to instantiate the object.
My goal here is to create a new instance of a new CheckBox every time a different string is passed into the method instead of creating the same instance of the same CheckBox multiple times.
With the above method am merely adding the exact same CheckBox to the panel multiple time, well at lease I think so. But I need to add a brand new check box and not instances of the same.
I hope you guys understand and i am not making this too confusing than it really is.
public thisDoesWork(string n)
{
CheckBox b = new CheckBox();
b.Text = n;
panel.Controls.Add(b);
}
May be this will help you:
private const int Step = 10
private int y = 0;
public ThisDoesWork(string n)
{
CheckBox checkBox = new CheckBox
{
Text = n,
Top = y,
Parent = panel1
};
y += checkBox.Height + Step; // you can set any rules for the postition of new controls
}

How to assign dynamically created objects to a string?

I have a form with this code assigned to a button:
TextBox[] tbxCantServ = new TextBox[1];
int i;
for (i = 0; i < tbxCantServ.Length; i++)
{
tbxCantServ[i] = new TextBox();
}
foreach (TextBox tbxActualCant in tbxCantServ)
{
tbxActualCant.Location = new Point(iHorizontal, iVertical);
tbxActualCant.Visible = true;
tbxActualCant.Width = 44;
tbxActualCant.MaxLength = 4;
this.Controls.Add(tbxActualCant);
iVertical = iVertical + 35;
}
And this code creates textboxes dynamically, one for every "button click", so I can have an "add" button to call it and the user can write a list of things that is not limited.
The question is: How can I assign these "textboxes.Text" to a string? They haven't got a name :S
something like:
string sAllBoxes = tbx1.Text + tbx2.Text + "..." + tbxN.Text;
Thanks!!
If your tbxCantServ is defined as local to a method, then you have to assign a Name to your TextBoxes like:
int counter = 0;
foreach (TextBox tbxActualCant in tbxCantServ)
{
tbxActualCant.Location = new Point(iHorizontal, iVertical);
tbxActualCant.Name = "tbx" + counter++;
tbxActualCant.Visible = true;
tbxActualCant.Width = 44;
tbxActualCant.MaxLength = 4;
this.Controls.Add(tbxActualCant);
iVertical = iVertical + 35;
}
And later in some other method if you want to get the joined text then you can do:
string sAllBoxes = string.Join(",", this.Controls.OfType<TextBox>()
.Where(r => r.Name.StartsWith("tbx"))
.Select(r => r.Text));
But if you have tbxCantServ defined at class level then you can do:
string sAllBoxes = string.Join(",", tbxCantServ
.Where(r=> r != null)
.Select(r => r.Text));
In string.Join, you can replace , with an empty string or any string depending on your requirement.
You can do it in the same way you created them.
Try this:
string sAllBoxes="";
foreach (TextBox tbxActualCant in tbxCantServ)
{
sAllBoxes+=tbxActualCant.Text;
}
OR
Using a StringBuilder:
StringBuilder textBuilder = new StringBuilder();
foreach (TextBox tbxActualCant in tbxCantServ)
{
textBuilder.Append(tbxActualCant.Text);
}
string allText = textBuilder.ToString();
If you have access to your textbox array, you can easily do this:
string sAllBoxes = string.Join(" ", tbxCantServ.Select(x => x.Text));
If you don't then use Control collection of your Form, and give name to your textboxes so you can access them using this.Controls[txtBoxName].
If you just want to concatanate your texts without a separator, you can also use string.Concat method:
string sAllBoxes = string.Concat(tbxCantServ.Select(x => x.Text));

how to store a part of dropdownlist text in a dynamically generated property

here is my code:
EntityLayer.Entity[] en = new EntityLayer.Entity[counter];
int count = 0;
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected == true)
{
en[count] = new EntityLayer.Entity();
en[count].all_Emp_Mobile = DropDownList1.SelectedValue.ToString();
en[count].all_Emp_Name = DropDownList1.Text;
en[count].all_Stu_Mobile = CheckBoxList1.Items[i].Value.ToString();
count++;
}
}
now the thing is that the dropdown list text has both the name and mobile number concatenated and seperated by "/" character. what i want to do is to just save the name of the person in the property en[count].all_Emp_Name., is there a method in c# like Remove or something like that which can remove the the latter part i.e the mobile number from the string.
Remove method demands the index from where you want to remove the text and for that i might use
Indexof but i am a little confused here how to go about it, as the property is generated dynamically in the for loop
do something like this:
string DropDownText = "EhsanSajjad/03123009099";
string[] temp = DropDownText.Split('/');
string personName = temp[0];
string CellNumber = temp[1];
got it finally....
int index = DropDownList1.SelectedItem.Text.LastIndexOf('/');
string empName = DropDownList1.SelectedItem.Text.Remove(index);
en[count].all_Emp_Name = empName;
this seems to be working just perfect.

Categories