Hello everyone,

I've been programming in VB.Net since i started and now i want to do everything in C#, but i ran in to some trouble in VB.Net is really easy

If(form1.Visible=true)Then
{
form1.Textbox1.Text = Textbox1.Text
//And So on for any field that i wanted
}

But in C# I'm unable to call something like this.

So here are the details, the main form is opened and there is a button that opens another form that brings up a list of articles(in a datagridview) and when i double click the article that i want the selection goes to the main form that was already opened to its corresponding textboxes

thanks in advance

If you are using the same code above for your C# sharp project you will need to use

If(form1.Visible == true)Then   -- note the double ==

as this is the correct C# form to test for equivalence. Single equal signs set a variable and can't be used to as you have here.

The form called 'Articulos' is the main form in that module, so the one called 'Lista de Familia' is a subform that allows the user to select the property family of that article, so that subform allows me to select the property that will be passed to the form 'Articulos', behind the datagridview 'Familia' are to textboxes that will call TB1(id) and TB2(Familia) that are databound to the datagridview and when I double click in this image the property text (what is actually in the textboxes) will be passed to the 'Articulos' form

When i used to do it in VB.Net, i was like this

If(Articulos.Visible=true)Then
{
Articulos.FamilyTextBox.Text = TB1.Text 'The ID Textbox
Articulos.label3.Text = TB2.Text ' This is the description Family Column
Me.Close
}

Hope im more clear about my detail, thanks for the reply Hericles

I would suggest that you don't databind objects like comboBoxes and textBoxes because it means your program will be less flexible, I tried (or think i did) something similar to what you're doing and found databinding object meant i couldn't add particular functionality i wanted. If you databind an object rather than writing seperate methods for the occurence of particular event (in you case one event for when a record is clicked in the main form and a seperate method for when a record is clicked in the subform) your code may become messy and less managable.

More to the point of your issue I used this line of code to retrieve data from a spacific cell within a spacific row of the DGV and set it to a textBox:

textBox1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();

Hope this helps

Hello,

i don't know much, but i know enough to know you have some syntax errors... i think.

If(form1.Visible=true)Then
{
form1.Textbox1.Text = Textbox1.Text
//And So on for any field that i wanted
}

In C# you need to use 2 = "==" or else you'll get some errors. You don't need the "Then" thingy in C#. And finally you need to use ";".

The correct code would be :

If (form1.Visible==true)
{
    form1.Textbox1.Text = Textbox1.Text;
}

Hope this helps you. :)

The code that i added from VB.Net is how its supposed to be done in VB.Net, that code goes in the subform(lista de familia) that allows me to select any value on the datagridview in that subform then pass the selected value to mainform(articulos)

So MainForm(Articulos) is opened already and SubForm(lista de familia) is opened by MainForm by a button, so when i doubleclick the any value in SubForm i want the values to be passed to the MainForm.

THIS CODE BELOW IS HOW I USED TO DO IT IN VB.NET AND IN C# IT DOESNT WORK THAT WAY.

//MainForm(Articulos) is already open

//this code goes in SubForm(Lista de Familia) under Cell Double Click method
If(Articulos.Visible=true)Then
{
Articulos.FamilyTextBox.Text = TB1.Text 'The ID Textbox
Articulos.label3.Text = TB2.Text ' This is the description Family Column
Me.Close
}

i hope im more clear with my problem

I ADDED I SMALL VIDEO ON HOW IT WAS DONE IN VB.NET, STILL I HAVENT BEEN ABLE TO DO IT IN C#

YouTube Video

As I mentioned in my first post the line

If(Articulos.Visible == true) {

should work. You keep saying you can't do the versionyou posted in C# but you never mention what goes wrong when you try. A couple of us have now pointed out that using == instead of = when make the if statement work but you don't seem to have tried it. What exactly goes wrong when you make the changes we suggest?

Hello Hericles

The way i can verify that the form is opened is this way

if (Articulos.ActiveForm.Visible == true)
            {
               //THIS DOES WORK
            }

Now what i need is the following how to pass from a textbox in the subform to the mainform that is already opened

MainForm.TextBox1.Text = SubForm.TextBox1.Text

I Solved my Issue

In MainForm i added a this method

public  void GetFamilia(){
            familiaTextBox.Text = Properties.Settings.Default.fam_id.ToString();
            label2.Text = Properties.Settings.Default.fam_desc.ToString();

        }

And of the SubForm i added this method on the CellClick

 private void AceptarSeleccion(object sender, DataGridViewCellEventArgs e)
        {
            if (Articulos.ActiveForm.Visible == true)
            {
                Properties.Settings.Default.fam_id = int.Parse(idTextBox.Text);
                Properties.Settings.Default.fam_desc = familiaTextBox.Text;
                Properties.Settings.Default.Save();
                articulos.GetFamilia();

                // Set a property on Form1 (w/o worrying about the implementation) form1.LabelText = textForLabelInForm1.Text;
               // MessageBox.Show("test");
            }


        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.