How to pass value from Form to UserControl? - c#

I need to pass a value from Form to UserControl.
I have tried to:
1.(and i make basketBox public)
Form:
UserControlBasket us1 = new UserControlBasket();
public void button1_Click(object sender, EventArgs e)
{
us1.basketBox.Text = g1.Name;
}
2.
Form:
UserControlBasket us1 = new UserControlBasket();
public void button1_Click(object sender, EventArgs e)
{
us1.Txt = g1.Name;
}
UserControl:
public string Txt
{
get { return basketBox.Text; }
set { basketBox.Text = value; }
}
3. And i have tried to do like here:
Pass value from Usercontrol to Form
I expect that in basketBox(it is a textBox) will be value from a g1.Name;

I just add a Controls.Add(us1) and it solved my problem.

I am just summarizing the comments (from #Jimi) with my answer here. Both the number 2 and 3 Solutions are acceptable and correct by design principles. It seems that a different object of user control was accessed here instead of using the one that placed the from.

In your case, you should better use textbox, which is attached to page and dont use a UserControl.

Related

C# .NET Substitutes for UserControl Panels

I'm fairly new to GUI-development in C# .NET, using WinForms, and I'm creating an application with one form, (like a main menu), and with different "sub-menus", that can all be opened in the same form. This is what I currently have:
Application with UserPanel1 open.
Application without any UserPanel selected.
I've seen some shady youtuber use UserControls for this, and that's what I'm currently working with. I'm using a TableLayoutPanel, with the UserControls dropped into it.
But I have some problems with this. First, I don't know if this is the correct way to use UserControls and if this will throw any errors.
Second of all, I don't know the exact way to use multiple UserControls in one form, because I can't drag multiple UserControls in a single cell of a TableLayoutPanel, and if I instantiate a new UserControl using some simple code, it won't display all of the custom controls I previously placed (in that UserControl). Here is that code:
private void button1_Click(object sender, EventArgs e)
{
Form1_UserControl1 form1_UserControl1 = new Form1_UserControl1();
form1_UserControl1.Show();
form1_UserControl1.BringToFront();
form1_UserControl1.Dock = DockStyle.Fill;
}
I know that this is probably a simple thing that I'm doing wrong, but I only need some simple advice.
Am I using the right control, or is there a better/easier way to achieve what I'm looking for?
Initialize all UserControls to private fields
TableLayoutPanel add all these UserControls
Use buttons to hide/show them.
My sample code:
public partial class Form1 : Form
{
private UserControl[] myUserControls = new UserControl[3];
public Form1()
{
InitializeComponent();
InitializeUserControls();
}
private void InitializeUserControls()
{
for (int i = 0; i < myUserControls.Length; ++i)
{
myUserControls[i] = new UserControl();
myUserControls[i].Hide();
myUserControls[i].BringToFront();
myUserControls[i].BackColor = Color.Blue;
myUserControls[i].Dock = DockStyle.Fill;
tableLayoutPanel2.Controls.Add(myUserControls[i]);
}
}
private void button1_Click(object sender, EventArgs e)
{
myUserControls[0].Visible = !myUserControls[0].Visible;
}
private void button2_Click(object sender, EventArgs e)
{
myUserControls[1].Visible = !myUserControls[1].Visible;
}
private void button3_Click(object sender, EventArgs e)
{
myUserControls[2].Visible = !myUserControls[2].Visible;
}
}

can't able to add value to textbox

I have a datacontrol.cs(UserControl), which is contain textbox1 and codebehind window having a method with parameter of currentvalue
public void bindvalue(float currentvalue)
{
textbox1.Clear();
textbox1.Text = currentvalue.ToString();
}
I have a Form, Here added the usercontrol in this form and which contains a Button
So when clicking button it pass a currentvalue by the way of method to the datacontrol class like that .
private void button_click(object sender, EventArgs e)
{
float currentvalue = 1500.00f;
datacontrol obj = new datacontrol();
obj.bindvalue(currentvalue);
}
Everything working fine to me. It pass the current value to the usercontrol class and there current value assigned/added to the textbox1.Text = currentvalue.ToString();. It doesn't shows any error . But finally the textbox doesn't shows any value.
I used breakpoint to check the functionality. It gave current value to the textbox. But strange!!!..
I can't predict whats wrong in my code.
Helps appreciated.:)
Your instance of datacontrol with required value (1500.00f) does not exist on your form. You are only delcaring it, passing value and forgetting about it.
If you have already added user control to form and want to call bindvalue method of existing control, you should do the following:
private void button_click(object sender, EventArgs e)
{
float currentvalue = 1500.00f;
this.dataControl1.bindvalue(currentvalue);
}
Note that dataControl1 is the name of your user control on the form, it can be different from dataControl1.
If you want to create new user control and call bindvalue, you should add new instance on the form:
private void button_click(object sender, EventArgs e)
{
float currentvalue = 1500.00f;
datacontrol obj = new datacontrol();
obj.bindvalue(currentvalue);
this.Controls.Add(obj);
}
If it is already dynamically added control on the form, declare a field of Form class, assign new instance of it control, when you want it, and call to it as it shows in the first example.
You need add instance of datacontrol to the form
datacontrol obj = new datacontrol();
obj.bindvalue(currentvalue);
Controls.Add(obj);
You can simply make a call to bindvalue method by using the tagename of your user control in button click event handler.
Suppose the tag name is 'datacontrol', then you should use the following lines of code to achieve your task:
protected void Button1_Click(object sender, EventArgs e)
{
float myvalue = 150.50f;
datacontrol1.BindValue(myvalue);
}
Regards, Aamir ( .Net developer)
Please marks this thread as answer if it solved what were looking for... thanks

How to avoid the MaskedTextBox PromptChar being removed, when its container modal Form is shown?

