Public variable invoking incorrect result - c#

public partial class ThanglishToTamilGUI : Form
{
public string anz;
public ThanglishToTamilGUI()
{
InitializeComponent();
}
public void btnConvertToBraille_Click(object sender, EventArgs e)
{
anz = richTextBoxTamil.Text.ToString();
GUI.TamilToBrailleGUI c1 = new GUI.TamilToBrailleGUI();
c1.Visible = true;
}
}
I need to pass my richtextbox (richTextBoxTamil) content to variable call anz.
I am retrriving anz variable in other form as form load event:
private void TamilToBrailleGUI_Load(object sender, EventArgs e)
{
ThanglishToTamilGUI tt = new ThanglishToTamilGUI();
String apper = tt.anz;
richTextBoxTamil.Text = apper;
}
My Problem:
I am getting null values as result. Since if I assigned any values that invoked correctly.
public partial class ThanglishToTamilGUI : Form
{
public string anz = "Hai";
public ThanglishToTamilGUI()
{
InitializeComponent();
} ...
Here my ans value is passed as "Hai". But my requirement is to get what ever the content in the richTextBoxTamil and pass it to that public variable call anz. What went wrong here please help me.
Thank you.

This is the problem:
ThanglishToTamilGUI tt = new ThanglishToTamilGUI();
String apper = tt.anz;
How do you expect apper to ever be anything other than null? You're fetching the variable from a freshly-created form, which has never been shown, and which has never had btnConvertToBraille_Click called on it.
Presumably there's an existing ThanglishToTamilGUI object somewhere, and that's the one you want to fetch the variable from. Basically, one form needs to know about the instance of the other form.
(I'd also strongly suggest using a property rather than a public variable, but that's a different matter. You might not even need to have a separate variable at all - just declare a property which fetches richTextBoxTamil.Text.)
Alternatively, just pass the relevant string to the constructor of the new form:
public void btnConvertToBraille_Click(object sender, EventArgs e)
{
GUI.TamilToBrailleGUI c1 = new GUI.TamilToBrailleGUI(richTextBoxTamil.Text);
c1.Visible = true;
}
Then the new form doesn't need to know about the old form at all - it only needs to know the text to display.
(You might want to pull it out of the constructor and into a settable property, but it's the same basically principle: the code creating the form pushes the data, rather than the new form pulling it.)

You can create a public property to access the current Text value of the textbox.
public string RichTextBoxText
{
get
{
return richTextBoxTamil.Text;
}
}
The way you do it now the form is instantiated, but the click event is not fired. So there's no way you will get anything other than what you initialized the field to.
Load is not the place to look for user input. An event (like click) is where you need to check the property value:
private void SomeClick(object sender, EventArgs e)
{
String result = thanglishToTamilGUIObject.RichTextBoxText;
//do something with text
}

Related

Getting value pointer and use it from somewhere else

First , I'm a C++ dev and new to C# , Sorry for this simple question.
I'm creating a wrapper for my native library.
I want to pass a value in a class and edit it from some other function in class , like pointers in C++ , I found out it can be done with unsafe mode but I need to do it without unsafe and I'm sure it's possible.
Here's my code :
Main Console
namespace ConsoleApp2
{
class Program
{
[STAThread]
static void Main(string[] args)
{
string DATA_VALUE = "Not Set Yet";
new Data_Picker_Form(DATA_VALUE).ShowDialog();
Console.WriteLine("Value is {0}", DATA_VALUE);
Console.ReadKey();
}
}
}
Form Code
using System;
using System.Windows.Forms;
namespace ConsoleApp2
{
public partial class Data_Picker_Form : Form
{
object data_in_obj;
public Data_Picker_Form(string data_in)
{
InitializeComponent();
data_in_obj = data_in;
}
private void button1_Click(object sender, EventArgs e)
{
string data_in_new = data_in_obj as string;
data_in_new = "OUTPUT_VALUE";
this.Close();
}
}
}
it's not working unfortunately , so I need to pass my string , int and etc. value to a new form , in initializing form creates a instance [like pointer] to original string and it can accessible from other functions ins class like button click.
thanks.
You already have a reference (sort of like a pointer), and that is your problem (well, half of it anyways).
string data_in_new = data_in_obj as string;
Says to create a variable that holds a string reference (as strings are immutable reference types) and copy the reference data_in_obj currently has to it. When you then reassign data_in_new it of course doesn't affect any other variable (just like it wouldn't in C++).
Because strings are immutable, there is no way for that code to affect other things that point to that string (passing by reference aside, but this is about variables/members). You need to store it in a simple struct or class so that everyone is pointing at an object that holds the current string reference, and can be updated.
Same idea with your int, it is actually a value type so any copy will copy the actual value, you can't change a different variable through it. You need a wrapper class so that each user is pointing at the same object.
What about setting up a simple get and set function since you are already Initializing your Form?
In Your Form:
private string data_in_new = "";
public Data_Picker_Form(string DATA_VALUE)
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Data_in_new = "OUTPUT_VALUE";
Close();
}
public string Data_in_new
{
get
{
return data_in_new;
}
set
{
data_in_new = value;
}
}
And in your Console:
class Program
{
static void Main(string[] args)
{
string DATA_VALUE = "Not Set Yet";
Data_Picker_Form D_P_T = new Data_Picker_Form(DATA_VALUE);
D_P_T.ShowDialog();
DATA_VALUE = D_P_T.Data_in_new;
Console.WriteLine("Value is {0}", DATA_VALUE);
Console.ReadKey();
}
}
This Way you can access you value at any desired point in the project.

