How to call a file for picturebox - c#

I have a Form with 6 arrays, 15 elements each. I wrote them in a way that if I search for myself (being the 1st element in the string[] name array), the form would load the 1st element of every array: age, gender, hobby, photo etc.
Here is the part of the pictureBox code I'm using
//I have more arrays here, like: Name,Gender,Age,Hobby,Place & Talent
string[] photo=new string[] { "764.jpg", " 765.jpg", " 766.jpg", " 767.jpg"," 765.jpg", " 767.jpg", " 768.jpg", " 769.jpg", " 770.jpg", " 771.jpg", " 772.jpg", " 773.jpg", " 774.jpg", " 775.jpg", " 776.jpg"};
string name="",gender="",place="",warning=""; int age = 0; string image="";
for (int i = 0; i < 15; i++)
{
if (textBox6.Text == emri[i])
{
//I applied every element of every array to the declared variables
image = photo[i];
j++;
}
if (j == 1)
{
//Associated the textBoxes with the variables
pictureBox1.Image = Image.FromFile(#"C:\Users\NComputers\Documents\Visual Studio 2015\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\Sony Xperia\" + image);
}
else
{
MessageBox.Show("Person not found!");
}
While there are no errors shown, the picture simply doesn't load. All the other items are working. Help?

Related

How to modify an item inside ListBox?

i want to concat(add) a string over an existing item here is the code:
private void button4_Click(object sender, EventArgs e)
{
double total = 0;
double[] prices = {0.5, 1.0, 1.5, 3.0, 2.5, 2.0};
CheckBox[] boxes = { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6 };
listBox2.Items.Add(textBox1.Text + " : ");
for (int i=0;i<boxes.Length ;i++)
{
if (boxes[i].Checked==true)
{
total += prices[i];
listBox2.Items.Add(boxes[i].Text+" "); //it adds a new item
}
}
the problem is with the output , every time it adds a new item i just want to add this line boxes[i].Text+" " , to the same item
thank you.
What do you mean by:
to the same item
Do you want to add it all to this one? listBox2.Items.Add(textBox1.Text + " : ");
If so could you not just do this:
string name = textBox1.Text + " : ";
for (int i=0; i < boxes.Length; i++)
{
if (boxes[i].Checked == true)
{
total += prices[i];
name += boxes[i].Text + " ";
}
}
listBox2.Items.Add(name);
You should use the following command listbox2.items[0]+=boxes[i].Text+" " with 0 being the index of the item you want to concatenate the string with.Because listbox2.items.Add will just keep adding items to your Listbox.
Best Regards.

c# finding a string in a text file then do something?

ok what im trying to do here is the following
private void addgsc()
{
if (File.Exists(hud))
{
{
string s = " itemDef\n\r"
+ "{"
+ " name \"zombiecounter\"\n\r"
+ " rect 100 70 0 0 HORIZONTAL_ALIGN_CENTER VERTICAL_ALIGN_BOTTOM\n\r "
+ " textscale .5\n\r"
+ " textstyle ITEM_TEXTSTYLE_SHADOWED\n\r"
+ " textfont UI_FONT_OBJECTIVE\n\r"
+ " textalign ITEM_ALIGN_CENTER\n\r"
+ " forecolor 1 0 0 1\n\r"
+ " exp text (\"Zombies Left: \" + dvarInt(\"zombie_counter\"))\n\r"
+ " visible when (dvarInt(\"zombie_counter\") > 0);\n\r"
+ "decoration\n\r"
+ "} ";
string file = hud;
List<string> lines = new List<string>(System.IO.File.ReadAllLines(file));
int index = lines.FindLastIndex(item => item.Contains("playerscores"));
if (index != -1)
{
lines.Insert(index + 1, s);//""
}
System.IO.File.WriteAllLines(file, lines);
MessageBox.Show("done");
}
}
and im looking for a line in a code like this
itemDef
{
name "playerscores"
rect 0 0 100 0
ownerdraw CG_COMPETITIVE_MODE_SCORES
visible 1
}
but what i want to do is find player scores then find the last } at the end and add it there because currently its adding it right under player scores but im not sure how i can get it to go to find that and then find the closest } one of those and add it under neath that so its in a new block not added into the player score one so would want something like the following
image 1
I'm a bit unclear of the desired output, so the below code may be more than what you need but should get you on the right track. Please forgive syntax errors.
EDIT: I reread the question a few times, and think this is what you are looking for.
var allLinesInFile = System.IO.File.ReadAllLines(file);
var isPlayerScore = false;
var linesToWrite = new List<string>();
var linesToAdd = new List<string> {
"itemDef\n\r",
"{",
" name \"zombiecounter\"\n\r",
//and so on
"}"
};
foreach (var line in allLinesInFile)
{
linesToWrite.Add(line);
if (line.IndexOf("playerscores", StringComparison.OrdinalIgnoreCase) >= 0)
{
//starting of data detected.
isPlayerScore = true;
}
else if (isPlayerScore == true && line.IndexOf("}") >= 0)
{
//end of data detected
isPlayerScore = false;
linesToWrite.AddRange(linesToAdd);
}
}
System.IO.File.WriteAllLines(file, linesToWrite);
MessageBox.Show("done");

Adding +1 every time radiobox is used

I'm wanting to add +1 every time a radiobox or checkbox is used. This happens when calculate is pressed and putting it into a string then when the summary button is pushed it displays it in a messagebox with the final total of times it was pushed.
this is my code so far but it doesnt seem to work,
This is my Calculate button
int Quantity;
int Finalprice;
if (lunchRadioButton.Checked == true)
{
Meal = Lunch;
}
else if (tableCheckBox.Checked && waiterCheckBox.Checked)
{
Extras = Waiter + Table;
}
else if (waiterCheckBox.Checked)
{
Extras = Waiter;
}
else if (tableCheckBox.Checked)
{
Extras = Table;
}
//Early Evening Meal
if (earlyEveningRadioButton.Checked == true)
{
Meal = Early;
}
else if (tableCheckBox.Checked && waiterCheckBox.Checked)
{
Extras = Table + Waiter;
}
else if (waiterCheckBox.Checked)
{
Extras = Waiter;
}
else if (tableCheckBox.Checked)
{
Extras = Table;
}
//Late evening options
if (lateEveningRadioButton.Checked == true)
{
Meal = Late;
}
//Late Evening, Corner table and Dedicated waiter selected.
else if (tableCheckBox.Checked && waiterCheckBox.Checked)
{
Extras = Table + Waiter;
}
//Late Evening and Dedicated waiter selected.
else if (waiterCheckBox.Checked)
{
Extras = Waiter;
}
//Late Evening and Corner Table
else if (tableCheckBox.Checked)
{
Extras = Table;
;
}
try
{
Quantity = int.Parse(guestTextBox.Text);
Mealprice = (Meal * Quantity);
Finalprice = (Mealprice + Extras);
finalAmountLabel.Text = Finalprice.ToString("C");
}
catch
{
guestTextBox.Focus();
guestTextBox.SelectAll();
MessageBox.Show("Please enter a quantity in numerical form","Quantity Error!");
}
This is the Summary Button
int TotalLunch = 0;
int TotalEarly = 0;
int TotalLate = 0;
int TotalBookings = 0;
int TotalWaiters = 0;
int LateWaiters = 0;
int EarlyWaiters = 0;
int LunchWaiters = 0;
int TotalCornerTables = 0;
int EarlyCornerTables = 0;
int LateCornerTables = 0;
int LunchCornerTables = 0;
if (earlyEveningRadioButton.Checked == true)
{
TotalEarly = +1;
TotalEarly.ToString();
}
else if (earlyEveningRadioButton.Checked && waiterCheckBox.Checked)
{
EarlyWaiters = +1;
EarlyWaiters.ToString();
}
else if (tableCheckBox.Checked && earlyEveningRadioButton.Checked)
{
EarlyCornerTables = +1;
EarlyCornerTables.ToString();
}
else if (waiterCheckBox.Checked && tableCheckBox.Checked)
{
EarlyWaiters = +1;
EarlyCornerTables = +1;
EarlyWaiters.ToString();
EarlyCornerTables.ToString();
}
MessageBox.Show("Number of Early Bookings" + " " + TotalEarly + " " +
"Number of Early Evening Waiters" + " " + EarlyWaiters
+ "number of Early Evening Corner Tables tables" + " " + EarlyCornerTables
);
Any suggestions or help would be greately appriciated :)
The best way you can do this, is using the eventhandler CheckedChanged foreach checkbox.
in the event do this:
if(checkBox.Checked)
{
//int +1
}
I'll start with an answer to your question...
Your code will not increment TotalEarly by one, but it will assign +1 to the variable.
TotalEarly = +1;
For incrementing you should either use the ++ or the += operator:
TotalEarly++; // increments by 1
TotalEarly += 1; // also increments by 1, but += can be used with other numbers
Some other notes, not related to your question:
TotalEarly.ToString();
This will not do anything since you do not assign the result to a variable. This will not convert TotalEarly to a string variable! The type of TotalEarly is fixed to int as you specified.
You would have to write
var totalEarlyString = TotalEarly.ToString();
to be able to use the result afterwards. You might wonder why your MessageBox still displays that number as a string although you use the int variable:
"Number of Early Bookings" + " " + TotalEarly
This is because the ToString() method is called automatically in this case. So, you can really save some lines by removing your calls to ToString() after the assignments.
One more thing: local variables are typically named using camelCase. That means, the first character is lower case and every additional word starts with an uppercase character. PascalCase (i.e. also starting with uppercase character) is typically used for properties, methods, class names etc, but not for local or member variables.
It seems like you want each value in the MessageBox to be on a separate line (at least I assume that is why you added some spaces at the end of the first line:
MessageBox.Show("Number of Early Bookings" + " " + TotalEarly + " " +
"Number of Early Evening Waiters" + " " + EarlyWaiters
+ "number of Early Evening Corner Tables tables" + " " + EarlyCornerTables
);
This might not work on all machines. The MessageBox has different sizing behavior on differnt versions of Windows. Some will wrap the text to multiple lines, some will not, or at least at different widths!
Therefore, you better add an Environment.NewLine at the end of each line. This will add a line break to your string which will be interpreted as such by the MessageBox:
MessageBox.Show("Number of Early Bookings " + TotalEarly + Environment.NewLine +
"Number of Early Evening Waiters " + EarlyWaiters + Environment.NewLine +
"Number of Early Evening Corner Tables tables " + EarlyCornerTables);
Accordion to your comments in your code I think the problem is that your if else statements are wrong.
Q1: Should it calculate on every checkbox click or only when you click calculate?
- if it should calculate on every checkbox click then you have to use the CheckedChanged event as RaZor points out.
Q2: Is it true that de client can have a lunch + waiter + table?
- if that is true then your if else statement should be as follows:
if (lunchRadioButton.Checked == true)
{
Meal = Lunch;
if (tableCheckBox.Checked && waiterCheckBox.Checked)
{
Extras = Waiter + Table;
}
else if (waiterCheckBox.Checked)
{
Extras = Waiter;
}
else if (tableCheckBox.Checked)
{
Extras = Table;
}
}

How to get the Radio button values from PDF form using PDFbox (C#)

How to get the Radio button values from PDF form using PDFbox (C#)
In Java :
doc = PDDocument.load("C:\\Users\\347702\\Desktop\\sample_form.pdf");
List pages = doc.getDocumentCatalog().getAllPages();
PDAcroForm form = doc.getDocumentCatalog().getAcroForm();
List FieldTypes = form.getFields();
PDField pdfFields;
for (int i = 0; i < FieldTypes.size(); i++) {
pdfFields = (PDField) ((List) FieldTypes).get(i);
if (pdfFields instanceof PDRadioCollection)
{
String iAsString = Integer.toString(k);
System.out.println(iAsString);
type = "RadioButton";
System.out.println("Radio Button" + " "
+ pdfFields.getFullyQualifiedName() + " ");
List kids = pdfFields.getKids();
for (Object kid : kids) {
if (kid instanceof PDCheckbox) {
PDCheckbox checkbox = (PDCheckbox) kid;
String Name = checkbox.getOnValue();
System.out.println(Name);
}
}
The above java code has been successfully working (successfully returns the children of Radiobuttons) but when the same logic is been implemented in c# its not running as expected
below is my c# code :
else if (pdfFields is PDRadioCollection)
{
System.Console.WriteLine("Radio Button" + " "
+ pdfFields.getFullyQualifiedName() + " ");
List kids = pdfFields.getKids();
System.Console.WriteLine(kids);
for (int w = 0; w < kids.size(); w++)
{
kids.get(w);
System.Console.WriteLine(kids.get(w)); ---- > Return null
System.Console.WriteLine(kids.get(w).ToString()); -- > Return null
}
}
Can any help out in this please
Thanks
At last got it work : here it is ::
C# : Need to type case with IEnumerable :
else if (pdfFields is PDRadioCollection)
{
System.Console.WriteLine("Radio Button" + " "
+ pdfFields.getFullyQualifiedName() + " ");
System.Collections.IEnumerable kids = (System.Collections.IEnumerable)pdfFields.getKids();
System.Console.WriteLine(kids);
foreach (object kid in kids)
{
PDCheckbox checkbox = (PDCheckbox)kid;
checkbox.check();
}

How can I add an unknownn number of dropdownlists programatically to a page and retrieve the selected values with c# on submit

I'm trying to do this and it is a bit confusing for me.
Basically the scenario is like this, I'm getting an XML from a 3rd party application with available dates for booking, for each day there are types of rooms the person can choose, single, double, etc.
Each hostel will return me an unknown number of room types. But dont get too confused with this.
The thing is simple I just need to add an unknown number of dropdownlists (or HTML Select) with the numbers of persons to book for. Now because I don't know how many of those dropdowns I will have I need to add them programatically inside a "for int i=0; i
How can I add an unknownn number of dropdownlists programatically to a page and retrieve the selected values with c# on submit?
The last column on the screenshot
http://i.stack.imgur.com/37chw.png
Update:
I'm creating the code from the xml results as a string that will print as html code:
XmlDocument xmlDoc2 = new XmlDocument();
xmlDoc.LoadXml(getPrices());
XmlNodeList prices = xmlDoc.GetElementsByTagName("RoomType");
string[] bookingDates = new string[Convert.ToInt32(Request.QueryString["nights"])];
string[] bookingDays = new string[Convert.ToInt32(Request.QueryString["nights"])];
bookingDates[0] = Request.QueryString["date"].ToString();
string[] dateArray = Request.QueryString["date"].ToString().Split('-');
DateTime initialDate = new DateTime(Convert.ToInt32(dateArray[0]), Convert.ToInt32(dateArray[1]), Convert.ToInt32(dateArray[2]));
bookingDays[0] = initialDate.DayOfWeek.ToString();
for (int z = 1; z < bookingDates.Length; z++)
{
DateTime nextDay = initialDate.AddDays(z);
string month = nextDay.Month.ToString();
string day = nextDay.Day.ToString();
if (day.Length == 1)
{
day = "0" + day;
}
if (month.Length == 1)
{
month = "0" + month;
}
bookingDates[z] = nextDay.Year + "-" + month + "-" + day;
bookingDays[z] = nextDay.DayOfWeek.ToString();
}
string pricesHeader = "<table width='100%'>";
pricesHeader += "<tr><td>Room Type</td>";
for (int x = 0; x < bookingDates.Length; x++)
{
string[] bookingDay = bookingDates[x].Split('-');
pricesHeader += "<td align='center'>" + bookingDays[x].Substring(0, 3) + "<br>" + bookingDay[2] + "</td>";
}
pricesHeader += "<td>Persons</td></tr>";
string pricesContent = "<tr>";
int dropNumber = 1;
foreach (XmlElement node in prices)
{
XmlNodeList roomTypeDescriptionN = node.GetElementsByTagName("roomTypeDescription");
string roomTypeDescriptionS = roomTypeDescriptionN[0].InnerText;
pricesContent += "<td>" + roomTypeDescriptionS + "</td>";
XmlNodeList priceN = node.GetElementsByTagName("price");
string priceS = priceN[0].InnerText;
XmlNodeList currencyN = node.GetElementsByTagName("currency");
string currencyS = currencyN[0].InnerText;
if (currencyS == "EUR")
{
currencyS = "&euro";
}
string avDates = "";
XmlNodeList availableDatesN = node.GetElementsByTagName("date");
int dateNumber = 0;
foreach (XmlElement avDate in availableDatesN)
{
avDates += availableDatesN[dateNumber].InnerText + ",";
dateNumber++;
}
for (int c = 0; c < bookingDates.Length; c++)
{
if (avDates.Contains(bookingDates[c]))
{
pricesContent += "<td>" + priceS + currencyS + "</td>";
}
else
{
pricesContent += "<td><center>X</center></td>";
}
}
pricesContent += "<td><select runat=server name='pers" + dropNumber + "' id='pers" + dropNumber + "'>" +
"<option>0</option><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option><option>7</option><option>8</option></select></td></tr>";
dropNumber++;
}
pricesLabel.Text = pricesHeader + pricesContent + "</table>";
I know that doing that and adding the runat=server won't help on my control, there is where my main problem is now, how to add the code on the html to be able to get the dropdownlist selected value later with c#. Can I do that with Request.Form ? was trying but so far I couldnt do it.
You can use the Repeater Class for generation of controls
You can also use the Request.Form Collection for obtaining of user's choice
You can use List for saving your created dropdownlists. On submit, you can read the data from your list.
List<DropDownList> ddlList = new List<DropDownList>{};
for(int i=0;i<count;i++)
{
//add control to page
ddlList.items.add(YourNewlyCreatedDdl);
}
for (int i = 0; i < 5; i++)
{
DropDownList ddl = new DropDownList();
ddl.ID=string.Format("ddl_{0}",i);
this.form1.Controls.Add(ddl);
}
This creates 5 empty DropDownLists.
You will need to rebuild and repopulate the controls on each postback in order for ASP to recognise their values.

Categories

Resources