I'm creating a program that allows you to manage a game server, now the main settings of it exist in a file called server.properties. I'm making a form which is able to change these settings. However, I want to be able to load the current settings into different things.
For example, if a line is spawn-npcs=true in the server.properties file, I want to be able to apply that to a CheckBox and make the CheckBox Checked. Or if something uses numbers then a TextBox would need to contain that so if a line is server-name=Funhuas in the server.properties file a certain TextBox would need to only read Funhaus.
I am using this code to read the lines I want:
string line = File.ReadLines("server.properties").Skip(14).Take(1).First();
And I am using this code to change the value of that line:
var lines = File.ReadAllLines("server.properties");
lines[6] = "spawn-npcs=" + textBox1.Text;
File.WriteAllLines("server.properties", lines);
The code above seemingly works fine however. Also, all this data will be entered when the user presses a Save button. Hope I've provided enough info, thanks!
Related
I am working on a test for a Web Application. I want to simulate the Copy command and verify the value of the Clipboard.
I have two ways to simulate this:
I simulate "Ctrl+C" by using this code:
System.Windows.Forms.SendKeys.SendWait("^{c}");
I use a button on my App which executes Copy on some text and puts it on the Clipboard
These two work and after using one of them, I can do "Ctrl+V" and it pastes the text correctly.
On my test, I am supposed to verify that the Clipboard contains the correct value.
I am using this code to check if the Clipboard is not empty and that it contains the correct string:
Clipboard.ContainsText(); // verify that Clipboard is not empty
Clipboard.GetText(); // verify that string on the Clipboard contains the good string
But after I simulate a Copy (with one of options above), the code just above returns respectively:
false
""
Does anyone have a solution to fill the Clipboard and to see its contents?
Ok I added this on the attribute of my test :
[Apartment(ApartmentState.STA)]
I can access the value of the Clipboard now.
(Source: https://docs.nunit.org/articles/nunit/writing-tests/attributes/apartment.html)
You can use Clipboard.SetText("your text here") to manually set text to the clipboard.
Afterwards, you can use Clipboard.ContainsText() and Clipboard.GetText() to check the clipboard contents as you mentioned above.
See the documentation here.
I am currently trying to integrate a weather module into my software from a freely available source and i can get my form to load if i write the code as follows:
WebBrowser1.DocumentText = "<html><img src=http://www.7timer.info/bin/astro.php?lon=-2.36&lat=55.91&lang=en&ac=0&unit=metric&tzshift=0 </html>";
What i want to do however is be able to dynamically alter the html code by using global variables defined in the parent form to insert the corrected lattitude and longitude into the code. The idea being that the user can enter their Lat/Lon on the parent form where it is used for other purposes but those values will then be the base for generating a corrected local weather forecast.
The code i have to do this is:
string myweather = "<html><img scr=http://www.7timer.info/bin/astro.php?lon=" + Global_Variables.Longitude + "&lat=" + Global_Variables.Lattitude + "&lang=en&ac=0&unit=metric&tzshift=0 </html>";
WebBrowser1.DocumentText = myweather;
Method 1 works just fine and the form populates with the weather forecast but when i use method 2 the form is always blank. I have used the debugger and written out the value generated by metho2 2 and it returns exactly the same as what was defined in method 1. I have tried various iterations such as
WebBrowser1.DocumentText = (char)34+myweatheer+(char)34
and also having it as a variable instead of a string but nothing i seem to do makes any difference and the (char)34 method only drops quotation marks into the form with a gap inbetween and the rest of the form is blank. I really cant understand how a different method that returns exactly the same value does not seem to work?
There is literally no more code within the form itself as it's only purpose is to be a pop up child form that displays a graphical weather forecast. When a button is clicked on the parent form then this one opens up and contains only a web browser container with this html code inserted into the container. If i go to the website referenced in the code there are option for setting parameters and generating the html string that can be copied and pasted into your own website if you wish. Alternately you can write your own string in the correct format to make the same query and return an image with the weather forecast. In fact if i take the resulting output of "myweather" without the "" and paste it into a web browser i do actually get the web browser showing exactly the image i am expecting. I have edited the original post to be clearer as to the default container name instead of my custom name.
i am trying to create a contract (document) that I can print like a normal word document.
I have a form which is my 'entry form') where i can type in details such as customer name, amount,date of contract etc. these information is then saved in ms access.
i have another form (which i call 'contract doc'). it has labels in it and the label holds the information i typed in my 'entry form'. I assigned one label to get the values that i entered in the textbox in the entry form.
contractdoc.contract_date_label.Text = contract_date_tb.Text;
contractdoc.deposit_label.Text = deposit_tb.Text;
contractdoc.customer_name_label.Text = customer_name_tb.Text;
i also added labels and typed the rest of the documents body and position them in the 'contract doc form ' to look like an actual contract.
but i dont know how to print it like a document. i iused :
printForm1.Print();
but what happens is, it asks me to save in xls format and only a messege box that says:"printing page 1 of document" with a cancel button.
I hope you can help. thank you in advance
I presume from the name printForm1, that you're using the PrintForm class provided as part of the Visual Basic PowerPack.
In that case, you need to make sure that the PrintAction property is set appropriately. To print to a physical printer, you need to set it to PrintAction.PrintToPrinter. Depending on what printer is configured as the system default, you might also need to set the PrinterSettings property. Then when you call the Print method, it will print to the correct printer.
It's probably prompting you to save the file in XLS format because your system default printer is a virtual one that generates XLS files.
I have a asp.net form with 5 HTML file input controls with runat=server and a submit button. after user selects the files and clicks the submit button, the files are upload in the server.
Problem is the HTML file input controls are editable, and user can edit the path after he has already selected the file from the browse button.
If he enters a invalid file path, the file is not uploaded because it does not exist.
How can I stop users from manually changing the file path? I have tried to make the controls read only, but it disables the browse button also.
You can't. Write your server side logic to cope with missing uploads.
you could try something like having a hidden field and when the user selects the file from browse, it populates to the hidden field as well. Then when they upload, you either read from the hidden field for your upload or using JS replace the values back to the original control and read from that.
Is this what you want to have...
You can do like that hiding the control when user select the file
and showing only the path to the user
in a div
if he disagree, let him click on a link called Choose Another next to the path div, so when user click Choose another toggle the same div with flushing the previous value
see this fiddle : http://jsfiddle.net/T2D3X/
While brian's suggestion works (Thank you brian for this), finally we decided to follow the server side handling approach. since in case of invalid file, the file size would be zero, so the response is instant and we do not need to wait till the file is uploaded because in this case there is not file in the first place.
//check if file exists
if (uploadCtrl.PostedFile.ContentLength > 0)
{
uploadCtrl.PostedFile.SaveAs(fileNameWithPath);
uploadCtrl.Dispose();
}
else
{
fileNameWithPath = "invalid";
}
You can also use these methods:
One before validating: http://jsfiddle.net/T2D3X/1/
One After validating: http://jsfiddle.net/T2D3X/2/
Friend of mine posted the same answer before I have re-modified it.
There are some internal jsfiddle error on second link you can ignore that, i have just added more functionally to check on submit what value exactly you are sending.
Let us know if you find the it helpful ;)
Thank!
I have a form in which there are various labels and a button..on the button click event there is a code written which generates a cs file in which i want the text of the label to be displayed..
I am trying to get the values with the help of the following function in the code dom but m not able to extract the values of the label i.e. i am just getting label1.text, label2.text, etc. instead i want the values that are there in the labels and the combobox..
can anyone please help..
start.Statements.Add(new CodeVariableReferenceExpression("Info.Valid("\"combobox1.SelectedValue.ToString()\"", "\"label1.Text\"", "\"label2.Text\"", "\"label3.Text\"", "\"numericupdown.Value.ToString()\"")");
here start is the CodeMemberMethod to which all the statements are to be added, Info is another class and Valid is a method to which i need to pass all these values as arguments..
Thats right, your code shouldnt extract any values because you specifies text constants. You may use string.Format method to prepare text data. Try something like below:
string pattern = "Info.Valid(\"\"{0}\"\", \"\"{1}\"\", \"\"{2}\"\", \"\"{3}\"\", \"\"{4}\")";
string data = string.Format(pattern,
combobox1.SelectedValue.ToString(),
label1.Text,
label2.Text,
label3.Text,
numericupdown.Value.ToString());
start.Statements.Add(new CodeVariableReferenceExpression(data));
For more details check out this