Getting Data from form to class file in C# Windows Form Application

I have a form with form.cs file and another separate class file named car.cs. In my form I have a textbox named textbox1. Now in the separate class file I have defined a variable as follows with its property :
private string carMake;
public string CarMake
{
get { return carMake; }
set { carMake = value; }
}
how can I set carMake value to the textbox1.text value by the get and set methods.
Typically I data bind the domain object property to the Winforms control field. See Data binding for TextBox
Of course you can set it directly as well.
Example:
void buttonSave_Click(object sender, EventArgs e)
{
car.CarMake = textBox1.Value;
// ... save car object to database
}
public void SetValue()
{
car objCar=new car();
objCar.carMake=textbox1.Text;
}
just try this.

How do i transfer control information between windows

I wanted to know how do i transfer multiple information between windows in WPF.
So far I have this:
Main form:
string path = #"C:\";
private void preview_Click(object sender, RoutedEventArgs e) {
preview newWindow = new preview(Path);
newWindow.Show();
}
The preview form:
public preview(string _path) {
InitializeComponent();
Path = _path;
}
But this only allows me to send one piece of information at a time. How would I send multiple information at one time?
Well, if you're sticking to that particular design, then you can send information in a class or a struct, which you would pass instead of the string:
preview newWindow = new preview(data);
public preview(CustomData data) { ... }
Or just pass multiple arguments if your constructor accepts multiple parameters:
preview newWindow = new preview(path, somethingElse, somethingMore);
public preview(string path, int somethingElse, int somethingMore) { ... }
I just prefer to keep things nice and tight if I work with some sort of collection of data a lot, that's why I recommended class or struct. However, if pieces of information have no particular relation to each other beyond being passed together, then multiple parameters approach works just fine.
You can pass your three values in a three parameter constructor of preview class.
string path = #"C:\";
private void preview_Click(object sender, RoutedEventArgs e) {
preview newWindow = new preview(1,"string1","String2");
newWindow.Show();
}
The preview form:
int a;
string b;
string c;
public preview(int _a, string _b, string _c)
{
InitializeComponent();
this.a=_a;
this.b=_b;
this.c=_c;
}
If you have to pass more values to Preview form I would suggest you should create a DTO class that will contain all the values you want to pass to preview form. Create instance of DTO class on your mainform and set values as required and pass this DTO class to preview form and on preview form you can have a custructor that will accept this dto class.

