I'm using code to create a dynamic text box, including assigning it a name, and then attempting to append new text to the textbox, however I'm receiving an error that the name doesn't exist in the current context.
Am I missing something simple, or have a done something wrong? I apologize if this is a basic thing I'm doing wrong; I'm still learning.
Here's the code in question:
TextBox dynamicTextBox = new TextBox();
dynamicTextBox.Name = "locBox";
dynamicTextBox.Multiline = true;
dynamicTextBox.Width = 300;
dynamicTextBox.Height = 40;
dynamicTextBox.Text = "Text ");
dynamicTextBox.ControlAdded += locBox;
locBox.AppendText = var1.ToString();
locBox.AppendText = var2.ToString();
The locBox is not defined. you have to remove the last two statement. and change the last statement after removal too.
replace "dynamicTextBox" to the form control instance .
{form instance name}.ControlAdded += locBox;
"{}" is a place holder.
I don't know how helpful this is.
Related
I'm creating a lot of buttons on the back end so I figured it would be intelligent to abstract it out to cut down on lines of code. When I try to do this though, it gives me an error saying "A local or parameter named "buttonName" cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter". Am I missing something here? I thought this was the exact reason to use a parameter in this situation.
Here is an example of what I'm trying to do.
turn this
Button buttonDetailsEdit = new Button();
buttonDetailsEdit.ID = "ButtonDetailsEdit";
buttonDetailsEdit.Text = "Edit";
buttonDetailsEdit.UseSubmitBehavior = false;
buttonDetailsEdit.Click += new EventHandler(EditCall);
PlaceHolderDetailsContent.Controls.Add(buttonDetailsEdit);
Button buttonDetailsBack = new Button();
buttonDetailsBack.ID = "ButtonDetailsBack";
buttonDetailsBack.Text = "Back to List";
buttonDetailsBack.UseSubmitBehavior = false;
buttonDetailsBack.Click += new EventHandler(IndexCall);
PlaceHolderDetailsContent.Controls.Add(buttonDetailsBack);
Into this (with a method call in place of the old code)
void CreateButton(string buttonName, string buttonIDText, string buttonText, PlaceHolder PlaceHolderName, string methodCall)
{
Button buttonName = new Button();
buttonName.ID = buttonIDText;
buttonName.Text = buttonText;
buttonName.UseSubmitBehavior = false;
buttonName.Click += new EventHandler(methodCall);
PlaceHolderName.Controls.Add(buttonName);
}
the methodCall portion also throws an error here, I'm assuming due to "methodCall" not existing in this instance.
You can't have two variables string buttonName and Button buttonName with the same name in the same scope.
Try to rename your button to, for example, Button newButton
You have declared a parameter named buttonName:
string buttonName
And you have declared a Button variable of the same name:
Button buttonName
You cannot have both. This is what the compiler is complaining about.
Now, actually, your issue seems to be that you want to use the content of the parameter as a local variable name. This is generally not possible - variable names must be known at compile time, not only at runtime.
Luckily, you do not actually need that - it does not matter what your local Button variable is called within your method, as that name will be lost when the method ends (actually, it will probably not even make it through compilation). The important part is that with each invocation of your method, a new Button instance will be created (and ultimately added to the list).
Therefore, simply rename your Button variable to newButton and drop the buttonName parameter because it isn't used any more.
The variable name buttonName was reused and the type of methodCall was incorrect. You need a delegate parameter to allow the selected function to be passed into the method.
void CreateButton(string buttonName,
string buttonIDText,
string buttonText,
PlaceHolder placeHolder,
EventHandler methodCall) // needed to fix your type
{
var button = new Button(); //reused variable name
button.ID = buttonIDText;
button.Name = buttonName; //you missed assigning the button name
button.Text = buttonText;
button.UseSubmitBehavior = false;
button.Click += methodCall; // you dont need to do new EventHandler
placeHolder.Controls.Add(button);
}
I have several controls in an open document, and i'm struggling to modify their values using interop.Word. Is it possible to get a contentcontrol via it's tagname, or am I barking up the wrong tree here?
This is the code i'm using:
Microsoft.Office.Interop.Word.ContentControl cc = (Microsoft.Office.Interop.Word.ContentControl)document.SelectContentControlsByTag("TheTagName");
cc.Range.Text = "My Data";
This works for me now:
object control = 1;
document.SelectContentControlsByTag("TagName").get_Item(ref control).Range.Text = "MyText";
I have a list of Accounts with some properties which are saved in a database.
On my Window I´m going to show the Username from the Account list
for (int i = 0; i < liste.Count; i++)
{
Label l = new Label();
l.Height = 30;
l.HorizontalAlignment = HorizontalAlignment.Left;
l.Content = liste[i].Username + "\n";
l.MouseDown += new MouseButtonEventHandler(Selectuser);
l.HorizontalContentAlignment = HorizontalAlignment.Left;
stack.Children.Add(l); // stackpanel in the xaml
SetKnownImageAcc(Convert.ToInt32( liste[i].AccAId)); // add a picture near the username
}
So I have now I MouseButton Event on every label I created but when I click on one of the usernames I the code behind I only can get the content of the Label so I don't know which Account it is exactly.
Sure i can make a loop to check liste[i].Username = this.content
But my Problem then is that i have more accounts and for example I have 2 accounts one facebook and one google but they have the same username how do I know now in the codebehind which account it is where I clicked
A friend said to me that I should try it with databinding but i´m new to coding so I don't know really how to do it or if this works with databinding.
Thanks for your help :)
One way you can do that is setting the Tag property of your label:
l.Tag = liste[i];
Then, you can get your user back in the subscribed method:
var myUser = (User)((Label)sender).Tag;
But your friend is right: Data binding is a much better way. In fact, in most cases, you shouldn't have any code in the code behind file (other than the automatically generated code). Try looking for MVVM on your favorite search engine.
I am writing a c# code for a Required field validator for a Multiline text box.
I have a issue in the run time:
when i won't enter any text inside the
text box
For first Click on submit (Button) it shows the error message
For second Click on submit it won't validate the text box and the form is submitted.
Same two issues when i even enter any
text inside the text box.
Overall it is not validating...
Please help me on what could be the possible bug in the below code.
txtReport = new InputFormTextBox();
txtReport.TextMode = TextBoxMode.MultiLine;
txtReport.RichText = true;
txtReport.RichTextMode = SPRichTextMode.Compatible;
txtReport.Rows = 5;
txtReport.Width = new Unit(200);
txtReport.ID = "txtReport";
txtReport.Text.Trim();
this.Controls.Add(txtReport);
reqVal = new RequiredFieldValidator();
reqVal.ID = "reqVal";
reqVal.ControlToValidate = txtReport.ID;
reqVal.SetFocusOnError = true;
reqVal.ErrorMessage = "*Comments field is mandatory";
reqVal.Enabled = true;
this.Controls.Add(reqVal);
Thanks in advance
From what it sounds like you are not re-adding the validator after the first submit, causing the second submit not to validate. But it's hard to tell from the fragment you posted (in what event/method is this being called?).
I'm writing a simple tic tac toe game for school. The assignment is in C++, but the teacher has given me permission to use C# and WPF as a challenge. I've gotten all the game logic finished and the form mostly complete, but I've run into a wall. I'm currently using a Label to indicate who's turn it is, and I want to change it when a player makes a valid move. According to Applications = Code + Markup, I should be able to use the FindName method of the Window class. However, it keeps returning null. Here's the code:
public TicTacToeGame()
{
Title = "TicTacToe";
SizeToContent = SizeToContent.WidthAndHeight;
ResizeMode = ResizeMode.NoResize;
UniformGrid playingField = new UniformGrid();
playingField.Width = 300;
playingField.Height = 300;
playingField.Margin = new Thickness(20);
Label statusDisplay = new Label();
statusDisplay.Content = "X goes first";
statusDisplay.FontSize = 24;
statusDisplay.Name = "StatusDisplay"; // This is the name of the control
statusDisplay.HorizontalAlignment = HorizontalAlignment.Center;
statusDisplay.Margin = new Thickness(20);
StackPanel layout = new StackPanel();
layout.Children.Add(playingField);
layout.Children.Add(statusDisplay);
Content = layout;
for (int i = 0; i < 9; i++)
{
Button currentButton = new Button();
currentButton.Name = "Space" + i.ToString();
currentButton.FontSize = 32;
currentButton.Click += OnPlayLocationClick;
playingField.Children.Add(currentButton);
}
game = new TicTacToe.GameCore();
}
void OnPlayLocationClick(object sender, RoutedEventArgs args)
{
Button clickedButton = args.Source as Button;
int iButtonNumber = Int32.Parse(clickedButton.Name.Substring(5,1));
int iXPosition = iButtonNumber % 3,
iYPosition = iButtonNumber / 3;
if (game.MoveIsValid(iXPosition, iYPosition) &&
game.Status() == TicTacToe.GameCore.GameStatus.StillGoing)
{
clickedButton.Content =
game.getCurrentPlayer() == TicTacToe.GameCore.Player.X ? "X" : "O";
game.MakeMoveAndChangeTurns(iXPosition, iYPosition);
// And this is where I'm getting it so I can use it.
Label statusDisplay = FindName("StatusDisplay") as Label;
statusDisplay.Content = "It is " +
(game.getCurrentPlayer() == TicTacToe.GameCore.Player.X ? "X" : "O") +
"'s turn";
}
}
What's going on here? I'm using the same name in both places, but FindName can't find it. I've tried using Snoop to see the hierarchy, but the form doesn't show up in the list of applications to choose from. I searched on StackOverflow and found I should be able to use VisualTreeHelper class, but I don't understand how to use it.
Any ideas?
FindName operates on the XAML namescope of the calling control. In your case, since the control is created entirely within code, that XAML namescope is empty -- and that's why FindName fails. See this page:
Any additions to the element tree after initial loading and processing must call the appropriate implementation of RegisterName for the class that defines the XAML namescope. Otherwise, the added object cannot be referenced by name through methods such as FindName. Merely setting a Name property (or x:Name Attribute) does not register that name into any XAML namescope.
The easiest way to fix your problem is to store a reference to your StatusDisplay label in the class as a private member. Or, if you want to learn how to use the VisualTreeHelper class, there's a code snippet at the bottom of this page that walks the visual tree to find the matching element.
(Edited: Of course, it's less work to call RegisterName than to use the VisualTreeHelper, if you don't want to store a reference to the label.)
I'd recommend reading the first link in its entirety if you plan on using WPF/Silverlight in any depth. Useful information to have.
You have to create a new NameScope for your window:
NameScope.SetNameScope(this, new NameScope());
Then you register name of your label with the window:
RegisterName(statusDisplay.Name, statusDisplay);
So this seems to be all you need to do to make FindName() work.