Using VS 2015 and C#...
I have this simple modal Form with just a MaskedTextBox control on it.
Each time after the first that ModalForm is shown with .ShowDialog(), the PromptChar in the control is gone.
To reproduce this issue:
public ModalForm()
{
InitializeComponent();
maskedTextBox1.Mask = "00/00/0000"; // happens with any
maskedTextBox1.TextMaskFormat = MaskFormat.IncludeLiterals;
}
Code for main Form:
public partial class Form1 : Form
{
private ModalForm modalForm = new ModalForm();
private void button1_Click(object sender, EventArgs e)
{
modalForm.ShowDialog();
}
}
The control's prompt appears again when its content changes, but at first view isn't present.
Setting the TextMaskFormat property to IncludePromptAndLiterals could be a solution, but then, .Text has to be cleaned up.
Is there another way to handle this?. Has become necessary for me, that all MaskedTextBox controls must always show its default prompt.
Try this on Form's Shown event,
private void ModalForm_Shown(object sender, EventArgs e){
if (!maskedTextBox1.MaskCompleted) // if there is missing parts it will return false, every false means prompts need in control
{
string tempText = maskedTextBox1.MaskedTextProvider.ToDisplayString(); // get the last value with prompts
maskedTextBox1.Text = "";
maskedTextBox1.Text = tempText; // then set the last value.
}
}
Hope helps,

Pass parameter in an already opened form in C#

I want to pass a parameter to the textbox. I have the following code and it is passing the parameter but not the way I want.
My main form in already open and I want to pass the parameter from my search form. when I do with the code below it opens mt 1 more main form and the parameter is shown in there. I want to by able to show in the opened main form.
When I erase frmMain.Show(); nothing happens.
Main frmMain = new Main();
artikal = "TEST TEST";
frmMain.ed_artiakal.Text = artikal;
frmMain.Show();
any suggestions?
You have many variants to solve your problem.
Option 1
Define and use custom event.
Search form code:
public event EventHandler ArtikalTextChanged;
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (ArtikalTextChanged != null)
ArtikalTextChanged(this, EventArgs.Empty);
}
Main form code:
private void button1_Click(object sender, EventArgs e)
{
Search search = new Search();
search.ArtikalTextChanged += OnArtikalTextChanged;
search.Show();
}
private void OnArtikalTextChanged(object sender, EventArgs e)
{
this.ed_artiakal.Text = (sender as Search).textBox1.Text;
}
Don't forget to make textBox1 of Search form public.
Option 2
Get instance of your main form in search form:
Search form code:
private void textBox1_TextChanged(object sender, EventArgs e)
{
var mainForm = Application.OpenForms.OfType<Main>().FirstOrDefault();
mainForm.ed_artiakal.Text = textBox1.Text;
}
Main form code:
private void button1_Click(object sender, EventArgs e)
{
Search search = new Search();
search.Show();
}
Don't forget to make ed_artiakal control public in your Main form.
Option 3
Share data between forms (recommend)
But if you application is large and you want to make it scaleable and flexible I recommend you to use data-binding technique to share data between forms without coupling them. You can read more at articles: http://msdn.microsoft.com/en-us/library/h974h4y2(v=vs.90).aspx
I have solved my problem in the following way.
On my Search Form I created a public string and when I showed the form I referenced to that string in my case GetItemCode.
The key here was to use ShowDialog() and not to use Show().
SEARCH FORM
Search frmSearch = new Search();
frmSearch.ShowDialog();
ed_artiakal.Text = frmSearch.GetItemCode;
MAIN FORM
public string GetItemCode
{
get { return Artikal; }
}
Now when I close the search form the value is shown in the TextBox on my main form.
Thanks for your answers and comments!

I'm trying to get a textbox to display my string from another form

And dammit, I'm getting frustrated. I'm doing my first mini-game, I think the name in english is Tic-Tac-Toe, and so, I have a main menu where I choose the option 2 players, then i get a inputbox asking for the 2 players names (i used a visual basic reference) and store it in 2 variables that i send to my constructor (?). I'm potuguese so I don't really know how you guys call it, but I'll show you the code.
So, in the first Form, I have:
private void doisJogadoresToolStripMenuItem_Click(object sender, EventArgs e)
{
jogadorTempUm = Microsoft.VisualBasic.Interaction.InputBox("Jogador 1");
jogadorTempDois = Microsoft.VisualBasic.Interaction.InputBox("Jogador 2");
jog = new DoisJogadores(jogadorTempUm, jogadorTempDois);
DoisJogadores novoDois = new DoisJogadores();
novoDois.ShowDialog();
}
That gets sent to the other Form:
public DoisJogadores(string teste1, string teste2)
{
jogador1 = teste1;
jogador2 = teste2;
}
And I save it in the class:
private string jogador1
private string jogador2
And the values get saved there. But I tried to place them in a textbox to show the players names and it just goes blank.
Anyone that can help me?
You are showing not that form which you are passing values to. Here is a fix
private void doisJogadoresToolStripMenuItem_Click(object sender, EventArgs e)
{
jogadorTempUm = Microsoft.VisualBasic.Interaction.InputBox("Jogador 1");
jogadorTempDois = Microsoft.VisualBasic.Interaction.InputBox("Jogador 2");
// pass values to form you are creating
DoisJogadores novoDois =
new DoisJogadores(jogadorTempUm, jogadorTempDois);
novoDois.ShowDialog();
}
Also make sure you are assigning jogador1 and jogador2 to textboxes. E.g. you can subscribe to form's load event in DoisJogadores form:
private void DoisJogadores_Load(object sender, EventArgs e)
{
textbox1.Text = jogador1;
textbox2.Text = jogador2;
}

Categories

Resources