how to add the text of a label to code dom? - c#

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

Related

C# Read specific line from file and take text from it

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!

How can I grab a textfield value in Ext.Net?

I know there's a lot of information on this, but mine doesn't seem to be working out so well. I have a form that's created in C# in a form panel. My textfields are created like below:
Ext.Net.Panel panel = new Ext.Net.Panel();
Field field = null;
field = new TextField();
field.Name = "FieldTitle";
field.ID = "FieldID";
panel.Add(field);
this.searchform.Add(panel);
this.searchform.Add(new Ext.Net.Button()
{
Text = "Search",
Handler = "getID();"
});
When the search button is hit, then a JavaScript function is called which performs a store reload:
Ext.getCmp("#Gridpanel").getStore().reload({});
On the reload, I want to read the form fields and use the text for another part of the code. However, the below code doesn't seem to be working:
if(X.isAjaxRequest){
var ctrl = X.GetCmp<TextField>("FieldID");
string value = ctrl.Value.ToString();
}
I can get inside the 'if' statement, but ctrl comes back as null. Is there something else I need to include to grab the text field data?
EDIT
So I'm realizing that I'll have to pass an array of the search field ID's to a JavaScript function, then send a dataset back to the server side in order to implement the search. Just to throw it out there, is there a way to dynamically create controls (ie; a TextField) in C# and then get the value from those controls after an event is fired (ie; a button click)?
You can try to get the textfield using
Ext.getCmp("#FieldID") or X.getCmp("#FieldID"). FieldID is the client ID for TextField.
Use developer Tools to check the ID for the TextField

Highlight selected text and save it

I am interested in saving text when user selects any text anywhere on a web page that text has to be highlighted and has to save that text as a string in C#.
when same user saw same page next time text has to be highlighted as done previously by him.
If anyone knows a simple elegant way to do this I would really appreciate it, but I'll take any solution at this point. Even if you can point me in the right directions that would be appreciated.
Thanks in advance.
You need to write service WCF or Webservice in server side that will receive userId and text and save it into database.
[WebMethod(Description = "Save Text")]
public string Savetext(int userId ,string text)
{
}
Second method will retrive the text from daatabase by user id
[[WebMethod(Description = "Get text")]
public string GetText(int userId)
{}
In client side do invokation using Ajax calls (Jquery)
Use this to get selected text from page: http://mark.koli.ch/2009/09/use-javascript-and-jquery-to-get-user-selected-text.html
Then in mouseup event copy it to some HiddenField. Now you need a button or maybe AJAX call in mouseup so you can send it to server. On the server side save it to DB along with UserID and page address or ID.
On the next visit of this user to this page check in DB for entry. If it exist put the text in some hidden field. Then using jQuery get that text clientside, find it on page (using regex or something) and select it. You should remember to disregard any HTML markup while finding the text which can be tricky...
That is general way I would take to do this.
you can do that by capturing the selected text and send it via ajax call to your database.
check this example to know haw you can capture the selected text.
If you use jquerythen you will use select() function to capture the selected text
<textarea id="txt"></textarea>
<script>
$(document).ready(function(){
$('textarea').select(function()
{
var selectedText=window.getSelection();
//here put the ajax call to your webservice
});
});
</script>

Open a new window using MapAreaAttributes in microsoft chart control

i am using MS asp.net 3.5 chart control (Pyramid) and on the click of the series/datapoint i need to open a URL in a new window, something like javascript window.open.
Now i have tried a hell lot but that doesn't work. I am not able to give javascript to the datapoint.
Secondly i got to know that MapAreaAttributes could be given to Series as mentioned below if a new window needs to be open
series.MapAreaAttributes= "target='_blank'";
But even this doesn't works????
Guide me! Thanks
I had your same problem just now.
Here is the solutions and it works:
Notice in your code that you're using the single quotation ('). It seems that this is not allowed by the chart control or something. Let me give you an example that might help you understand:
Let's assume you have a JavaScript function that opens a window showing some data when the user clicks on a column (point) in your data (series). You can do it like this:
Chart1.Series["MySeries"].Points[0].Url = "javascript:void(0)"; //this is just to tell the browser not follow a URL, since you will control this with your javascript
Chart1.Series["MySeries"].Points[0].MapAreaAttributes = "onclick=\"OpenWindow();\""; //this is to set the onclick attribute to fire your javascript function when the user clicks your column.
In the above example in the second line of code, notice that I have used double quotation instead of single ones. If you wrote it like this :
"onclick=\'OpenWindow();\'";
it will never work! You have to use double quotations...
Also, since I am a C# developer, you have to use the \" code to write double quotations otherwise, you will get compiler error.
I hope this helps!

How to insert text from a text file into a Label Control in C#

Could someone demonstrate how to insert text from a text file e.g. test.txt into a Label control on a visual C# form please
You leave much to the imagination as to where you currently are with this and from which point you need help, but in the simplest form, try this:
theLabel.Text = File.ReadAllText(pathToFile);
label.Text = File.ReadAllText("test.txt");
File.ReadAllText Method
The answers for the question are useful so far.
Also you are considering this for WinForms as far as I understood from the tags.
If you would like to do any kind of action like this on Web Forms, you should consider that the text from the file goes without any encoding to label control. So, any kind of JavaScript code can be injected.
Always HtmlEncode your text;
var pathToFile = Server.MapPath("~/poo.txt");
lblPoo.Text = HttpUtility.HtmlEncode(File.ReadAllText(pathToFile));

Categories

Resources