Populating data in dropdown menu

I am new to c# so bit stuck at what I thought was a very simple module. I just need to display data in the dropdown menu but getting some error while binding... or I will say even before binding. Here is what I am trying to do..I am really sorry if I am doing a very simple mistake but I tried my best & now I think I need some guidance..
CustomService.cs
public partial class CustomService
{
public List<Code> GetDepartment(bool activeOnly)
{
List<Code> retVal = new List<Code>();
---some code----
return retVal;
}
}
ProgramList.ascx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<Code> dept = new List<Code>CustomService.GetDepartment(true);
ddlDepartment.DataSource = dept;
ddlDepartment.DataBind();
}
}
//error an object reference is required for an nonstatic field, method or Property CustomService.GetDepartment(true);
you forgot to create object first and than you can call the method
another thing is you just need to assign the value directly as i did below, there is no need to create any new list
check the code below that will work for you
CustomService custsrv = new CustomService();
List<Code> dept = custsrv.GetDepartment(true);
To be able to call the method GetDepartment, you need to have a new instance of CustomService created:
CustomService service = new CustomService();
service.GetDepartment(true);
or to make the method static:
public static List<Code> GetDepartment(bool activeOnly) { }
However, if you put it static, every variables used by that method that reside inside the class will also need to be static.
I think this would help.
CustomService custS = new CustomService();
ddlDepartment.DataSource = custS.GetDepartment(true);
ddlDepartment.DataBind();

WinForms - Baffled - How to Handle Certain Controls Dynamically - Properly

I have a System.Windows.Form class (my main class). There is a RootMenu object. This is my own custom object. I'm trying to loop through the RootMenu object and on each pass add a ToolStripMenuItem to a ContextMenuStrip (which I named ContextMenu). The RootMenu object contains a List. Links have Names and Urls (both strings).
When the form loads my "Factory" class loads me up a RootMenu object, which I then pass into the ProcessMenu method.
Code Excerpt Here:
private void ProcessMenu(RootMenu rm)
{
foreach (var lnk in rm.Links)
{
var tsmi = new ToolStripMenuItem(lnk.Name, null, new EventHandler(Navigate));
tsmi.ToolTipText = lnk.Url;
ContextMenu.Items.Add(tsmi);
}
}
private void Navigate(object sender, EventArgs e)
{
var tsmi = (ToolStripMenuItem) sender;
System.Diagnostics.Process.Start(tsmi.ToolTipText);
}
Do you see how I have to store the lnk.Url in the ToolTipText? In the VB6 days all the controls had the "tag" property. You used to be able to stuff extra stuff into the control that you would need later on. I don't want to use the tooltip for this, but what are my alternatives? Storing all the Urls in a Hash/Dictionary using the name as a key? I may not always have unique names, so I would like to avoid this route. What is the proper way to handle this in .NET? Maybe I missing some basic concept I have never been exposed to.
ToolStripMenuItem has a Tag property:
tsmi.Tag = lnk.Url;
In fact, quite a few Windows Forms controls have it.
Just inherit the old class and stick a Tag property in there:
public class myToolStripMenuItem : ToolStripMenuItem
{
public object myTag { get; set; }
}
Create your own object inheriting from ToolStripMenuItem and add any custom properties....
private void ProcessMenu(RootMenu rm)
{
foreach (var lnk in rm.Links)
{
var tsmi = new UrlToolStripMenuItem(lnk.Name, null, new EventHandler(Navigate))
{
Url = lnk.Url,
};
ContextMenu.Items.Add(tsmi);
}
}
private void Navigate(object sender, EventArgs e)
{
var tsmi = (UrlToolStripMenuItem)sender;
System.Diagnostics.Process.Start(tsmi.Url);
}
public class UrlToolStripMenuItem : ToolStripMenuItem
{
public UrlToolStripMenuItem(string text, Image image, EventHandler onClick) : base(text, image, onClick)
{
}
public string Url { get; set; }
}

Categories