I have a question I hope some of you might be able to answer, I haven't found any ways to do this on google or here.
What I want:
- A custom control that functions just like an input box. (But it has to be a winform control that can be added to a form. Not a form.)
- It has to be able to grab the value from its text box and send it to the parent in the function it was called in.
Here is how I want to call it:
string str = MyBox.GetString("control title");
Can anyone help?
I don't know if this is event possible in c#. I couldn't figure it out, but if anyone can please answer!
You want something like this
public partial class MyBox : Form
{
public MyBox()
{
InitializeComponent();
}
public string ResultText { get; set; }
public static string GetString(string title)
{
var box = new MyBox {Text = title};
if (box.ShowDialog() == DialogResult.OK)
{
return box.ResultText;
}
return string.Empty;
}
private void okButton_Click(object sender, EventArgs e)
{
this.ResultText = txtUserInput.Text;
this.DialogResult = DialogResult.OK;
}
}
where MyBox would be a Form with TextBox - txtUserInput and an okay button linked to the okButton_Click event.
And you can make calls from other forms like this:
string userInput = MyBox.GetString("Title for MyBox");
If you want the box to reside on a form, you can just use a regular TextBox to get the inupt. Maybe eclose it in a GroupBox to give a "title", add a description label.
Lastly, and most importantly, add an "Update" Button to the GroupBox. Inside this button's Click handler, you can retrieve the value of the textbox with string str = textbox.Text.
Related
I am making an app in which there are multiple UserControls stacked onto each other. So the elements go like this: MainForm -> User clicks on a UserControl1 on the MainForm which (UserControl1) has a panel on which there is displayed another UserControl2 with a button. When the user clicks on it, it displays another UserControl3 which is then displayed in the panel beneath the button, where finally the user enters some text in the textbox. I need the data from the textbox in the MainForm so I have MainForm and UserControls connected via EventHandlers and pass my ResponseModel in which there is some dat a that I need to pass to MainForm. The first time this works, an item is created and displayed, after the item there is this "button" (User controls) displayed, in case the user wants to create another one. But then comes the problem when the user types in a different text for a new item, it creates an item with the same text!! Like the textbox was never changed (I have a debugging point set on the constructor to see every time that the textbox is empty). Below is some code and an image, for you to see how this should work. Also when I first delete the item it then doesn't work to create a new item for some reason.
This is how I send the data from the last UserControl:
if (tbx_list_name.Text == "")
MessageBox.Show("You can't create new list without a name!", "Can't create new list!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
else
CreateListTextBoxHandler?.Invoke(this, new ListCreationResponseModel() {
Code = id, ListName = tbx_list_name.Text
});
This is how I create the last UserControl which has the textbox:
control.CreateListTextBoxHandler += GetHandlerData;
panel.Controls.Add(control);
And this is how I get the data one stage down (this practice continues through couple more stages back to MainForm):
public void GetHandlerData(object sender, ListCreationResponseModel e)
{
try
{
panel.Controls.Clear();
CreateListButtonHandler?.Invoke(this, e);
}
catch (Exception ex)
{
_ = new ErrorHandler(ex);
}
}
You seem to have a recursion here. GetHandlerData is added to the CreateListTextBoxHandler event (or delegate) and invokes CreateListTextBoxHandler again, which will call GetHandlerData again...?? But tbx_list_name.Text is passed to the model only once at the top level down to all the other calls.
You can fix this by passing a reference to the textbox instead of the text itself. Then you will always be able to retrieve the current text of the textbox.
public class ListCreationResponseModel
{
private readonly TextBox _listNameTextBox;
public ListCreationResponseModel(TextBox listNameTextBox)
{
_listNameTextBox = listNameTextBox;
}
public int Code { get; set; }
public string ListName => _listNameTextBox .Text;
}
Now, when you retrieve the ListName you don't get a stored value but the actual text of the textbox.
You can create the handler like this:
CreateListTextBoxHandler?.Invoke(this, new ListCreationResponseModel(tbx_list_name) {
Code = id
});
I'm trying to create an hangman game in UWP and I'm not quite sure where to type the button click event of each letter in the code in order to have it recognize all of the variables in MainPage without affecting functionality.
If possible to have the button clicks in a separate class, even better.
Would appreciate if you could help me, thanks in advance!
namespace Hangman
{
public sealed partial class MainPage : Page
{
int _currentIndex;
string _currentWord;
string[] _strArr = { "ant", "bee", "spider", "mosquito" };
int _difficulty = 1;
public MainPage()
{
this.InitializeComponent();
Random rnd = new Random();
_currentIndex = rnd.Next(0, 4);
_currentWord = _strArr[_currentIndex];
foreach (char c in _currentWord)
{
string _hiddenWord = string.Empty;
foreach (char ch in _currentWord)
{
_hiddenWord += "_" + (char)160;
}
_textBl.Text = _hiddenWord;
}
}
private void a_Click(object sender, RoutedEventArgs e)
{
}
}
}
I want to capture Button clicks of buttons on my XML code. I've made a property like so public char Key { get; set; } and had each Button click insert a different value dependant on the letter, for example: on button_a, Key = 'a';
I probably understand what you mean. You have a lot of buttons (like a button that makes up a keyboard). Each time you click the button, you get the button's identity and then type it into the text box.
You can use the Button.Tag property to record your key, like this:
<Button Tag="a" Content="A" Click="Button_Click" />
In code-behind, the Button_Click method has two parameters, where the sender refers to the Button that triggered the event, so you can convert it and get the Tag property.
private void Button_Click(object sender, RoutedEventArgs e)
{
var btn = sender as Button;
string tag = btn.Tag.ToString();
// Do Something
}
In this way, you can bind all the buttons to the same handler, which is very convenient.
Best regards.
You have to go to your XAML file where you design your UI, find the button and then add
<Button Content="Your Button Text" onClick="a_Click" />
there.
I am trying to change the Visible state of controls from the form shown event.
I am reading the name of the controls from the database table and accessing it using this.Controls["controlname"].Visible. But some of the controls are not able to access from within this event. It is showing exception.
How can I access the controls from form shown event?
Use Controls.Find() to search for it. As scheien pointed out, the control is probably inside a different container causing it not to be "found" with your original syntax. Here's a quick example:
private void Form1_Shown(object sender, EventArgs e)
{
string ctlNameFromDatabase = "textBox1";
Control[] matches = this.Controls.Find(ctlNameFromDatabase, true);
if (matches.Length > 0)
{
// ... do something with "matches[0]" ...
// you may need to CAST to a specific type:
if (matches[0] is TextBox)
{
TextBox tb = matches[0] as TextBox;
tb.Text = "Hello!";
}
}
else
{
MessageBox.Show("Name: " + ctlNameFromDatabase, "Control Not Found!");
}
}
EDIT:
For MenuItems you'll have to flag the control name in the database as a "menu item" and then use this code, where menuStrip1 is the name of your MenuStrip, to find them:
string menuName = "copyToolStripMenuItem";
ToolStripItem[] matches = menuStrip1.Items.Find(menuName, true);
if (matches.Length > 0)
{
matches[0].Visible = true;
}
The same code will work for ToolStrips as well. For example, replace menuStrip1 with toolStrip1.
I create a user control and add a textbox to it. In my windows form I add the user control i created and add a textbox and a button. How to copy the text I input from the textbox of Form to textbox of Usercontrol and vice versa. Something like
usercontrol.textBox1.text = textBox1.text
You could add to your User Control code a public property that delegates into the TextBox's Text property:
public string MyTxtBoxValue { get { return this.txtBox.Text; } }
And you could also have a setter to that, of course, if needed.
What you don't want to do, however, is exposing the whole TextBox by making it public. That is flawed.
From Form to Usercontrol
Form Code
public string ID
{
get { return textBox1.Text; }
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
userControl11.ID = ID;
}
Usercontrol Code
public string ID
{
set { textBox1.Text = value; }
}
There are multiple ways to access your user control text box data. One way to accomplish this would be to expose the text box on the user control at a scope that can be accessed via the form it's loaded on. Another way would be raising an event on the button click of the user control and subscribing to it on the parent form.
Although some stuff are inherited when creating a custom user control, for the most part you have to define your own properties. (like text value, etc..)
I would take a look at this:
http://msdn.microsoft.com/en-us/library/6hws6h2t.aspx
good luck!
I have created a custom message box with a textbox for input which appears under a certain condition in form1. I want form1 to hold the value of the textbox if the submit btn is clicked. I am not getting the desired result.
This is similar to this however I don't want the processing to happen in the message box because the process requires so many variables that I would have to transfer to the messsagebox.
Form condition
}
else //NOT ALL APPROVE
{
string BtnClicked = DenyEmpRequest.ShowBox(AllDenied, EmpRequestID);
if (BtnClicked == "1") //SUBMIT BTN WAS CLICKED
{
DenyEmpRequest emp = new DenyEmpRequest();
string reason = emp.Reason_Txt.Text;
}
I know that it is because I am creating a new instance of the form that I used in the messagebox when I said "DenyEmpRequest emp = new DenyEmpRequest();". I don't know any other way to access the textbox in the messagebox.
Messagebox code
public static string ShowBox(string DenyEmp, string RequestID)
{
newMessageBox = new DenyEmpRequest();
newMessageBox.EmpToDeny_lbl.Text = DenyEmp;
EmpRequestID = RequestID;
newMessageBox.ShowDialog();
return Button_id;
}
private void SubmitBtn_Click(object sender, EventArgs e)
{
if (Reason_Txt.Text == string.Empty)
{
NoReason_Lbl.Visible = true;
}
else
{
Button_id = "1";
newMessageBox.Dispose();
}
Looks like you're overcomplicating it. If you are just trying to retrieve a string from a custom MessageBox, just make a form with an OK/Cancel button and a text box. Make a public string property that wraps around the value of the text box's "Text" property. And make the form set it's DialogResult to DialogResult.OK if the OK button is clicked, DialogResult.Cancel if the cancel button is clicked.
Then you can call this form with code shown below:
using (CustomMessageBox myMessageBox = new CustomMessageBox())
{
myMessageBox.Text = "Initial text"; // optionally set the initial value of the text box
if (myMessageBox.ShowDialog(this) == DialogResult.OK)
{
someVariable = myMessageBox.Text;
}
}
This is the format you should be using.
EDIT:
In reference to your comment, if you have a form with a text box on it, just write the property like this:
public class CustomMessageBox : Form
{
public string Text
{
get
{
return textBox.Text;
}
set
{
textBox.Text = value;
}
}
}