I am trying to add multiple items in to a grid, so that later items will pop up underneath earlier items.
TextBox foo = new TextBox();
foo.Text = " " + id + " ";
foo.Width = 440;
foo.Height = 200;
foo.VerticalAlignment = "Top";
foo.Background = new SolidColorBrush(Colors.Red);
((Grid)daysPanels[id].Content).Children.Add(foo);
Unfortunately, the VerticalAlignment = "Top" line isn't working, it says it can't implicitly convert type "String" to type "VerticalAlignment". Am I doing this the right way and how can I fix that error?
Look at the msdn documentation. VerticalAlignment is an enum.
foo.VerticalAlignment = VerticalAlignment.Top;
Related
I create a label and set his TEXT color to red. I use ForeColor property but with him not working :(
This is my UI:
This is my label code:
Label reqF = new Label();
reqF.Text = "*";
reqF.ForeColor = System.Drawing.Color.Red;
reqF.CssClass = "formvalidation";
reqF.Font.Size = 15; // 15px
This is my string code:
TableHeaderCell header1 = new TableHeaderCell();
header1.Text = "Destination" + reqF.Text; <----------- My label
tRow0.Cells.Add(header1);
In this line
header1.Text = "Destination" + reqF.Text;
you add only the text (*) to the destination, not the full div with the style on.
The easiest (and faster) way is to direct add the style online as:
header1.Text = "Destination <span style=\"color=red\">*</span>" ;
You are only using text of the control, not the whole control with styling. Your code should actually look something like that:
TableHeaderCell header1 = new TableHeaderCell();
LiteralControl literal = new LiteralControl();
literal.Text = "Destination";
header1.Controls.Add(literal);
header1.Controls.Add(reqF);
tRow0.Cells.Add(header1);
Note that here Literal control is used to insert the Description string, and then the whole Label with the * is being inserted.
I have a string containing some text (note the \n),
string mText = "Hello\nWorld";
When I apply this to a TextBlock (generated by code) like this:
TextBlock tb = new TextBlock();
tb.Foreground = (Brush)App.Current.Resources["PhoneForegroundBrush"];
tb.FontSize = (double)App.Current.Resources["PhoneFontSizeMedium"];
tb.Margin = new Thickness(24, 32, 24, 12);
tb.TextWrapping = TextWrapping.Wrap;
tb.Text = mText;
I don't see the second line. Any ideas?
[UPDATE - Added the usage of this code]
I use this in showing a PopUp on the Screen:
// Create PopUp Content:
TextBlock tb = new TextBlock();
tb.Foreground = (Brush)App.Current.Resources["PhoneForegroundBrush"];
tb.FontSize = (double)App.Current.Resources["PhoneFontSizeMedium"];
tb.Margin = new Thickness(24, 32, 24, 12);
tb.TextWrapping = TextWrapping.Wrap;
tb.Text = pMessage;
Grid grid = new Grid();
grid.Background = (Brush)App.Current.Resources["PhoneAccentBrush"];
grid.Children.Add(tb);
grid.Width = page.ActualWidth;
// Create PopUp itself:
Popup popup = new Popup();
popup.Child = grid;
// Show PopUp:
SystemTray.BackgroundColor = (Color)App.Current.Resources["PhoneAccentColor"];
popup.IsOpen = true;
You could also add the following:
tb.Text = mText + Environment.NewLine;
or
tb.Text = mText + "
" + "
"
that seems to insert the <LineBreak/> into.
I wanted to display no of course json objects in each textbox
but as they are unpredictable number of objects therefore i created textbox on the fly using this code
List<Course> Cdata = JsonConvert.DeserializeObject<List<Course>>(App.data);
TextBox[] Tblock = new TextBox[Cdata.Count];
double top = 0; int i = 0;
foreach (Course de in Cdata)
{
result += de.course_name + "\r\n";
result += "Total Absents = " + de.absents;
result += " + " + de.presents;
result += " = " + de.sessions + "\r\n\r\n\r\n";
Tblock[i] = new TextBox();
Tblock[i].Text = result;
Tblock[i].AcceptsReturn = true;
Tblock[i].TextWrapping = TextWrapping.Wrap;
Tblock[i].Width = 475;
Tblock[i].Height = 270;
Tblock[i].IsReadOnly = true;
Tblock[i].Margin =new Thickness (0,top,0,0);
Tblock[i].Visibility = System.Windows.Visibility.Visible;
Tblock[i].VerticalAlignment = System.Windows.VerticalAlignment.Top;
top += 270; i++;
result = "";
}
Now when i debug my app data it is working as its supposed to the only problem is textbox
never display on View
and i haven't coded any textbox in Xaml file of view
Thanks in Advance
You have to add the Textboxes to any existing Panel (generally to Grid or StackPanel) in the XAML as shown below
StackPanel sp = new StackPanel(); //Create stack panel before foreach loop
foreach (Course de in Cdata)
{
//your code which you shown above
sp.Children.Add(Tblock[i]); //Add all the Textboxes to the stackpanel
}
ContentPanel.Children.Add(sp); //And add the above stackpanel to the existing Grid named ContentPanel
By the way, I suggest you to use a ListBox with ItemTemplate to bind the data instead of creating the TextBoxes as shown above.
Also, I don't understand why you have choosen TextBox instead of TextBlock to display data
I have a problem I seem to stumble over all the time, I have a Drop Down box and you can select a number which creates x number of textboxes with images buttons its for a survey it the image buttons are used to create "Sub-Answers" so they can have answers to answers so my question is I need to when they hit the image button to create a textbox under the orginal textbox here is the code.
for (Int32 i = 1; i <= NumberOfAnwsers; i++)
{
Literal l1 = new Literal();
l1.Text = "<tr><td>Answer " + i + " text.</td><td>";
TextBox tb = new TextBox();
tb.ID = "TextBoxAnswer" + i;
tb.EnableViewState = false;
tb.Width = 300;
Literal l3 = new Literal();
l3.Text = "</td><td>";
Literal l2 = new Literal();
l2.Text = "</td></tr>";
RadColorPicker CPI = new RadColorPicker();
CPI.PaletteModes = PaletteModes.WebPalette;
CPI.ID = "RadColorPicker" + i;
CPI.ShowIcon = true;
CPI.SelectedColor = System.Drawing.Color.Black;
ImageButton IBVideo = new ImageButton();
IBVideo.ID = "IBVideo" + i;
IBVideo.ImageUrl = "/images/video-icon.jpg";
IBVideo.ToolTip = "Add Video";
IBVideo.Height = 20;
IBVideo.Width = 20;
ImageButton IBAdd = new ImageButton();
IBAdd.ID = "IBAdd" + i;
IBAdd.ImageUrl = "/images/add-icon.png";
IBAdd.ToolTip = "Add Sub-Answers";
//IBAdd.OnClientClick = "showDialog(" + i + ");return false;";
IBAdd.Height = 20;
IBAdd.Width = 20;
//Add Textbox
PanelAnswersToQuestions.Controls.Add(l1);
PanelAnswersToQuestions.Controls.Add(tb);
PanelAnswersToQuestions.Controls.Add(l3);
PanelAnswersToQuestions.Controls.Add(CPI);
PanelAnswersToQuestions.Controls.Add(IBVideo);
PanelAnswersToQuestions.Controls.Add(IBAdd);
PanelAnswersToQuestions.Controls.Add(l2);
}
As you can see I just add controls to the panel, I need to know when that ImageBUtton is hit I can add a Textbox and in this case it could be more then just one textbox to it.
I hope this is clear but for some reason I dont think it is ... sorry.
I have added a radwindow and poping that up sending the Data to the partent via javascript the which created a new problem for me, I can not in javascript seem to find the dynamicly created hiddenfield
function OnClientClose(radWindow) {
var oWnd = $find("<%=RadWindowAddSubAnswer.ClientID%>");
var SubAnswerValues = oWnd.get_contentFrame().contentWindow.document.forms(0).HiddenFieldSubAnswers.value;
alert(SubAnswerValues);
var AnswerID = oWnd.get_contentFrame().contentWindow.document.forms(0).HiddenFieldAnswerID.value;
alert(AnswerID);
var HiddenName = "HiddenFieldSubAnswers" + AnswerID;
alert(HiddenName);
document.getElementById(HiddenName).value = SubAnswerValues;
$get("DivSubAnswers" + AnswerID).innerHTML = SubAnswerValues;
}
The "document.getElementById(HiddenName).value = SubAnswerValues;" seems to never be found, I also tried $get(HiddenName).value = SubAnswerValues; that does not seem to work either both come back as null as for the code behind its:
HiddenField HFSubAnswers = new HiddenField();
HFSubAnswers.ID = "HiddenFieldSubAnswers" + i;
HFSubAnswers.Value = "0";
Im not sure if I got your question right but if you need to dynamically add controls on a Page here is what I can say.
Before adding your control I guess you need to find the control where you need to add it on, Add the control then assign the properties.
PlaceHolder myPlaceHolder = (PlaceHolder)Page.FindControl("PlaceHolder1");
myPlaceHolder.Controls.Add(myButton);
myButton.Text = "Hello World";
For a more detailed expalnation go here http://anyrest.wordpress.com/2010/04/06/dynamically-removing-controls-in-a-parent-page-from-a-child-control/
I would like to fill my ListView with do aligned elements, a little icon (either a confirm mark or a cross) and a string which contains the result of a question (Right / Wrong, that's why icon).
daInserire = new ListViewItem();
daInserire.Foreground = new SolidColorBrush(Colors.DarkGreen);
daInserire.Content = "Giusto: "+ straniero.Text + " = " + vocItaliano[current];
daInserire.HorizontalContentAlignment = HorizontalAlignment.Center;
daInserire.FontSize = 18;
//...
listViewConInfo.Items.Add(daInserire);
This works perfectly, I'd like to add before the string, on the same line an image.
Looks like you're using WPF, so you'll need to create a StackPanel for your Content property and add the Image and a Label to that StackPanel.
ListView lV = new listView();
ListViewItem Item = new ListViewItem();
WrapPanel wrap = new WrapPanel();
Image image = new image();
image.Source = <<yourSource>>;
Label label = new Label();
label.Content = "W/E you want";
wrap.Children.Add(image);
wrap.Children.Add(label);
Item.Content = wrap;
lV.Items.Add(Item);