Click here to Skip to main content
15,890,557 members
Home / Discussions / C#
   

C#

 
Questioniserting,deleting,updating mysql database FROM form in C# Pin
Xonel6-Feb-12 20:18
Xonel6-Feb-12 20:18 
AnswerRe: iserting,deleting,updating mysql database FROM form in C# Pin
V.6-Feb-12 21:37
professionalV.6-Feb-12 21:37 
AnswerRe: iserting,deleting,updating mysql database FROM form in C# Pin
Mycroft Holmes7-Feb-12 0:00
professionalMycroft Holmes7-Feb-12 0:00 
Questionhow to calculate other forms textbox values from one form in c#? Pin
v surya dev6-Feb-12 19:46
v surya dev6-Feb-12 19:46 
AnswerRe: how to calculate other forms textbox values from one form in c#? Pin
Abhinav S6-Feb-12 20:09
Abhinav S6-Feb-12 20:09 
AnswerRe: how to calculate other forms textbox values from one form in c#? Pin
AmbiguousName6-Feb-12 20:17
AmbiguousName6-Feb-12 20:17 
GeneralRe: how to calculate other forms textbox values from one form in c#? Pin
v surya dev7-Feb-12 5:31
v surya dev7-Feb-12 5:31 
AnswerRe: how to calculate other forms textbox values from one form in c#? Pin
BillWoodruff7-Feb-12 15:17
professionalBillWoodruff7-Feb-12 15:17 
A key piece of information here is how the Forms are created. If there is one primary Form that creates, and Shows, the other Forms, then, at the point where you create the secondary Forms, you have a reference to that newly created Form.

That means that if you do this in your main form:
Form2 F2 = new Form2();
And you define the variable 'F2 as being within the scope of the main form: you can always access the instance of Form2, 'F2, within the main form: once you have instantiated it ... most likely in the main Form Load EventHandler.

The question that you should be asking yourself now is: "wait a minute, 'F2 has a TextBox on it, and that TextBox is an access-as-private-only Control: it can't be seen using the reference to 'F2: this won't work:
Form2 F2;

TextBox TextBoxOnForm2;

private void Form1_Load(object sender, EventArgs e)
{
   F2 = new Form2();
   F2.Show();
   TextBoxOnForm2 = // cannot be accessed
}
And this is the point at which I encourage you to start thinking about the issue of "what is the bare essential content, or control, that one Form needs to access from another ?: does it need to only access content in the Control, or does it need access to the Control itself so that it can modify the content of the Control, and/or its settings, or visual appearance, or whatever."

In this case it appears you want access to either a TextBox itself implying you might want to both read from and write to the TextBox perhaps make changes to its structrure: probably, at least, access the contents of the TextBox.

Let's take the case where you want full access to the TextBox on the instance of Form2, 'F2:

1. first define a public property in Form2 of Type TextBox:
C#
public TextBox F2TextBox
{
    private set;
    get { }
}
2. then in the Load or Shown events of Form2: set that public property to the instance of the TextBox: so your code in Form2 might look like this:
C#
public TextBox F2TextBox;

private void Form2_Load(object sender, EventArgs e)
{
    F2TextBox = textBox1;
}
And now in your main form, you no longer need to keep a reference to the whole instance of Form2; you just need access to the TextBox on Form2:
public TextBox TextBoxOnForm2;

private void Form1_Load(object sender, EventArgs e)
{
   // note in this example we no longer keep a reference in the main form to the instance of Form2 !
   Form2 F2 = new Form2();
   F2.Show();
   TextBoxOnForm2 = F2.F2TextBox;
}
Now you have access to the TextBox itself: you can manipulate its Size, BackGroundColor, whatever, an access its Text by TextBoxOnForm2.Text

And if you want to get the current content (Text) in the TextBox on F2 "on demand," for example,, by a Button Click EventHandler on the main form:
C#
private void button1_Click(object sender, EventArgs e)
{
    SomeTextBoxOnForm1.Text = TextBoxOnForm2 .Text;
}
What if you need to instantly synchronize any change in the Text on F2 with other Text somewhere on the main Form: then you are going to have to define a Public Event that accesses F2's TextBox, and Raises that Event with each change in the Text in the TextBox on the instance of Form2: the main form will then have to "subscribe" to that Event. The answer above by Abhinav points you to the use of Delegates to help you with that.

There are other ways, also, to approach "access to control and/or data" between Forms, like the use of Interfaces, and Static classes.

And we could re-write this example so the only thing you could get access to on Form2 was the actual Text content of the TextBox, not the TextBox itself, but we'll leave that for you to think about. But, here's a hint: the public Property defined on Form2 is now going to look something like this:
public string F2TextBoxText 
{
    set { textBox1.Text = value; }
    get { return textBox1.Text; }
}

"The first principle is that you must not fool yourself, and you are the easiest person to fool." Richard Feynman


modified 8-Feb-12 9:36am.

QuestionBasic ComboBox Problem :( Pin
AmbiguousName6-Feb-12 18:58
AmbiguousName6-Feb-12 18:58 
AnswerRe: Basic ComboBox Problem :( Pin
Abhinav S6-Feb-12 19:07
Abhinav S6-Feb-12 19:07 
GeneralRe: Basic ComboBox Problem :( Pin
AmbiguousName6-Feb-12 19:21
AmbiguousName6-Feb-12 19:21 
GeneralRe: Basic ComboBox Problem :( Pin
Mycroft Holmes6-Feb-12 19:49
professionalMycroft Holmes6-Feb-12 19:49 
GeneralRe: Basic ComboBox Problem :( Pin
Abhinav S6-Feb-12 20:07
Abhinav S6-Feb-12 20:07 
GeneralRe: Basic ComboBox Problem :( Pin
AmbiguousName6-Feb-12 20:09
AmbiguousName6-Feb-12 20:09 
NewsRe: Basic ComboBox Problem :( Pin
V.6-Feb-12 21:38
professionalV.6-Feb-12 21:38 
GeneralRe: Basic ComboBox Problem :( Pin
BobJanova6-Feb-12 22:21
BobJanova6-Feb-12 22:21 
GeneralRe: Basic ComboBox Problem :( Pin
Mycroft Holmes6-Feb-12 23:56
professionalMycroft Holmes6-Feb-12 23:56 
GeneralRe: Basic ComboBox Problem :( Pin
BobJanova8-Feb-12 4:05
BobJanova8-Feb-12 4:05 
AnswerRe: Basic ComboBox Problem :( Pin
Luc Pattyn7-Feb-12 0:20
sitebuilderLuc Pattyn7-Feb-12 0:20 
GeneralRe: Basic ComboBox Problem :( Pin
ProEnggSoft7-Feb-12 6:25
ProEnggSoft7-Feb-12 6:25 
Questionhow to get names and count of tables Pin
phowarso6-Feb-12 17:25
phowarso6-Feb-12 17:25 
AnswerRe: how to get names and count of tables Pin
PIEBALDconsult6-Feb-12 18:08
mvePIEBALDconsult6-Feb-12 18:08 
GeneralRe: how to get names and count of tables Pin
Mycroft Holmes6-Feb-12 19:43
professionalMycroft Holmes6-Feb-12 19:43 
GeneralRe: how to get names and count of tables Pin
PIEBALDconsult7-Feb-12 2:32
mvePIEBALDconsult7-Feb-12 2:32 
AnswerRe: how to get names and count of tables Pin
Abhinav S6-Feb-12 19:10
Abhinav S6-Feb-12 19:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.